From 3d05249dadaa2f3b2b58d7aecbe5518b09d35d78 Mon Sep 17 00:00:00 2001 From: thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> Date: Sun, 16 Aug 2020 19:27:23 -0700 Subject: Update Kotlin for Forge 1.4.0 --- .../thedarkcolour/kotlinforforge/forge/Forge.kt | 27 +++++-- .../kotlinforforge/forge/KDeferredRegister.kt | 84 ++++++++++++++++++++++ src/main/resources/META-INF/mods.toml | 4 +- 3 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/thedarkcolour/kotlinforforge/forge/KDeferredRegister.kt (limited to 'src') diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt index 37cc207..01f9e90 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt @@ -18,6 +18,7 @@ import thedarkcolour.kotlinforforge.eventbus.KotlinEventBusWrapper import java.util.* import java.util.function.Consumer import java.util.function.Predicate +import java.util.function.Supplier import kotlin.collections.HashMap import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty @@ -49,7 +50,7 @@ public val FORGE_BUS: KotlinEventBusWrapper = KotlinEventBusWrapper(MinecraftFor * * Examples: * @see net.minecraftforge.fml.event.lifecycle.InterModProcessEvent - * @see net.minecraftforge.event.AttachCapabilitiesEvent + * @see net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent * @see net.minecraftforge.event.RegistryEvent */ public val MOD_BUS: KotlinEventBus @@ -158,19 +159,26 @@ public fun sidedDelegate(clientValue: () -> T, serverValue: () -> T): ReadOn /** @since 1.2.2 * Creates a new [ObjectHolderDelegate] with the specified [registryName]. * - * This delegate serves as an alternative to using the - * `@ObjectHolder` annotation, making it easier to use in Kotlin. + * Provides ObjectHolders as property delegates instead of magic annotations. */ public inline fun > objectHolder(registryName: ResourceLocation): ReadOnlyProperty { return ObjectHolderDelegate(registryName, ObjectHolderDelegate.getRegistry(T::class.java)) } +/** @since 1.2.2 + * Creates a new with the specified [namespace] and [registryName]. + * + * Provides ObjectHolders as property delegates instead of magic annotations. + */ +public inline fun > objectHolder(namespace: String, registryName: String): ReadOnlyProperty { + return ObjectHolderDelegate(ResourceLocation(namespace, registryName), ObjectHolderDelegate.getRegistry(T::class.java)) +} + /** @since 1.2.2 * Creates a new [ObjectHolderDelegate]. * This overload uses a string instead of a ResourceLocation. * - * This delegate serves as an alternative to using the - * `@ObjectHolder` annotation, making it easier to use in Kotlin. + * Provides ObjectHolders as property delegates instead of magic annotations. */ public inline fun > objectHolder(registryName: String): ReadOnlyProperty { return ObjectHolderDelegate( @@ -222,6 +230,9 @@ private class SidedDelegate(private val clientValue: () -> T, private val ser * This class has proper implementations of * [copy], [hashCode], [equals], and [toString]. * + * @since 1.4.0 + * [ObjectHolderDelegate] can now be used with the [KDeferredRegister]. + * * @param T the type of object this delegates to * @property registryName the registry name of the object this delegate references * @property registry the registry the object of this delegate is in @@ -230,7 +241,7 @@ private class SidedDelegate(private val clientValue: () -> T, private val ser public data class ObjectHolderDelegate>( private val registryName: ResourceLocation, private val registry: IForgeRegistry<*>, -) : ReadOnlyProperty, Consumer> { +) : ReadOnlyProperty, Consumer>, Supplier { /** * Should be initialized by [accept]. If you don't register * a value for [registryName] during the appropriate registry event @@ -242,6 +253,10 @@ public data class ObjectHolderDelegate>( ObjectHolderRegistry.addHandler(this) } + override fun get(): T { + return value + } + override fun getValue(thisRef: Any?, property: KProperty<*>): T { return value } diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/forge/KDeferredRegister.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/KDeferredRegister.kt new file mode 100644 index 0000000..f556d0f --- /dev/null +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/KDeferredRegister.kt @@ -0,0 +1,84 @@ +package thedarkcolour.kotlinforforge.forge + +import net.minecraft.util.ResourceLocation +import net.minecraftforge.event.RegistryEvent +import net.minecraftforge.eventbus.api.IEventBus +import net.minecraftforge.fml.RegistryObject +import net.minecraftforge.registries.DeferredRegister +import net.minecraftforge.registries.IForgeRegistry +import net.minecraftforge.registries.IForgeRegistryEntry +import net.minecraftforge.registries.RegistryManager +import thedarkcolour.kotlinforforge.eventbus.KotlinEventBus +import kotlin.properties.ReadOnlyProperty + +/** + * Alternative version of [DeferredRegister] that creates + * [ObjectHolderDelegate] instances instead of [RegistryObject]. + */ +public class KDeferredRegister>( + public val registry: IForgeRegistry, + public val modid: String, +) { + /** + * A map of all registry objects and their value suppliers. + */ + private val entries = HashMap, () -> V>() + + /** + * Alternative constructor that uses a class instead of a registry. + */ + public constructor(registryClass: Class, modid: String) : this(RegistryManager.ACTIVE.getRegistry(registryClass), modid) + + /** + * Registers this deferred register to the `KotlinEventBus`. + */ + public fun register(bus: KotlinEventBus) { + bus.addGenericListener(registry.registrySuperType, ::addEntries) + } + + /** + * Registers this deferred register to the `IEventBus`. + */ + @Deprecated("Use a KotlinEventBus. Forge's EventBus does not support function references for event listeners.") + public fun register(bus: IEventBus) { + // function references are not supported by Forge's eventbus + bus.addGenericListener(registry.registrySuperType) { event: RegistryEvent.Register -> + addEntries(event) + } + } + + /** + * Adds a registry object to this deferred registry. + * + * @param name the path of the new registry object's registry name with namespace [modid] + * @param supplier the function to initialize the value of the new registry object with and + * to reset the value when forge adds reloadable registries. + * + * @return A new [ObjectHolderDelegate] with the given registry name and value + */ + public fun register(name: String, supplier: () -> T): ReadOnlyProperty { + val key = ResourceLocation(modid, name) + val a = ObjectHolderDelegate(key, registry) + + entries[a] = { supplier().setRegistryName(key) } + + return a + } + + public fun getEntries(): Set> { + return entries.keys + } + + /** + * Adds all entries in this registry to the corresponding game registries. + */ + private fun addEntries(event: RegistryEvent.Register) { + val registry = event.registry + + for ((objectHolder, supplier) in entries) { + registry.register(supplier()) + // pass true to always update the entry + objectHolder.accept { true } + } + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 6de045b..47e863b 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -1,5 +1,5 @@ modLoader="kotlinforforge" # IModLanguageProvider -loaderVersion="[1.3,)" # IModLanguageProvider version +loaderVersion="[1.4,)" # IModLanguageProvider version issueTrackerURL="https://github.com/thedarkcolour/KotlinForForge/issues" # Issues page @@ -31,6 +31,6 @@ Kotlin for Forge. Allows mods to use the Kotlin programming language. [[mods]] #mandatory displayName="Kotlin for Forge" # Name of mod modId="kotlinforforge" # Modid -version="1.3.1" # Version of kotlinforforge +version="1.4.0" # Version of kotlinforforge authors="TheDarkColour" # Author credits="Herobrine knows all." # Credits \ No newline at end of file -- cgit