-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathConfigurable.kt
More file actions
472 lines (446 loc) · 19.1 KB
/
Configurable.kt
File metadata and controls
472 lines (446 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright 2024 Lambda
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lambda.config
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.lambda.Lambda
import com.lambda.Lambda.LOG
import com.lambda.config.settings.CharSetting
import com.lambda.config.settings.FunctionSetting
import com.lambda.config.settings.StringSetting
import com.lambda.config.settings.collections.ListSetting
import com.lambda.config.settings.collections.MapSetting
import com.lambda.config.settings.collections.SetSetting
import com.lambda.config.settings.comparable.BooleanSetting
import com.lambda.config.settings.comparable.EnumSetting
import com.lambda.config.settings.complex.BlockPosSetting
import com.lambda.config.settings.complex.BlockSetting
import com.lambda.config.settings.complex.ColorSetting
import com.lambda.config.settings.complex.KeyBindSetting
import com.lambda.config.settings.complex.Vec3dSetting
import com.lambda.config.settings.numeric.DoubleSetting
import com.lambda.config.settings.numeric.FloatSetting
import com.lambda.config.settings.numeric.IntegerSetting
import com.lambda.config.settings.numeric.LongSetting
import com.lambda.util.Communication.logError
import com.lambda.util.KeyCode
import com.lambda.util.Nameable
import net.minecraft.block.Block
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import java.awt.Color
/**
* Represents a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
* The settings are managed by this [Configurable] and are saved and loaded as part of the [Configuration].
*
* This class also provides a series of helper methods ([setting]) for creating different types of settings.
*
* @property settings A set of [AbstractSetting]s that this configurable manages.
*/
abstract class Configurable(
private val configuration: Configuration,
) : Jsonable, Nameable {
val settings = mutableSetOf<AbstractSetting<*>>()
init {
registerConfigurable()
}
private fun registerConfigurable() = configuration.configurables.add(this)
inline fun <reified T : AbstractSetting<*>> T.register(): T {
check(settings.add(this)) { "Setting with name $name already exists for configurable: ${this@Configurable.name}" }
return this
}
override fun toJson() =
JsonObject().apply {
settings.forEach { setting ->
try {
add(setting.name, setting.toJson())
} catch (e: Exception) {
logError("Failed to serialize $setting in ${this::class.simpleName}", e)
}
}
}
override fun loadFromJson(serialized: JsonElement) {
serialized.asJsonObject.entrySet().forEach { (name, value) ->
settings.find { it.name == name }?.loadFromJson(value)
?: LOG.warn("No saved setting found for $name with $value in ${this::class.simpleName}")
}
}
/**
* Creates a [BooleanSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Boolean] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* private val foo by setting("Foo", true)
* ```
*
* @return The created [BooleanSetting].
*/
fun setting(
name: String,
defaultValue: Boolean,
description: String = "",
visibility: () -> Boolean = { true },
) = BooleanSetting(name, defaultValue, description, visibility).register()
/**
* Creates an [EnumSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Enum] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* enum class Foo { A, B, C }
* private val foo by setting("Foo", Foo.A)
* ```
*
*
* @return The created [EnumSetting].
*/
inline fun <reified T : Enum<T>> setting(
name: String,
defaultValue: T,
description: String = "",
noinline visibility: () -> Boolean = { true },
) = EnumSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [CharSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Char] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [CharSetting].
*/
fun setting(
name: String,
defaultValue: Char,
description: String = "",
visibility: () -> Boolean = { true },
) = CharSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [StringSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [String] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* private val foo by setting("Foo", "bar")
* ```
*
* @return The created [StringSetting].
*/
fun setting(
name: String,
defaultValue: String,
description: String = "",
visibility: () -> Boolean = { true },
) = StringSetting(name, defaultValue, description, visibility).register()
/**
* Constructs a [ListSetting] instance with the specified parameters and appends it to the [settings] collection.
*
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [List] value of type [T] for the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* // the parameter type is inferred from the defaultValue
* private val foo by setting("Foo", arrayListOf("bar", "baz"))
* ```
*
* @return The created [ListSetting].
*/
inline fun <reified T : Any> setting(
name: String,
defaultValue: List<T>,
description: String = "",
noinline visibility: () -> Boolean = { true },
) = ListSetting(
name,
defaultValue.toMutableList(),
TypeToken.getParameterized(MutableList::class.java, T::class.java).type,
description,
visibility,
).register()
/**
* Constructs a [MapSetting] instance with the specified parameters and appends it to the [settings] collection.
*
* The type parameter [K] and [V] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Map] value of type [K] and [V] for the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* // the parameter types are inferred from the defaultValue
* private val foo by setting("Foo", mapOf("bar" to 1, "baz" to 2))
* ```
*
* @return The created [MapSetting].
*/
inline fun <reified K : Any, reified V : Any> setting(
name: String,
defaultValue: Map<K, V>,
description: String = "",
noinline visibility: () -> Boolean = { true },
) = MapSetting(
name,
defaultValue.toMutableMap(),
TypeToken.getParameterized(MutableMap::class.java, K::class.java, V::class.java).type,
description,
visibility
).register()
/**
* Constructs a [SetSetting] instance with the specified parameters and appends it to the [settings] collection.
*
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Set] value of type [T] for the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* ```kotlin
* // the parameter type is inferred from the defaultValue
* private val foo by setting("Foo", setOf("bar", "baz"))
* ```
*
* @return The created [SetSetting].
*/
inline fun <reified T : Any> setting(
name: String,
defaultValue: Set<T>,
description: String = "",
noinline visibility: () -> Boolean = { true },
) = SetSetting(
name,
defaultValue.toMutableSet(),
TypeToken.getParameterized(MutableSet::class.java, T::class.java).type,
description,
visibility,
).register()
/**
* Creates a [DoubleSetting] with the provided parameters and adds it to the [settings].
*
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Double] value of the setting.
* @param range The range within which the setting's value must fall.
* @param step The step to which the setting's value is rounded.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
* @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
*
* @return The created [DoubleSetting].
*/
fun setting(
name: String,
defaultValue: Double,
range: ClosedRange<Double>,
step: Double = 1.0,
description: String = "",
unit: String = "",
visibility: () -> Boolean = { true },
) = DoubleSetting(name, defaultValue, range, step, description, unit, visibility).register()
/**
* Creates a [FloatSetting] with the provided parameters and adds it to the [settings].
*
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Float] value of the setting.
* @param range The range within which the setting's value must fall.
* @param step The step to which the setting's value is rounded.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
* @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
*
* @return The created [FloatSetting].
*/
fun setting(
name: String,
defaultValue: Float,
range: ClosedRange<Float>,
step: Float = 1f,
description: String = "",
unit: String = "",
visibility: () -> Boolean = { true },
) = FloatSetting(name, defaultValue, range, step, description, unit, visibility).register()
/**
* Creates an [IntegerSetting] with the provided parameters and adds it to the [settings].
*
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Int] value of the setting.
* @param range The range within which the setting's value must fall.
* @param step The step to which the setting's value is rounded.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
* @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
*
* @return The created [IntegerSetting].
*/
fun setting(
name: String,
defaultValue: Int,
range: ClosedRange<Int>,
step: Int = 1,
description: String = "",
unit: String = "",
visibility: () -> Boolean = { true },
) = IntegerSetting(name, defaultValue, range, step, description, unit, visibility).register()
/**
* Creates a [LongSetting] with the provided parameters and adds it to the [settings].
*
* The value of the setting is coerced into the specified [range] and rounded to the nearest [step].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Long] value of the setting.
* @param range The range within which the setting's value must fall.
* @param step The step to which the setting's value is rounded.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
* @param unit The unit of the setting. E.g. "°C", "m/s", "ms", "ticks", etc.
*
* @return The created [LongSetting].
*/
fun setting(
name: String,
defaultValue: Long,
range: ClosedRange<Long>,
step: Long = 1,
description: String = "",
unit: String = "",
visibility: () -> Boolean = { true },
) = LongSetting(name, defaultValue, range, step, description, unit, visibility).register()
/**
* Creates a [KeyBindSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [KeyCode] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [KeyBindSetting].
*/
fun setting(
name: String,
defaultValue: KeyCode,
description: String = "",
visibility: () -> Boolean = { true },
) = KeyBindSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [ColorSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Color] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [ColorSetting].
*/
fun setting(
name: String,
defaultValue: Color,
description: String = "",
visibility: () -> Boolean = { true },
) = ColorSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [Vec3dSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Vec3d] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [Vec3dSetting].
*/
fun setting(
name: String,
defaultValue: Vec3d,
description: String = "",
visibility: () -> Boolean = { true },
) = Vec3dSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [BlockPosSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [BlockPos.Mutable] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [BlockPosSetting].
*/
fun setting(
name: String,
defaultValue: BlockPos.Mutable,
description: String = "",
visibility: () -> Boolean = { true },
) = BlockPosSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [BlockPosSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [BlockPos] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [BlockPosSetting].
*/
fun setting(
name: String,
defaultValue: BlockPos,
description: String = "",
visibility: () -> Boolean = { true },
) = BlockPosSetting(name, defaultValue, description, visibility).register()
/**
* Creates a [BlockSetting] with the provided parameters and adds it to the [settings].
*
* @param name The unique identifier for the setting.
* @param defaultValue The default [Block] value of the setting.
* @param description A brief explanation of the setting's purpose and behavior.
* @param visibility A lambda expression that determines the visibility status of the setting.
*
* @return The created [BlockSetting].
*/
fun setting(
name: String,
defaultValue: Block,
description: String = "",
visibility: () -> Boolean = { true },
) = BlockSetting(name, defaultValue, description, visibility).register()
fun setting(
name: String,
defaultValue: () -> Unit,
description: String = "",
visibility: () -> Boolean = { true }
) = FunctionSetting(name, defaultValue, description, visibility).register()
}