diff options
author | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2019-11-09 11:12:21 -0800 |
---|---|---|
committer | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2019-11-09 11:12:21 -0800 |
commit | 00d859187a8ca433028634e56bd30243ddaa69a4 (patch) | |
tree | 0582bdb9aa28e1117c72332535f3805ed8864712 /src/main/kotlin/thedarkcolour/kotlinforforge | |
parent | 7716f30272565f750182d58da753cd4cffb88761 (diff) | |
download | KotlinForForge-00d859187a8ca433028634e56bd30243ddaa69a4.tar.gz KotlinForForge-00d859187a8ca433028634e56bd30243ddaa69a4.tar.bz2 KotlinForForge-00d859187a8ca433028634e56bd30243ddaa69a4.zip |
Adding files
Diffstat (limited to 'src/main/kotlin/thedarkcolour/kotlinforforge')
4 files changed, 18 insertions, 17 deletions
diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt index b77a567..e36f9ce 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt @@ -1,6 +1,7 @@ package thedarkcolour.kotlinforforge import net.minecraftforge.api.distmarker.Dist +import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.Logging import net.minecraftforge.fml.ModContainer import net.minecraftforge.fml.common.Mod @@ -11,7 +12,6 @@ import org.objectweb.asm.Type import thedarkcolour.kotlinforforge.KotlinForForge.logger import java.util.* import java.util.stream.Collectors -import kotlin.reflect.full.companionObjectInstance /** * Handles [net.minecraftforge.fml.common.Mod.EventBusSubscriber] @@ -39,9 +39,9 @@ object AutoKotlinEventBusSubscriber { * You must define the [net.minecraftforge.eventbus.api.SubscribeEvent] methods inside the companion object. * Example Usage: * - * @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) * public class ExampleSubscriberClass { * + * @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) * companion object ExampleSubscriberCompanion { * @SubscribeEvent * public fun onItemRegistry(event: RegistryEvent.Register<Item>) { @@ -65,25 +65,20 @@ object AutoKotlinEventBusSubscriber { val modid = annotationData.annotationData.getOrDefault("modid", mod.modId) val busTargetHolder: ModAnnotation.EnumHolder = annotationData.annotationData.getOrDefault("bus", ModAnnotation.EnumHolder(null, "FORGE")) as ModAnnotation.EnumHolder val busTarget = Mod.EventBusSubscriber.Bus.valueOf(busTargetHolder.value) - val clazz = Class.forName(annotationData.classType.className, true, classLoader) - val ktClass = clazz.kotlin - val ktObject = ktClass.objectInstance + val ktObject = Class.forName(annotationData.classType.className, true, classLoader).kotlin.objectInstance if (ktObject != null && mod.modId == modid && sides.contains(FMLEnvironment.dist)) { try { logger.debug(Logging.LOADING, "Auto-subscribing kotlin object {} to {}", annotationData.classType.className, busTarget) - busTarget.bus().get().register(ktObject) + if (busTarget == Mod.EventBusSubscriber.Bus.MOD) { + // Gets the correct mod loading context + KotlinModLoadingContext.get().getEventBus().register(ktObject) + } else { + MinecraftForge.EVENT_BUS.register(ktObject) + } } catch (e: Throwable) { logger.fatal(Logging.LOADING, "Failed to load mod class {} for @EventBusSubscriber annotation", annotationData.classType, e) throw RuntimeException(e) } - } else if (ktClass.companionObjectInstance != null) { - try { - logger.debug(Logging.LOADING, "Auto-subscribing kotlin companion object from {} to {}", ktClass.simpleName, busTarget) - busTarget.bus().get().register(ktClass.companionObjectInstance) - } catch (e: Throwable) { - logger.fatal(Logging.LOADING, "Failed to load kotlin companion object {} for @EventBusSubscriber annotation", annotationData.classType, e) - throw RuntimeException(e) - } } } } diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt index e697f10..337f82a 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt @@ -1,11 +1,14 @@ package thedarkcolour.kotlinforforge import net.minecraftforge.fml.common.Mod +import net.minecraftforge.registries.DeferredRegister import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger /** - * Set 'modLoader' in mods.toml to "kotlinforforge". + * Set 'modLoader' in mods.toml to "kotlinforforge" and loaderVersion to "1". + * + * It is recommended that you use [DeferredRegister] instead of ObjectHolders. */ @Mod("kotlinforforge") object KotlinForForge { diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt index 26a65ea..28722cd 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt @@ -54,8 +54,11 @@ class KotlinModContainer(info: IModInfo, className: String, loader: ClassLoader, private fun fireEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) { val event = lifecycleEvent.getOrBuildEvent(this) - logger.debug(Logging.LOADING, "Firing event for modid {} : {}", this.getModId(), event) + logger.debug(Logging.LOADING, "Firing event for modid {} : {}", getModId(), event) + // Now actually fires the event try { + eventBus.post(event) + logger.debug(Logging.LOADING, "Fired event for modid {} : {}", getModId(), event) } catch (e: Throwable) { logger.error(Logging.LOADING,"An error occurred while dispatching event {} to {}", lifecycleEvent.fromStage(), modId) diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt index 4b7edc0..766b64d 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt @@ -10,7 +10,7 @@ class KotlinModLoadingContext internal constructor(private val container: Kotlin companion object { @JvmStatic - fun get() { + fun get(): KotlinModLoadingContext { return ModLoadingContext.get().extension() } } |