@@ -13,6 +13,8 @@ import kotlin.reflect.KProperty
1313
1414open class ConfigSection (val internalSection : ConfigurationSection ) {
1515
16+ private val cache: MutableMap <String , Any ?> = mutableMapOf ()
17+
1618 val keys: Set <String >
1719 get() = internalSection.getKeys(false )
1820
@@ -25,18 +27,38 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
2527 }
2628
2729 fun getSection (key : String ): ConfigSection ? {
30+ val cached = cache[key]
31+ if (cached != null ) {
32+ if (cached !is ConfigSection ) {
33+ error(" $key is not a config section" )
34+ }
35+ return cached
36+ }
37+
2838 val newConfig = internalSection.getConfigurationSection(key) ? : return null
29- return ConfigSection (newConfig)
39+ val configSection = ConfigSection (newConfig)
40+ cache[key] = configSection
41+ return configSection
3042 }
3143
3244 fun getSectionOrThrow (key : String ): ConfigSection =
3345 getSection(key) ? : throw KeyNotFoundException (internalSection.currentPath, key)
3446
3547 fun getFluid (key : String ): PylonFluid ? {
48+ val cached = cache[key]
49+ if (cached != null ) {
50+ if (cached !is PylonFluid ) {
51+ return null
52+ }
53+ return cached
54+ }
55+
3656 val name = get<String >(key) ? : return null
37- return PylonRegistry .FLUIDS [
57+ val fluid = PylonRegistry .FLUIDS [
3858 NamespacedKey .fromString(name) ? : error(" '$name ' is not a namespaced key" )
3959 ]
60+ cache[key] = fluid
61+ return fluid
4062 }
4163
4264 fun getFluidOrThrow (key : String ): PylonFluid {
@@ -47,8 +69,18 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
4769 }
4870
4971 fun getMaterial (key : String ): Material ? {
72+ val cached = cache[key]
73+ if (cached != null ) {
74+ if (cached !is Material ) {
75+ return null
76+ }
77+ return cached
78+ }
79+
5080 val name = get<String >(key) ? : return null
51- return Material .getMaterial(name.uppercase())
81+ val material = Material .getMaterial(name.uppercase())
82+ cache[key] = material
83+ return material
5284 }
5385
5486 fun getMaterialOrThrow (key : String ): Material {
@@ -64,7 +96,17 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
6496 ? : error(" No such item $key " )
6597
6698 fun <T > get (key : String , type : Class <out T >): T ? {
99+ val cached = cache[key]
100+ if (cached != null ) {
101+ if (! type.isInstance(cached)) {
102+ return null
103+ }
104+ @Suppress(" UNCHECKED_CAST" )
105+ return cached as T ?
106+ }
107+
67108 val value = internalSection.get(key) ? : return null
109+ cache[key] = value
68110 return type.cast(value)
69111 }
70112
0 commit comments