Skip to content

Commit c83fcbc

Browse files
committed
store configurable in setting, check and throw if duplicate configuration / configurable, and remove visibility edit (which we can add back at a later date to setting core if it's needed)
1 parent 48a79ad commit c83fcbc

File tree

5 files changed

+43
-46
lines changed

5 files changed

+43
-46
lines changed

src/main/kotlin/com/lambda/command/commands/ConfigCommand.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ object ConfigCommand : LambdaCommand(
7676
executeWithResult {
7777
val confName = config().value()
7878
val settingName = setting().value()
79-
val conf = Configuration.configurableByCommandName(confName) ?: run {
79+
val configurable = Configuration.configurableByCommandName(confName) ?: run {
8080
return@executeWithResult failure("$confName is not a valid configurable.")
8181
}
82-
val setDel = Configuration.settingDelegateByCommandName(conf, settingName) ?: run {
82+
val setting = Configuration.settingByCommandName(configurable, settingName) ?: run {
8383
return@executeWithResult failure("$settingName is not a valid setting for $confName.")
8484
}
85-
setDel.reset()
85+
setting.reset()
8686
return@executeWithResult success()
8787
}
8888
}

src/main/kotlin/com/lambda/config/ConfigEditor.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ open class SettingGroupEditor<T : Configurable>(open val c: T) {
9292
hide(settings.map { it.setting() })
9393

9494
open class BasicEditBuilder(val c: SettingGroupEditor<*>, open val settings: Collection<Setting<*, *>>) {
95-
@SettingEditorDsl
96-
fun visibility(vis: () -> Boolean) =
97-
settings.forEach { it.visibility = vis }
98-
9995
@SettingEditorDsl
10096
fun hide() = c.hide(settings)
10197

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.google.gson.JsonElement
2121
import com.google.gson.JsonObject
2222
import com.google.gson.reflect.TypeToken
2323
import com.lambda.Lambda.LOG
24+
import com.lambda.config.Configuration.Companion.configurables
2425
import com.lambda.config.settings.CharSetting
2526
import com.lambda.config.settings.FunctionSetting
2627
import com.lambda.config.settings.StringSetting
@@ -61,7 +62,7 @@ import java.awt.Color
6162
* @property settings A set of [SettingCore]s that this configurable manages.
6263
*/
6364
abstract class Configurable(
64-
private val configuration: Configuration,
65+
val configuration: Configuration,
6566
) : Jsonable, Nameable {
6667
val settings = mutableListOf<Setting<*, *>>()
6768
val settingGroups = mutableListOf<SettingGroup>()
@@ -70,7 +71,11 @@ abstract class Configurable(
7071
registerConfigurable()
7172
}
7273

73-
private fun registerConfigurable() = configuration.configurables.add(this)
74+
private fun registerConfigurable() {
75+
if (configurables.any { it.name == name })
76+
throw IllegalStateException("Configurable with name $name already exists")
77+
configuration.configurables.add(this)
78+
}
7479

7580
fun <T : SettingCore<R>, R : Any> Setting<T, R>.register() = apply {
7681
if (settings.any { it.name == name })
@@ -101,22 +106,22 @@ abstract class Configurable(
101106
defaultValue: Boolean,
102107
description: String = "",
103108
visibility: () -> Boolean = { true },
104-
) = Setting(name, description, BooleanSetting(defaultValue), visibility).register()
109+
) = Setting(name, description, BooleanSetting(defaultValue), this, visibility).register()
105110

106111
inline fun <reified T : Enum<T>> setting(
107112
name: String,
108113
defaultValue: T,
109114
description: String = "",
110115
noinline
111116
visibility: () -> Boolean = { true },
112-
) = Setting(name, description,EnumSetting(defaultValue), visibility).register()
117+
) = Setting(name, description,EnumSetting(defaultValue), this, visibility).register()
113118

114119
fun setting(
115120
name: String,
116121
defaultValue: Char,
117122
description: String = "",
118123
visibility: () -> Boolean = { true },
119-
) = Setting(name, description, CharSetting(defaultValue), visibility).register()
124+
) = Setting(name, description, CharSetting(defaultValue), this, visibility).register()
120125

121126
fun setting(
122127
name: String,
@@ -125,7 +130,7 @@ abstract class Configurable(
125130
flags: Int = ImGuiInputTextFlags.None,
126131
description: String = "",
127132
visibility: () -> Boolean = { true },
128-
) = Setting(name, description, StringSetting(defaultValue, multiline, flags), visibility).register()
133+
) = Setting(name, description, StringSetting(defaultValue, multiline, flags), this, visibility).register()
129134

130135
@JvmName("collectionSetting1")
131136
fun setting(
@@ -134,7 +139,7 @@ abstract class Configurable(
134139
immutableCollection: Collection<Block> = Registries.BLOCK.toList(),
135140
description: String = "",
136141
visibility: () -> Boolean = { true },
137-
) = Setting(name, description, BlockCollectionSetting(immutableCollection, defaultValue.toMutableList()), visibility).register()
142+
) = Setting(name, description, BlockCollectionSetting(immutableCollection, defaultValue.toMutableList()), this, visibility).register()
138143

139144
@JvmName("collectionSetting2")
140145
fun setting(
@@ -143,7 +148,7 @@ abstract class Configurable(
143148
immutableCollection: Collection<Item> = Registries.ITEM.toList(),
144149
description: String = "",
145150
visibility: () -> Boolean = { true },
146-
) = Setting(name, description, ItemCollectionSetting(immutableCollection, defaultValue.toMutableList()), visibility).register()
151+
) = Setting(name, description, ItemCollectionSetting(immutableCollection, defaultValue.toMutableList()), this, visibility).register()
147152

148153
@JvmName("collectionSetting3")
149154
inline fun <reified T : Comparable<T>> setting(
@@ -160,6 +165,7 @@ abstract class Configurable(
160165
immutableList,
161166
TypeToken.getParameterized(Collection::class.java, T::class.java).type
162167
),
168+
this,
163169
visibility
164170
).register()
165171

@@ -170,7 +176,7 @@ abstract class Configurable(
170176
immutableList: Collection<T> = defaultValue,
171177
description: String = "",
172178
noinline visibility: () -> Boolean = { true },
173-
) = Setting(name, description, ClassCollectionSetting(immutableList, defaultValue.toMutableList()), visibility).register()
179+
) = Setting(name, description, ClassCollectionSetting(immutableList, defaultValue.toMutableList()), this, visibility).register()
174180

175181
// ToDo: Actually implement maps
176182
inline fun <reified K : Any, reified V : Any> setting(
@@ -185,6 +191,7 @@ abstract class Configurable(
185191
defaultValue.toMutableMap(),
186192
TypeToken.getParameterized(MutableMap::class.java, K::class.java, V::class.java).type
187193
),
194+
this,
188195
visibility
189196
).register()
190197

@@ -196,7 +203,7 @@ abstract class Configurable(
196203
description: String = "",
197204
unit: String = "",
198205
visibility: () -> Boolean = { true },
199-
) = Setting(name, description, DoubleSetting(defaultValue, range, step, unit), visibility).register()
206+
) = Setting(name, description, DoubleSetting(defaultValue, range, step, unit), this, visibility).register()
200207

201208
fun setting(
202209
name: String,
@@ -206,7 +213,7 @@ abstract class Configurable(
206213
description: String = "",
207214
unit: String = "",
208215
visibility: () -> Boolean = { true },
209-
) = Setting(name, description, FloatSetting(defaultValue, range, step, unit), visibility).register()
216+
) = Setting(name, description, FloatSetting(defaultValue, range, step, unit), this, visibility).register()
210217

211218
fun setting(
212219
name: String,
@@ -216,7 +223,7 @@ abstract class Configurable(
216223
description: String = "",
217224
unit: String = "",
218225
visibility: () -> Boolean = { true },
219-
) = Setting(name, description, IntegerSetting(defaultValue, range, step, unit), visibility).register()
226+
) = Setting(name, description, IntegerSetting(defaultValue, range, step, unit), this, visibility).register()
220227

221228
fun setting(
222229
name: String,
@@ -226,61 +233,61 @@ abstract class Configurable(
226233
description: String = "",
227234
unit: String = "",
228235
visibility: () -> Boolean = { true },
229-
) = Setting(name, description, LongSetting(defaultValue, range, step, unit), visibility).register()
236+
) = Setting(name, description, LongSetting(defaultValue, range, step, unit), this, visibility).register()
230237

231238
fun setting(
232239
name: String,
233240
defaultValue: Bind,
234241
description: String = "",
235242
visibility: () -> Boolean = { true },
236-
) = Setting(name, description, KeybindSettingCore(defaultValue), visibility).register()
243+
) = Setting(name, description, KeybindSettingCore(defaultValue), this, visibility).register()
237244

238245
fun setting(
239246
name: String,
240247
defaultValue: KeyCode,
241248
description: String = "",
242249
visibility: () -> Boolean = { true },
243-
) = Setting(name, description, KeybindSettingCore(defaultValue), visibility).register()
250+
) = Setting(name, description, KeybindSettingCore(defaultValue), this, visibility).register()
244251

