aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v3/KotlinExts.kt
blob: 2c5165646041cfd85b8d25595afcff8a765b3e0b (plain)
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
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()
}