aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dev/isxander/yacl3/dsl/Util.kt
blob: 87337aac6f392e3ae991b1a7529e71e2c0ace3c2 (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
package dev.isxander.yacl3.dsl

import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

class RegisterableDelegateProvider<R>(
    private val registerFunction: (id: String) -> R,
    private val id: String?,
) {
    operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): ExistingDelegateProvider<R> {
        return ExistingDelegateProvider(registerFunction(id ?: property.name))
    }
}

class RegisterableActionDelegateProvider<Dsl, Return>(
    private val registerFunction: (String, Dsl.() -> Unit) -> Return,
    private val action: Dsl.() -> Unit,
    private val name: String?
) {
    operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): ExistingDelegateProvider<Return> {
        return ExistingDelegateProvider(registerFunction(name ?: property.name, action))
    }
}

class ExistingDelegateProvider<Return>(
    private val delegate: Return
) : ReadOnlyProperty<Any?, Return> {
    override fun getValue(thisRef: Any?, property: KProperty<*>): Return {
        return delegate
    }
}