Skip to content

Commit b7d2bee

Browse files
committed
KDoc: Settings
1 parent cb9a092 commit b7d2bee

File tree

7 files changed

+302
-3
lines changed

7 files changed

+302
-3
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,49 @@ import com.lambda.util.Nameable
88
import kotlin.properties.Delegates
99
import kotlin.reflect.KProperty
1010

11+
/**
12+
* Represents a setting with a [defaultValue], [visibility] condition, and [description].
13+
* This setting is serializable ([Jsonable]) and has a [name].
14+
*
15+
* When the [value] is modified, all registered [listeners] are notified.
16+
* The [visibility] of the setting can be checked with the [isVisible] property.
17+
* The setting can be [reset] to its [defaultValue].
18+
*
19+
* Simple Usage:
20+
* ```kotlin
21+
* // this uses the delegate (by) association to access the setting value in the code directly.
22+
* val mode by setting("Mode", Modes.FREEZE, { page == Page.CUSTOM }, "The mode of the module.")
23+
*
24+
* init {
25+
* listener<TickEvent.Pre> {
26+
* LOG.info("Mode: $mode") // direct access of the value
27+
* }
28+
* }
29+
* ```
30+
*
31+
* Advanced usage with listeners:
32+
* ```kotlin
33+
* // notice how this does not use the delegate (by) association, to access the setting object to register listeners.
34+
* val mode = setting("Mode", Modes.FREEZE, { page == Page.CUSTOM }, "The mode of the module.")
35+
*
36+
* init {
37+
* mode.listener { from, to ->
38+
* // Do something when the mode changes in a safe context
39+
* }
40+
* mode.unsafeListener { from, to ->
41+
* // Do something when the mode changes in an unsafe context
42+
* }
43+
*
44+
* listener<TickEvent.Pre> {
45+
* LOG.info("Mode: ${mode.value}") // indirect access of the value
46+
* }
47+
* }
48+
* ```
49+
*
50+
* @property defaultValue The default value of the setting.
51+
* @property visibility A function that determines whether the setting is visible.
52+
* @property description A description of the setting.
53+
*/
1154
abstract class AbstractSetting<T : Any>(
1255
private val defaultValue: T,
1356
val visibility: () -> Boolean,
@@ -20,7 +63,7 @@ abstract class AbstractSetting<T : Any>(
2063
listeners.forEach { it(from, to) }
2164
}
2265

23-
val isVisible get() = visibility()
66+
private val isVisible get() = visibility()
2467
val isModified get() = value != defaultValue
2568

2669
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
@@ -47,7 +90,7 @@ abstract class AbstractSetting<T : Any>(
4790
listeners.add(block)
4891
}
4992

50-
fun reset() {
93+
private fun reset() {
5194
value = defaultValue
5295
}
5396
}

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

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ import com.lambda.util.KeyCode
1717
import com.lambda.util.Nameable
1818
import net.minecraft.block.Block
1919
import net.minecraft.util.math.BlockPos
20+
import com.lambda.Lambda
2021

2122
/**
22-
* Holds a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
23+
* Represents a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
24+
* The settings are managed by this [Configurable] and are saved and loaded as part of the [Configuration].
25+
*
26+
* This class also provides a series of helper methods ([setting]) for creating different types of settings.
27+
*
28+
* @property settings A set of [AbstractSetting]s that this configurable manages.
2329
*/
2430
abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
2531
val settings = mutableSetOf<AbstractSetting<*>>()
@@ -44,6 +50,20 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
4450
}
4551
}
4652

53+
/**
54+
* Creates a [BooleanSetting] with the provided parameters and adds it to the [settings].
55+
*
56+
* @param name The unique identifier for the setting.
57+
* @param defaultValue The default [Boolean] value of the setting.
58+
* @param visibility A lambda expression that determines the visibility status of the setting.
59+
* @param description A brief explanation of the setting's purpose and behavior.
60+
*
61+
* ```kotlin
62+
* private val foo by setting("Foo", true)
63+
* ```
64+
*
65+
* @return The created [BooleanSetting].
66+
*/
4767
fun setting(
4868
name: String,
4969
defaultValue: Boolean,
@@ -53,6 +73,22 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
5373
settings.add(it)
5474
}
5575

76+
/**
77+
* Creates an [EnumSetting] with the provided parameters and adds it to the [settings].
78+
*
79+
* @param name The unique identifier for the setting.
80+
* @param defaultValue The default [Enum] value of the setting.
81+
* @param visibility A lambda expression that determines the visibility status of the setting.
82+
* @param description A brief explanation of the setting's purpose and behavior.
83+
*
84+
* ```kotlin
85+
* enum class Foo { A, B, C }
86+
* private val foo by setting("Foo", Foo.A)
87+
* ```
88+
*
89+
*
90+
* @return The created [EnumSetting].
91+
*/
5692
inline fun <reified T : Enum<T>> setting(
5793
name: String,
5894
defaultValue: T,
@@ -62,6 +98,20 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
6298
settings.add(it)
6399
}
64100

