diff options
author | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2019-11-11 14:47:37 -0800 |
---|---|---|
committer | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2019-11-11 14:47:37 -0800 |
commit | bf11f1cb96cfeb98a21025459f24f235ff06cd42 (patch) | |
tree | 1f2ba123cb23ee764f83a509152892a580b74678 /src | |
parent | eff6ca9a8dc3d8844443cbba79a535d39d2a2405 (diff) | |
download | KotlinForForge-bf11f1cb96cfeb98a21025459f24f235ff06cd42.tar.gz KotlinForForge-bf11f1cb96cfeb98a21025459f24f235ff06cd42.tar.bz2 KotlinForForge-bf11f1cb96cfeb98a21025459f24f235ff06cd42.zip |
Adding files
Diffstat (limited to 'src')
3 files changed, 81 insertions, 1 deletions
diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt index e36f9ce..46cdc4d 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt @@ -17,7 +17,7 @@ import java.util.stream.Collectors * Handles [net.minecraftforge.fml.common.Mod.EventBusSubscriber] */ @Suppress("unused") -object AutoKotlinEventBusSubscriber { +internal object AutoKotlinEventBusSubscriber { private val EVENT_BUS_SUBSCRIBER: Type = Type.getType(Mod.EventBusSubscriber::class.java) /** diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtDeferredRegister.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtDeferredRegister.kt new file mode 100644 index 0000000..dedfb27 --- /dev/null +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtDeferredRegister.kt @@ -0,0 +1,34 @@ +package thedarkcolour.kotlinforforge.registry + +import net.minecraft.util.ResourceLocation +import net.minecraftforge.event.RegistryEvent +import net.minecraftforge.eventbus.api.IEventBus +import net.minecraftforge.registries.IForgeRegistry +import net.minecraftforge.registries.IForgeRegistryEntry +import java.util.* +import java.util.function.Supplier +import kotlin.collections.LinkedHashMap + +/** + * @see [net.minecraftforge.registries.DeferredRegister] for example use + */ +class KtDeferredRegister<T : IForgeRegistryEntry<T>>(private val registry: IForgeRegistry<T>, private val modid: String) { + private val entries: MutableMap<KtRegistryObject<T>, Supplier<out T>> = LinkedHashMap() + val entriesView: Set<KtRegistryObject<T>> = Collections.unmodifiableSet(entries.keys) + + fun register(name: String, supplier: Supplier<out T>): KtRegistryObject<T> { + val key = ResourceLocation(modid, name) + val obj = KtRegistryObject(key, registry) + require(entries.putIfAbsent(obj, Supplier { supplier.get().setRegistryName(key) }) == null) { "Duplicate registration $name" } + return obj + } + + fun register(bus: IEventBus) = bus.addListener(::addEntries) + + private fun addEntries(event: RegistryEvent.Register<T>) { + for (entry in entries.entries) { + registry.register(entry.value.get()) + entry.key.refresh(registry) + } + } +}
\ No newline at end of file diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtRegistryObject.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtRegistryObject.kt new file mode 100644 index 0000000..0b44705 --- /dev/null +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtRegistryObject.kt @@ -0,0 +1,46 @@ +package thedarkcolour.kotlinforforge.registry + +import net.minecraft.util.ResourceLocation +import net.minecraftforge.registries.IForgeRegistry +import net.minecraftforge.registries.IForgeRegistryEntry +import net.minecraftforge.registries.ObjectHolderRegistry +import net.minecraftforge.registries.RegistryManager +import java.util.function.Supplier + +/** + * An alternative to DeferredRegistry that enforces non-nullability. + */ +open class KtRegistryObject<T : IForgeRegistryEntry<T>>(private val name: ResourceLocation, private val registry: IForgeRegistry<T>) : Supplier<T> { + private lateinit var value: T + + constructor(name: String, registry: IForgeRegistry<T>) : this(ResourceLocation(name), registry) + + constructor(name: String, registryType: Supplier<Class<out T>>) : this(ResourceLocation(name), RegistryManager.ACTIVE.getRegistry(registryType.get()) as IForgeRegistry<T>) + + init { + ObjectHolderRegistry.addHandler { predicate -> + if (predicate.test(registry.registryName)) { + value = registry.getValue(name)!! + } + } + } + + override fun get(): T = value + + fun refresh(registry: IForgeRegistry<out T>) { + value = registry.getValue(name)!! + } + + override fun equals(other: Any?): Boolean { + return when (other) { + other === this -> true + other?.javaClass != javaClass -> false + (other as KtRegistryObject<*>).name != name -> false + else -> true + } + } + + override fun hashCode(): Int { + return name.hashCode() + } +}
\ No newline at end of file |