Skip to content

Commit 5290e4c

Browse files
committed
Feature: Mutable collection settings
1 parent b836878 commit 5290e4c

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
164164
*/
165165
inline fun <reified T : Any> setting(
166166
name: String,
167-
defaultValue: ArrayList<T>,
167+
defaultValue: List<T>,
168168
description: String = "",
169169
noinline visibility: () -> Boolean = { true },
170-
) = ListSetting(name, defaultValue, TypeToken.getParameterized(ArrayList::class.java, T::class.java).type, description, visibility).also {
170+
) = ListSetting(name, defaultValue.toMutableList(), TypeToken.getParameterized(MutableList::class.java, T::class.java).type, description, visibility).also {
171171
settings.add(it)
172172
}
173173

@@ -193,7 +193,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
193193
defaultValue: Map<K, V>,
194194
description: String = "",
195195
noinline visibility: () -> Boolean = { true },
196-
) = MapSetting(name, defaultValue, TypeToken.getParameterized(Map::class.java, K::class.java, V::class.java).type, description, visibility).also {
196+
) = MapSetting(name, defaultValue.toMutableMap(), TypeToken.getParameterized(Map::class.java, K::class.java, V::class.java).type, description, visibility).also {
197197
settings.add(it)
198198
}
199199

@@ -219,7 +219,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
219219
defaultValue: Set<T>,
220220
description: String = "",
221221
noinline visibility: () -> Boolean = { true },
222-
) = SetSetting(name, defaultValue, TypeToken.getParameterized(Set::class.java, T::class.java).type, description, visibility).also {
222+
) = SetSetting(name, defaultValue.toMutableSet(), TypeToken.getParameterized(Set::class.java, T::class.java).type, description, visibility).also {
223223
settings.add(it)
224224
}
225225

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package com.lambda.config.settings.collections
22

33
import com.google.gson.JsonElement
4-
import com.google.gson.reflect.TypeToken
54
import com.lambda.Lambda.gson
65
import com.lambda.config.AbstractSetting
76
import java.lang.reflect.Type
8-
import java.util.ArrayList
97

108
class ListSetting<T : Any>(
119
override val name: String,
12-
defaultValue: ArrayList<T>,
10+
private val defaultValue: MutableList<T>,
1311
private val type: Type,
1412
description: String,
1513
visibility: () -> Boolean,
16-
) : AbstractSetting<ArrayList<T>>(
14+
) : AbstractSetting<MutableList<T>>(
1715
defaultValue,
1816
description,
1917
visibility
2018
) {
2119
override fun loadFromJson(serialized: JsonElement) {
2220
value = gson.fromJson(serialized, type)
2321
}
22+
23+
override fun toJson(): JsonElement {
24+
value = defaultValue.toMutableList() // Hack the Delegates.observable
25+
return gson.toJsonTree(value)
26+
}
2427
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import java.lang.reflect.Type
77

88
class MapSetting<K, V>(
99
override val name: String,
10-
defaultValue: Map<K, V>,
10+
private val defaultValue: MutableMap<K, V>,
1111
private val type: Type,
1212
description: String,
1313
visibility: () -> Boolean,
14-
) : AbstractSetting<Map<K, V>>(
14+
) : AbstractSetting<MutableMap<K, V>>(
1515
defaultValue,
1616
description,
1717
visibility
1818
) {
1919
override fun loadFromJson(serialized: JsonElement) {
2020
value = gson.fromJson(serialized, type)
2121
}
22-
}
22+
23+
override fun toJson(): JsonElement {
24+
value = defaultValue.toMutableMap() // Hack the Delegates.observable
25+
return gson.toJsonTree(value)
26+
}
27+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import java.lang.reflect.Type
77

88
class SetSetting<T : Any>(
99
override val name: String,
10-
defaultValue: Set<T>,
10+
private val defaultValue: MutableSet<T>,
1111
private val type: Type,
1212
description: String,
1313
visibility: () -> Boolean,
14-
) : AbstractSetting<Set<T>>(
14+
) : AbstractSetting<MutableSet<T>>(
1515
defaultValue,
1616
description,
1717
visibility
1818
) {
1919
override fun loadFromJson(serialized: JsonElement) {
2020
value = gson.fromJson(serialized, type)
2121
}
22-
}
22+
23+
override fun toJson(): JsonElement {
24+
value = defaultValue.toMutableSet() // Hack the Delegates.observable
25+
return gson.toJsonTree(value)
26+
}
27+
}

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ object Loader {
4242
}
4343
}
4444

45-
FriendManager.add(GameProfile(UUID.randomUUID(), "Lambda"))
46-
4745
LOG.info("${Lambda.MOD_NAME} ${Lambda.VERSION} was successfully initialized (${initTime}ms)")
4846
}
4947
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/GuiConfigurable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import com.lambda.gui.impl.clickgui.windows.TagWindow
77

88
object GuiConfigurable : Configurable(GuiConfig), Loadable {
99
override val name = "gui"
10-
val windows = setting("windows", arrayListOf(TagWindow()))
10+
val windows = setting("windows", listOf(TagWindow()))
1111
}

0 commit comments

Comments
 (0)