diff --git a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt index 645a9d4..e2e60c3 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt @@ -8,8 +8,10 @@ import net.minecraftforge.fml.common.ModContainer import net.minecraftforge.fml.common.discovery.ASMDataTable import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData import net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.relauncher.Side import org.apache.logging.log4j.LogManager +import java.lang.reflect.Modifier import java.util.EnumSet import kotlin.reflect.full.companionObjectInstance @@ -45,17 +47,35 @@ object ForgelinAutomaticEventSubscriber { val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue val kotlinClass = subscriberClass.kotlin - val subscriberInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue + val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue - MinecraftForge.EVENT_BUS.unregister(subscriberClass) - MinecraftForge.EVENT_BUS.register(subscriberInstance) - LOGGER.debug("Registered @EventBusSubscriber object {}", subscriber.className) + if (!hasStaticEventHandlers(subscriberClass)) { + MinecraftForge.EVENT_BUS.unregister(subscriberClass) + LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className) + } + if (hasObjectEventHandlers(objectInstance)) { + MinecraftForge.EVENT_BUS.register(objectInstance) + LOGGER.debug("Registered @EventBusSubscriber object instance {}", subscriber.className) + } } catch (e: Throwable) { LOGGER.error("An error occurred trying to load an @EventBusSubscriber object {} for modid {}", mod.modId, e) + throw LoaderException(e) } } } + private fun hasObjectEventHandlers(objectInstance: Any): Boolean { + return objectInstance.javaClass.methods.any { + !Modifier.isStatic(it.modifiers) && it.isAnnotationPresent(SubscribeEvent::class.java) + } + } + + private fun hasStaticEventHandlers(clazz: Class<*>): Boolean { + return clazz.methods.any { + Modifier.isStatic(it.modifiers) && it.isAnnotationPresent(SubscribeEvent::class.java) + } + } + private fun parseModId(containedMods: MutableSet, subscriber: ASMData): String? { val parsedModId: String? = subscriber.annotationInfo["modid"] as? String if (parsedModId.isNullOrEmpty()) { diff --git a/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt b/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt index 6b7c308..e98cb51 100644 --- a/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt +++ b/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt @@ -1,5 +1,6 @@ package net.shadowfacts.forgelin +import net.minecraftforge.event.entity.player.PlayerInteractEvent import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.Mod.EventBusSubscriber @@ -15,5 +16,11 @@ object AutomaticKtSubscriberTest { fun onRightClickBlock(event: RightClickBlock) { println("Automatic KT subscriber: Right click ${event.pos}") } + + @JvmStatic + @SubscribeEvent + fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) { + println("Right click item") + } } }