also make implement lazy

This commit is contained in:
DBotThePony 2022-11-03 21:43:56 +07:00
parent bdb2e85bcd
commit 745979d101
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -12,14 +12,21 @@ import java.util.function.Supplier
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
class RegistryDelegate<T>(key: String) : ReadOnlyProperty<Any, ForgeRegistry<T>>, Supplier<ForgeRegistry<T>> { class RegistryDelegate<T>(key: String) : ReadOnlyProperty<Any, ForgeRegistry<T>>, Supplier<ForgeRegistry<T>>, Lazy<ForgeRegistry<T>> {
private var value: Supplier<IForgeRegistry<T>?>? = null private var _value: Supplier<IForgeRegistry<T>?>? = null
override val value: ForgeRegistry<T>
get() = get()
override fun isInitialized(): Boolean {
return _value != null
}
val location = ResourceLocation(OverdriveThatMatters.MOD_ID, key) val location = ResourceLocation(OverdriveThatMatters.MOD_ID, key)
val key: ResourceKey<Registry<T>> = ResourceKey.createRegistryKey(location) val key: ResourceKey<Registry<T>> = ResourceKey.createRegistryKey(location)
override fun get(): ForgeRegistry<T> { override fun get(): ForgeRegistry<T> {
val supp = value ?: throw IllegalStateException("Tried to access uninitialized registry $location") val supp = _value ?: throw IllegalStateException("Tried to access uninitialized registry $location")
return supp.get() as ForgeRegistry<T>? ?: throw IllegalStateException("Accessing registry $location too early") return supp.get() as ForgeRegistry<T>? ?: throw IllegalStateException("Accessing registry $location too early")
} }
@ -28,11 +35,11 @@ class RegistryDelegate<T>(key: String) : ReadOnlyProperty<Any, ForgeRegistry<T>>
} }
fun build(event: NewRegistryEvent) { fun build(event: NewRegistryEvent) {
if (value != null) { if (_value != null) {
throw IllegalStateException("Already built registry $location!") throw IllegalStateException("Already built registry $location!")
} }
value = RegistryBuilder<T>().let { _value = RegistryBuilder<T>().let {
it.setName(location) it.setName(location)
// it.type = AndroidFeatureType::class.java // it.type = AndroidFeatureType::class.java
event.create(it) event.create(it)