Skip to content

Commit 1b4c700

Browse files
committed
Patch collection setting loading
1 parent 06ea6c8 commit 1b4c700

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import com.lambda.config.settings.collections.ListSetting
88
import com.lambda.config.settings.collections.MapSetting
99
import com.lambda.config.settings.collections.SetSetting
1010
import com.lambda.config.settings.comparable.BooleanSetting
11+
import com.lambda.config.settings.comparable.EnumSetting
1112
import com.lambda.config.settings.numeric.*
1213
import com.lambda.util.Nameable
1314

1415
/**
1516
* Holds a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
1617
*/
1718
abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
18-
private val settings = mutableSetOf<AbstractSetting<*>>()
19+
val settings = mutableSetOf<AbstractSetting<*>>()
1920

2021
init {
2122
configuration.configurables.add(this) // ToDo: Find non-leaking solution
@@ -30,7 +31,9 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
3031

3132
override fun loadFromJson(serialized: JsonElement) {
3233
serialized.asJsonObject.entrySet().forEach { (name, value) ->
33-
settings.find { it.name == name }?.loadFromJson(value) ?: LOG.info("No saved setting found for $name")
34+
settings.find {
35+
it.name == name
36+
}?.loadFromJson(value) ?: LOG.warn("No saved setting found for $name with $value in ${this::class.simpleName}")
3437
}
3538
}
3639

@@ -43,6 +46,15 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
4346
settings.add(it)
4447
}
4548

49+
inline fun <reified T : Enum<T>> setting(
50+
name: String,
51+
defaultValue: T,
52+
noinline visibility: () -> Boolean = { true },
53+
description: String = ""
54+
) = EnumSetting(name, defaultValue, visibility, description).also {
55+
settings.add(it)
56+
}
57+
4658
fun setting(
4759
name: String,
4860
defaultValue: String,
@@ -52,7 +64,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
5264
settings.add(it)
5365
}
5466

55-
private inline fun <reified T : Any> setting(
67+
inline fun <reified T : Any> setting(
5668
name: String,
5769
defaultValue: List<T>,
5870
noinline visibility: () -> Boolean = { true },
@@ -61,7 +73,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
6173
settings.add(it)
6274
}
6375

64-
private inline fun <reified K : Any, V : Any> setting(
76+
inline fun <reified K : Any, V : Any> setting(
6577
name: String,
6678
defaultValue: Map<K, V>,
6779
noinline visibility: () -> Boolean = { true },
@@ -70,7 +82,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
7082
settings.add(it)
7183
}
7284

73-
private inline fun <reified T : Any> setting(
85+
inline fun <reified T : Any> setting(
7486
name: String,
7587
defaultValue: Set<T>,
7688
noinline visibility: () -> Boolean = { true },

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lambda.config
33
import com.google.gson.JsonElement
44
import com.google.gson.JsonObject
55
import com.google.gson.JsonParser
6+
import com.lambda.Lambda
67
import com.lambda.Lambda.LOG
78
import com.lambda.Lambda.gson
89
import com.lambda.event.EventFlow.lambdaScope
@@ -37,7 +38,7 @@ abstract class Configuration : Jsonable {
3738
serialized.asJsonObject.entrySet().forEach { (name, value) ->
3839
configurables.find {
3940
it.name == name
40-
}?.loadFromJson(value) ?: println("No saved setting found for $name")
41+
}?.loadFromJson(value) ?: LOG.warn("No matching setting found for saved setting $name with $value in $configName config")
4142
}
4243
}
4344

common/src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.lambda.config.settings.collections
22

3+
import com.google.gson.JsonElement
4+
import com.google.gson.reflect.TypeToken
5+
import com.lambda.Lambda
36
import com.lambda.config.AbstractSetting
47

58
class ListSetting<T>(
@@ -11,4 +14,9 @@ class ListSetting<T>(
1114
defaultValue,
1215
visibility,
1316
description
14-
)
17+
) {
18+
override fun loadFromJson(serialized: JsonElement) {
19+
val listType = object : TypeToken<List<T>>() {}.type
20+
value = Lambda.gson.fromJson(serialized, listType)
21+
}
22+
}

common/src/main/kotlin/com/lambda/config/settings/collections/MapSetting.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.lambda.config.settings.collections
22

3+
import com.google.common.reflect.TypeToken
4+
import com.google.gson.JsonElement
5+
import com.lambda.Lambda.gson
36
import com.lambda.config.AbstractSetting
47

58
class MapSetting<K, V>(
@@ -11,4 +14,9 @@ class MapSetting<K, V>(
1114
defaultValue,
1215
visibility,
1316
description
14-
)
17+
) {
18+
override fun loadFromJson(serialized: JsonElement) {
19+
val mapType = object : TypeToken<Map<K, V>>() {}.type
20+
value = gson.fromJson(serialized, mapType)
21+
}
22+
}

common/src/main/kotlin/com/lambda/config/settings/collections/SetSetting.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.lambda.config.settings.collections
22

3+
import com.google.gson.JsonElement
4+
import com.google.gson.reflect.TypeToken
5+
import com.lambda.Lambda.gson
36
import com.lambda.config.AbstractSetting
47

58
class SetSetting<T : Any>(
@@ -11,4 +14,9 @@ class SetSetting<T : Any>(
1114
defaultValue,
1215
visibility,
1316
description
14-
)
17+
) {
18+
override fun loadFromJson(serialized: JsonElement) {
19+
val setType = object : TypeToken<Set<T>>() {}.type
20+
value = gson.fromJson(serialized, setType)
21+
}
22+
}

common/src/main/kotlin/com/lambda/module/modules/BoringModule.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ object BoringModule : Module(
1111
tags = setOf(ModuleTag.MISC, ModuleTag.COMBAT),
1212
) {
1313
private val superBoring by setting("Super Boring", false)
14+
private val boringValue by setting("Boring Value", 0.0, 0.1..5.0, 0.1)
15+
private val boringFriends by setting("Boring Friends List", listOf("Peter", "Paul", "Mary", "John", "Ringo"))
16+
private val boringEnemies by setting("Boring Enemies Set", setOf("Sauron", "Voldemort", "Darth Vader", "The Joker"))
17+
private val boringMap by setting("Boring Map", mapOf("One" to 1, "Two" to 2, "Three" to 3, "Four" to 4, "Five" to 5))
18+
private val boringEnum by setting("Boring Enum", BoringEnum.ONE)
19+
20+
enum class BoringEnum {
21+
ONE, TWO, THREE, FOUR, FIVE
22+
}
1423

1524
init {
1625
listener<TickEvent.Pre> {
17-
if (isEnabled) println("I'm ${if (superBoring) "super boring" else "boring"}!")
26+
if (isEnabled) println("I'm ${if (superBoring) "super boring ($boringValue)" else "boring"}!")
1827
}
1928
}
2029
}

0 commit comments

Comments
 (0)