101+
/**
102+
* Creates a [StringSetting] with the provided parameters and adds it to the [settings].
103+
*
104+
* @param name The unique identifier for the setting.
105+
* @param defaultValue The default [String] value of the setting.
106+
* @param visibility A lambda expression that determines the visibility status of the setting.
107+
* @param description A brief explanation of the setting's purpose and behavior.
108+
*
109+
* ```kotlin
110+
* private val foo by setting("Foo", "bar")
111+
* ```
112+
*
113+
* @return The created [StringSetting].
114+
*/
65115
fun setting(
66116
name: String,
67117
defaultValue: String,
@@ -71,6 +121,23 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
71121
settings.add(it)
72122
}
73123

124+
/**
125+
* Constructs a [ListSetting] instance with the specified parameters and appends it to the [settings] collection.
126+
*
127+
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
128+
*
129+
* @param name The unique identifier for the setting.
130+
* @param defaultValue The default [List] value of type [T] for the setting.
131+
* @param visibility A lambda expression that determines the visibility status of the setting.
132+
* @param description A brief explanation of the setting's purpose and behavior.
133+
*
134+
* ```kotlin
135+
* // the parameter type is inferred from the defaultValue
136+
* private val foo by setting("Foo", listOf("bar", "baz"))
137+
* ```
138+
*
139+
* @return The created [ListSetting].
140+
*/
74141
inline fun <reified T : Any> setting(
75142
name: String,
76143
defaultValue: List<T>,
@@ -80,6 +147,23 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
80147
settings.add(it)
81148
}
82149

150+
/**
151+
* Constructs a [MapSetting] instance with the specified parameters and appends it to the [settings] collection.
152+
*
153+
* The type parameter [K] and [V] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
154+
*
155+
* @param name The unique identifier for the setting.
156+
* @param defaultValue The default [Map] value of type [K] and [V] for the setting.
157+
* @param visibility A lambda expression that determines the visibility status of the setting.
158+
* @param description A brief explanation of the setting's purpose and behavior.
159+
*
160+
* ```kotlin
161+
* // the parameter types are inferred from the defaultValue
162+
* private val foo by setting("Foo", mapOf("bar" to 1, "baz" to 2))
163+
* ```
164+
*
165+
* @return The created [MapSetting].
166+
*/
83167
inline fun <reified K : Any, V : Any> setting(
84168
name: String,
85169
defaultValue: Map<K, V>,
@@ -89,6 +173,23 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
89173
settings.add(it)
90174
}
91175

176+
/**
177+
* Constructs a [SetSetting] instance with the specified parameters and appends it to the [settings] collection.
178+
*
179+
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
180+
*
181+
* @param name The unique identifier for the setting.
182+
* @param defaultValue The default [Set] value of type [T] for the setting.
183+
* @param visibility A lambda expression that determines the visibility status of the setting.
184+
* @param description A brief explanation of the setting's purpose and behavior.
185+
*
186+
* ```kotlin
187+
* // the parameter type is inferred from the defaultValue
188+
* private val foo by setting("Foo", setOf("bar", "baz"))
189+
* ```
190+
*
191+
* @return The created [SetSetting].
192+
*/
92193
inline fun <reified T : Any> setting(
93194
name: String,
94195
defaultValue: Set<T>,
@@ -98,6 +199,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
98199
settings.add(it)
99200
}
100201

202+
/**
203+
* Creates a [ByteSetting] with the provided parameters and adds it to the [settings].
204+
*
205+
* @param name The unique identifier for the setting.
206+
* @param defaultValue The default [Byte] value of the setting.
207+
* @param range The range within which the setting's value must fall.
208+
* @param step The step to which the setting's value is rounded.
209+
* @param visibility A lambda expression that determines the visibility status of the setting.
210+
* @param description A brief explanation of the setting's purpose and behavior.
211+
*
212+
* @return The created [ByteSetting].
213+
*/
101214
fun setting(
102215
name: String,
103216
defaultValue: Byte,
@@ -109,6 +222,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
109222
settings.add(it)
110223
}
111224

225+
/**
226+
* Creates a [DoubleSetting] with the provided parameters and adds it to the [settings].
227+
*
228+
* @param name The unique identifier for the setting.
229+
* @param defaultValue The default [Double] value of the setting.
230+
* @param range The range within which the setting's value must fall.
231+
* @param step The step to which the setting's value is rounded.
232+
* @param visibility A lambda expression that determines the visibility status of the setting.
233+
* @param description A brief explanation of the setting's purpose and behavior.
234+
*
235+
* @return The created [DoubleSetting].
236+
*/
112237
fun setting(
113238
name: String,
114239
defaultValue: Double,
@@ -120,6 +245,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
120245
settings.add(it)
121246
}
122247

