aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt2
-rw-r--r--src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtDeferredRegister.kt34
-rw-r--r--src/main/kotlin/thedarkcolour/kotlinforforge/registry/KtRegistryObject.kt46
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