245252
fun setting(
246253
name: String,
247254
defaultValue: Color,
248255
description: String = "",
249256
visibility: () -> Boolean = { true },
250-
) = Setting(name, description, ColorSetting(defaultValue), visibility).register()
257+
) = Setting(name, description, ColorSetting(defaultValue), this, visibility).register()
251258

252259
fun setting(
253260
name: String,
254261
defaultValue: Vec3d,
255262
description: String = "",
256263
visibility: () -> Boolean = { true },
257-
) = Setting(name, description, Vec3DSetting(defaultValue), visibility).register()
264+
) = Setting(name, description, Vec3DSetting(defaultValue), this, visibility).register()
258265

259266
fun setting(
260267
name: String,
261268
defaultValue: BlockPos.Mutable,
262269
description: String = "",
263270
visibility: () -> Boolean = { true },
264-
) = Setting(name, description, BlockPosSetting(defaultValue), visibility).register()
271+
) = Setting(name, description, BlockPosSetting(defaultValue), this, visibility).register()
265272

266273
fun setting(
267274
name: String,
268275
defaultValue: BlockPos,
269276
description: String = "",
270277
visibility: () -> Boolean = { true },
271-
) = Setting(name, description, BlockPosSetting(defaultValue), visibility).register()
278+
) = Setting(name, description, BlockPosSetting(defaultValue), this, visibility).register()
272279