248+
/**
249+
* Creates a [FloatSetting] with the provided parameters and adds it to the [settings].
250+
*
251+
* @param name The unique identifier for the setting.
252+
* @param defaultValue The default [Float] value of the setting.
253+
* @param range The range within which the setting's value must fall.
254+
* @param step The step to which the setting's value is rounded.
255+
* @param visibility A lambda expression that determines the visibility status of the setting.
256+
* @param description A brief explanation of the setting's purpose and behavior.
257+
*
258+
* @return The created [FloatSetting].
259+
*/
123260
fun setting(
124261
name: String,
125262
defaultValue: Float,
@@ -131,6 +268,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
131268
settings.add(it)
132269
}
133270

271+
/**
272+
* Creates an [IntegerSetting] with the provided parameters and adds it to the [settings].
273+
*
274+
* @param name The unique identifier for the setting.
275+
* @param defaultValue The default [Int] value of the setting.
276+
* @param range The range within which the setting's value must fall.
277+
* @param step The step to which the setting's value is rounded.
278+
* @param visibility A lambda expression that determines the visibility status of the setting.
279+
* @param description A brief explanation of the setting's purpose and behavior.
280+
*
281+
* @return The created [IntegerSetting].
282+
*/
134283
fun setting(
135284
name: String,
136285
defaultValue: Int,
@@ -142,6 +291,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
142291
settings.add(it)
143292
}
144293

294+
/**
295+
* Creates a [LongSetting] with the provided parameters and adds it to the [settings].
296+
*
297+
* @param name The unique identifier for the setting.
298+
* @param defaultValue The default [Long] value of the setting.
299+
* @param range The range within which the setting's value must fall.
300+
* @param step The step to which the setting's value is rounded.
301+
* @param visibility A lambda expression that determines the visibility status of the setting.
302+
* @param description A brief explanation of the setting's purpose and behavior.
303+
*
304+
* @return The created [LongSetting].
305+
*/
145306
fun setting(
146307
name: String,
147308
defaultValue: Long,
@@ -153,6 +314,18 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
153314
settings.add(it)
154315
}
155316

317+
/**
318+
* Creates a [ShortSetting] with the provided parameters and adds it to the [settings].
319+
*
320+
* @param name The unique identifier for the setting.
321+
* @param defaultValue The default [Short] value of the setting.
322+
* @param range The range within which the setting's value must fall.
323+
* @param step The step to which the setting's value is rounded.
324+
* @param visibility A lambda expression that determines the visibility status of the setting.
325+
* @param description A brief explanation of the setting's purpose and behavior.
326+
*
327+
* @return The created [ShortSetting].
328+
*/
156329
fun setting(
157330
name: String,
158331
defaultValue: Short,
@@ -164,6 +337,16 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
164337
settings.add(it)
165338
}
166339

340+
/**
341+
* Creates a [KeyBindSetting] with the provided parameters and adds it to the [settings].
342+
*
343+
* @param name The unique identifier for the setting.
344+
* @param defaultValue The default [KeyCode] value of the setting.
345+
* @param visibility A lambda expression that determines the visibility status of the setting.
346+
* @param description A brief explanation of the setting's purpose and behavior.
347+
*
348+
* @return The created [KeyBindSetting].
349+
*/
167350
fun setting(
168351
name: String,
169352
defaultValue: KeyCode,
@@ -173,6 +356,16 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
173356
settings.add(it)
174357
}
175358

359+
/**
360+
* Creates a [BlockPosSetting] with the provided parameters and adds it to the [settings].
361+
*
362+
* @param name The unique identifier for the setting.
363+
* @param defaultValue The default [BlockPos] value of the setting.
364+
* @param visibility A lambda expression that determines the visibility status of the setting.
365+
* @param description A brief explanation of the setting's purpose and behavior.
366+
*
367+
* @return The created [BlockPosSetting].
368+
*/
176369
fun setting(
177370
name: String,
178371
defaultValue: BlockPos,
@@ -182,6 +375,16 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
182375
settings.add(it)
183376
}
184377

378+
/**
379+
* Creates a [BlockSetting] with the provided parameters and adds it to the [settings].
380+
*
381+
* @param name The unique identifier for the setting.
382+
* @param defaultValue The default [Block] value of the setting.
383+
* @param visibility A lambda expression that determines the visibility status of the setting.
384+
* @param description A brief explanation of the setting's purpose and behavior.
385+
*
386+
* @return The created [BlockSetting].
387+
*/
185388
fun setting(
186389
name: String,
187390
defaultValue: Block,

0 commit comments

Comments
 (0)