aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2024-06-11 23:13:49 +0100
committerisXander <xander@isxander.dev>2024-06-11 23:13:57 +0100
commit305718e163f91802a4bc1c1ed6540febb2ce204e (patch)
treed72fe8b95dab1ef89f67b13a19f8c06fdb582c28 /src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt
parent65b4f7ba8374bbaebc6a431f8347ffc3e8afdced (diff)
downloadYetAnotherConfigLib-305718e163f91802a4bc1c1ed6540febb2ce204e.tar.gz
YetAnotherConfigLib-305718e163f91802a4bc1c1ed6540febb2ce204e.tar.bz2
YetAnotherConfigLib-305718e163f91802a4bc1c1ed6540febb2ce204e.zip
codec config and rewritten kotlin dsl
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt')
-rw-r--r--src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt b/src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt
new file mode 100644
index 0000000..2c51656
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt
@@ -0,0 +1,53 @@
+package dev.isxander.yacl3.config.v3
+
+import com.mojang.serialization.Codec
+import dev.isxander.yacl3.dsl.OptionDsl
+import dev.isxander.yacl3.dsl.OptionRegistrar
+import org.jetbrains.annotations.ApiStatus
+import kotlin.properties.PropertyDelegateProvider
+import kotlin.properties.ReadOnlyProperty
+import kotlin.reflect.KProperty
+
+@ApiStatus.Experimental
+fun <T> EntryAddable.register(
+ default: T,
+ codec: Codec<T>
+) = PropertyDelegateProvider<EntryAddable, ReadOnlyProperty<EntryAddable, ConfigEntry<T>>> { thisRef, property ->
+ val entry = thisRef.register(property.name, default, codec)
+ ReadOnlyProperty { _, _ -> entry }
+}
+
+fun <T : CodecConfig<T>> EntryAddable.register(
+ fieldName: String? = null,
+ configInstance: T
+) = PropertyDelegateProvider<EntryAddable, T> { thisRef, property ->
+ thisRef.register(fieldName ?: property.name, configInstance)
+ configInstance
+}
+
+operator fun <T : CodecConfig<T>> T.getValue(thisRef: CodecConfig<*>?, property: KProperty<*>): T {
+ return this
+}
+
+@get:ApiStatus.Experimental
+@set:ApiStatus.Experimental
+var <T> ConfigEntry<T>.value: T
+ get() = this.get()
+ set(value) = this.set(value)
+
+@get:ApiStatus.Experimental
+val <T> ConfigEntry<T>.default: T
+ get() = this.defaultValue()
+
+@get:ApiStatus.Experimental
+val ConfigEntry<*>.fieldName: String
+ get() = this.fieldName()
+
+@ApiStatus.Experimental
+fun <T : Any> OptionRegistrar.register(
+ configEntry: ConfigEntry<T>,
+ block: OptionDsl<T>.() -> Unit
+) = register<T>(configEntry.fieldName) {
+ binding(configEntry.asBinding())
+ block()
+}