273280
fun setting(
274281
name: String,
275282
defaultValue: Block,
276283
description: String = "",
277284
visibility: () -> Boolean = { true },
278-
) = Setting(name, description, BlockSetting(defaultValue), visibility).register()
285+
) = Setting(name, description, BlockSetting(defaultValue), this, visibility).register()
279286

280287
fun setting(
281288
name: String,
282289
defaultValue: () -> Unit,
283290
description: String = "",
284291
visibility: () -> Boolean = { true }
285-
) = Setting(name, description, FunctionSetting(defaultValue), visibility).register()
292+
) = Setting(name, description, FunctionSetting(defaultValue), this, visibility).register()
286293
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ abstract class Configuration : Jsonable, Loadable {
7272

7373
// Avoid context-leaking warning
7474
private fun register() {
75+
if (configurations.any { it.configName == configName })
76+
throw IllegalStateException("Configuration with name $configName already exists")
77+
7578
fixedRateTimer(
7679
daemon = true,
7780
name = "Scheduler-config-${configName}",
@@ -159,23 +162,16 @@ abstract class Configuration : Jsonable, Loadable {
159162
val configurations = mutableSetOf<Configuration>()
160163
val configurables: Set<Configurable>
161164
get() = configurations.flatMapTo(mutableSetOf()) { it.configurables }
162-
val settings: Set<Setting<out SettingCore<*>, *>>
163-
get() = configurables.flatMapTo(mutableSetOf()) { it.settings }
164-
165-
//ToDo: Store owner in setting
166-
fun configurableBySetting(setting: Setting<*, *>) =
167-
configurables.find { it.settings.any { del -> del.name == setting.name } }
165+
val settings: List<Setting<*, *>>
166+
get() = configurables.flatMapTo(mutableListOf()) { it.settings }
168167

169168
fun configurableByName(name: String) =
170169
configurables.find { it.name == name }
171170

172171
fun configurableByCommandName(name: String) =
173172
configurables.find { it.commandName == name }
174173

175-
fun settingDelegateByName(configurable: Configurable, name: String) =
176-
configurable.settings.find { it.name == name }
177-
178-
fun settingDelegateByCommandName(configurable: Configurable, name: String) =
174+
fun settingByCommandName(configurable: Configurable, name: String) =
179175
configurable.settings.find { it.commandName == name }
180176
}
181177
}

src/main/kotlin/com/lambda/config/Setting.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,12 @@ abstract class SettingCore<T : Any>(
112112
} catch (_: Exception) {
113113
return@executeWithResult failure("$valueString is not a valid JSON string.")
114114
}
115-
val config = Configuration.configurableBySetting(setting)
116115
?: return@executeWithResult failure("No config found for $name.")
117116
val previous = this@SettingCore.value
118117
try {
119118
loadFromJson(parsed)
120119
} catch (_: Exception) {
121-
return@executeWithResult failure("Failed to load $valueString as a ${type::class.simpleName} for $name in ${config.name}.")
120+
return@executeWithResult failure("Failed to load $valueString as a ${type::class.simpleName} for $name in ${setting.configurable.name}.")
122121
}
123122
ConfigCommand.info(setting.setMessage(previous, this@SettingCore.value))
124123
return@executeWithResult success()
@@ -145,7 +144,8 @@ class Setting<T : SettingCore<R>, R : Any>(
145144
override val name: String,
146145
override val description: String,
147146
var core: T,
148-
var visibility: () -> Boolean,
147+
val configurable: Configurable,
148+
val visibility: () -> Boolean,
149149
) : Nameable, Describable {
150150
val originalCore = core
151151
val listeners = mutableListOf<ValueListener<R>>()
@@ -236,8 +236,7 @@ class Setting<T : SettingCore<R>, R : Any>(
236236
fun setMessage(previousValue: R, newValue: R) = buildText {
237237
literal("Set ")
238238
changedMessage(previousValue, newValue)
239-
val config = Configuration.configurableBySetting(this@Setting) ?: return@buildText
240-
clickEvent(ClickEvents.suggestCommand("${CommandRegistry.prefix}${ConfigCommand.name} reset ${config.commandName} $commandName")) {
239+
clickEvent(ClickEvents.suggestCommand("${CommandRegistry.prefix}${ConfigCommand.name} reset ${configurable.commandName} $commandName")) {
241240
hoverEvent(HoverEvents.showText(buildText {
242241
literal("Click to reset to default value ")
243242
highlighted(core.defaultValue.toString())
@@ -261,16 +260,15 @@ class Setting<T : SettingCore<R>, R : Any>(
261260
}
262261

263262
private fun TextBuilder.changedMessage(previousValue: R, newValue: R) {
264-
val config = Configuration.configurableBySetting(this@Setting) ?: return
265-
highlighted(config.name)
263+
highlighted(configurable.name)
266264
literal(" > ")
267265
highlighted(name)
268266
literal(" from ")
269267
highlighted(previousValue.toString())
270268
literal(" to ")
271269
highlighted(newValue.toString())
272270
literal(".")
273-
clickEvent(ClickEvents.suggestCommand("${CommandRegistry.prefix}${ConfigCommand.name} set ${config.commandName} $commandName $previousValue")) {
271+
clickEvent(ClickEvents.suggestCommand("${CommandRegistry.prefix}${ConfigCommand.name} set ${configurable.commandName} $commandName $previousValue")) {
274272
hoverEvent(HoverEvents.showText(buildText {
275273
literal("Click to undo to previous value ")
276274
highlighted(previousValue.toString())

0 commit comments

Comments
 (0)