@@ -55,10 +55,15 @@ abstract class Configurable(
5555 val settings = mutableSetOf<AbstractSetting <* >>()
5656
5757 init {
58- register ()
58+ registerConfigurable ()
5959 }
6060
61- private fun register () = configuration.configurables.add(this )
61+ private fun registerConfigurable () = configuration.configurables.add(this )
62+
63+ inline fun <reified T : AbstractSetting <* >> T.register (): T {
64+ check(settings.add(this )) { " Setting with name $name already exists for configurable: ${this @Configurable.name} " }
65+ return this
66+ }
6267
6368 override fun toJson () =
6469 JsonObject ().apply {
@@ -99,9 +104,7 @@ abstract class Configurable(
99104 defaultValue : Boolean ,
100105 description : String = "",
101106 visibility : () -> Boolean = { true },
102- ) = BooleanSetting (name, defaultValue, description, visibility).also {
103- settings.add(it)
104- }
107+ ) = BooleanSetting (name, defaultValue, description, visibility).register()
105108
106109 /* *
107110 * Creates an [EnumSetting] with the provided parameters and adds it to the [settings].
@@ -124,9 +127,7 @@ abstract class Configurable(
124127 defaultValue : T ,
125128 description : String = "",
126129 noinline visibility : () -> Boolean = { true },
127- ) = EnumSetting (name, defaultValue, description, visibility).also {
128- settings.add(it)
129- }
130+ ) = EnumSetting (name, defaultValue, description, visibility).register()
130131
131132 /* *
132133 * Creates a [CharSetting] with the provided parameters and adds it to the [settings].
@@ -143,9 +144,7 @@ abstract class Configurable(
143144 defaultValue : Char ,
144145 description : String = "",
145146 visibility : () -> Boolean = { true },
146- ) = CharSetting (name, defaultValue, description, visibility).also {
147- settings.add(it)
148- }
147+ ) = CharSetting (name, defaultValue, description, visibility).register()
149148
150149 /* *
151150 * Creates a [StringSetting] with the provided parameters and adds it to the [settings].
@@ -166,9 +165,7 @@ abstract class Configurable(
166165 defaultValue : String ,
167166 description : String = "",
168167 visibility : () -> Boolean = { true },
169- ) = StringSetting (name, defaultValue, description, visibility).also {
170- settings.add(it)
171- }
168+ ) = StringSetting (name, defaultValue, description, visibility).register()
172169
173170 /* *
174171 * Constructs a [ListSetting] instance with the specified parameters and appends it to the [settings] collection.
@@ -193,17 +190,13 @@ abstract class Configurable(
193190 defaultValue : List <T >,
194191 description : String = "",
195192 noinline visibility : () -> Boolean = { true },
196- hackDelegates : Boolean = false,
197193 ) = ListSetting (
198194 name,
199195 defaultValue.toMutableList(),
200196 TypeToken .getParameterized(MutableList ::class .java, T ::class .java).type,
201197 description,
202- hackDelegates,
203198 visibility,
204- ).also {
205- settings.add(it)
206- }
199+ ).register()
207200
208201 /* *
209202 * Constructs a [MapSetting] instance with the specified parameters and appends it to the [settings] collection.
@@ -227,18 +220,14 @@ abstract class Configurable(
227220 name : String ,
228221 defaultValue : Map <K , V >,
229222 description : String = "",
230- hackDelegates : Boolean ,
231223 noinline visibility : () -> Boolean = { true },
232224 ) = MapSetting (
233225 name,
234226 defaultValue.toMutableMap(),
235227 TypeToken .getParameterized(MutableMap ::class .java, K ::class .java, V ::class .java).type,
236228 description,
237- hackDelegates,
238229 visibility
239- ).also {
240- settings.add(it)
241- }
230+ ).register()
242231
243232 /* *
244233 * Constructs a [SetSetting] instance with the specified parameters and appends it to the [settings] collection.
@@ -268,36 +257,7 @@ abstract class Configurable(
268257 TypeToken .getParameterized(MutableSet ::class .java, T ::class .java).type,
269258 description,
270259 visibility,
271- ).also {
272- settings.add(it)
273- }
274-
275- /* *
276- * Creates a [ByteSetting] with the provided parameters and adds it to the [settings].
277- *
278- * The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
279- *
280- * @param name The unique identifier for the setting.
281- * @param defaultValue The default [Byte] value of the setting.
282- * @param range The range within which the setting's value must fall.
283- * @param step The step to which the setting's value is rounded.
284- * @param description A brief explanation of the setting's purpose and behavior.
285- * @param visibility A lambda expression that determines the visibility status of the setting.
286- * @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
287- *
288- * @return The created [ByteSetting].
289- */
290- fun setting (
291- name : String ,
292- defaultValue : Byte ,
293- range : ClosedRange <Byte >,
294- step : Byte = 1,
295- description : String = "",
296- unit : String = "",
297- visibility : () -> Boolean = { true },
298- ) = ByteSetting (name, defaultValue, range, step, description, visibility, unit).also {
299- settings.add(it)
300- }
260+ ).register()
301261
302262 /* *
303263 * Creates a [DoubleSetting] with the provided parameters and adds it to the [settings].
@@ -322,9 +282,7 @@ abstract class Configurable(
322282 description : String = "",
323283 unit : String = "",
324284 visibility : () -> Boolean = { true },
325- ) = DoubleSetting (name, defaultValue, range, step, description, visibility, unit).also {
326- settings.add(it)
327- }
285+ ) = DoubleSetting (name, defaultValue, range, step, description, visibility, unit).register()
328286
329287 /* *
330288 * Creates a [FloatSetting] with the provided parameters and adds it to the [settings].
@@ -349,9 +307,7 @@ abstract class Configurable(
349307 description : String = "",
350308 unit : String = "",
351309 visibility : () -> Boolean = { true },
352- ) = FloatSetting (name, defaultValue, range, step, description, visibility, unit).also {
353- settings.add(it)
354- }
310+ ) = FloatSetting (name, defaultValue, range, step, description, visibility, unit).register()
355311
356312 /* *
357313 * Creates an [IntegerSetting] with the provided parameters and adds it to the [settings].
@@ -376,9 +332,7 @@ abstract class Configurable(
376332 description : String = "",
377333 unit : String = "",
378334 visibility : () -> Boolean = { true },
379- ) = IntegerSetting (name, defaultValue, range, step, description, visibility, unit).also {
380- settings.add(it)
381- }
335+ ) = IntegerSetting (name, defaultValue, range, step, description, visibility, unit).register()
382336
383337 /* *
384338 * Creates a [LongSetting] with the provided parameters and adds it to the [settings].
@@ -403,36 +357,7 @@ abstract class Configurable(
403357 description : String = "",
404358 unit : String = "",
405359 visibility : () -> Boolean = { true },
406- ) = LongSetting (name, defaultValue, range, step, description, visibility, unit).also {
407- settings.add(it)
408- }
409-
410- /* *
411- * Creates a [ShortSetting] with the provided parameters and adds it to the [settings].
412- *
413- * The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
414- *
415- * @param name The unique identifier for the setting.
416- * @param defaultValue The default [Short] value of the setting.
417- * @param range The range within which the setting's value must fall.
418- * @param step The step to which the setting's value is rounded.
419- * @param description A brief explanation of the setting's purpose and behavior.
420- * @param visibility A lambda expression that determines the visibility status of the setting.
421- * @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
422- *
423- * @return The created [ShortSetting].
424- */
425- fun setting (
426- name : String ,
427- defaultValue : Short ,
428- range : ClosedRange <Short >,
429- step : Short = 1,
430- description : String = "",
431- unit : String = "",
432- visibility : () -> Boolean = { true },
433- ) = ShortSetting (name, defaultValue, range, step, description, visibility, unit).also {
434- settings.add(it)
435- }
360+ ) = LongSetting (name, defaultValue, range, step, description, visibility, unit).register()
436361
437362 /* *
438363 * Creates a [KeyBindSetting] with the provided parameters and adds it to the [settings].
@@ -449,9 +374,7 @@ abstract class Configurable(
449374 defaultValue : KeyCode ,
450375 description : String = "",
451376 visibility : () -> Boolean = { true },
452- ) = KeyBindSetting (name, defaultValue, description, visibility).also {
453- settings.add(it)
454- }
377+ ) = KeyBindSetting (name, defaultValue, description, visibility).register()
455378
456379 /* *
457380 * Creates a [ColorSetting] with the provided parameters and adds it to the [settings].
@@ -468,9 +391,7 @@ abstract class Configurable(
468391 defaultValue : Color ,
469392 description : String = "",
470393 visibility : () -> Boolean = { true },
471- ) = ColorSetting (name, defaultValue, description, visibility).also {
472- settings.add(it)
473- }
394+ ) = ColorSetting (name, defaultValue, description, visibility).register()
474395
475396 /* *
476397 * Creates a [BlockPosSetting] with the provided parameters and adds it to the [settings].
@@ -487,9 +408,7 @@ abstract class Configurable(
487408 defaultValue : BlockPos ,
488409 description : String = "",
489410 visibility : () -> Boolean = { true },
490- ) = BlockPosSetting (name, defaultValue, description, visibility).also {
491- settings.add(it)
492- }
411+ ) = BlockPosSetting (name, defaultValue, description, visibility).register()
493412
494413 /* *
495414 * Creates a [BlockSetting] with the provided parameters and adds it to the [settings].
@@ -506,7 +425,5 @@ abstract class Configurable(
506425 defaultValue : Block ,
507426 description : String = "",
508427 visibility : () -> Boolean = { true },
509- ) = BlockSetting (name, defaultValue, description, visibility).also {
510- settings.add(it)
511- }
428+ ) = BlockSetting (name, defaultValue, description, visibility).register()
512429}
0 commit comments