diff --git a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt index 499b07b..41cce73 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt @@ -19,9 +19,10 @@ object ForgelinAutomaticEventSubscriber { private val DEFAULT_SUBSCRIPTION_SIDES = EnumSet.allOf(Side::class.java) private val LOGGER = LogManager.getLogger(ForgelinAutomaticEventSubscriber::class.java) - fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) { - LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", mod.modId) + private val unregistered = mutableSetOf>() + private val registered = mutableSetOf() + fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) { val modAnnotations = asm.getAnnotationsFor(mod) ?: return val containedMods = modAnnotations.get(Mod::class.java.name) @@ -30,36 +31,48 @@ object ForgelinAutomaticEventSubscriber { val loader = Loader.instance().modClassLoader - for (subscriber in subscribers) { - try { - val ownerModId = parseModId(containedMods, subscriber) - if (ownerModId.isNullOrEmpty()) { - LOGGER.warn("Could not determine owning mod for @EventBusSubscriber on {} for mod {}", subscriber.className, mod.modId) - continue - } - - if (mod.modId != ownerModId) { - LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", subscriber.className, mod.modId) - continue - } - - LOGGER.debug("Registering @EventBusSubscriber object for {} for mod {}", subscriber.className, mod.modId) - val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue - val kotlinClass = subscriberClass.kotlin - val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue + for (containedMod in containedMods) { + val containedModId = containedMod.annotationInfo["modid"] as String + if (containedMod.annotationInfo["modLanguageAdapter"] != KotlinAdapter::class.qualifiedName) { + LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it does not use KotlinAdapter", containedModId) + continue + } - 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) + LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", containedModId) + + for (subscriber in subscribers) { + try { + val ownerModId = parseModId(containedMods, subscriber) + if (ownerModId.isNullOrEmpty()) { + LOGGER.debug("Could not determine owning mod for @EventBusSubscriber on {} for mod {}", subscriber.className, mod.modId) + continue + } + + if (containedModId != ownerModId) { + LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", subscriber.className, containedModId) + continue + } + + val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue + val kotlinClass = subscriberClass.kotlin + val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue + + if (!hasStaticEventHandlers(subscriberClass) && subscriberClass !in unregistered) { + MinecraftForge.EVENT_BUS.unregister(subscriberClass) + unregistered += subscriberClass + LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className) + } + if (hasObjectEventHandlers(objectInstance) && objectInstance !in registered) { + MinecraftForge.EVENT_BUS.register(objectInstance) + registered += 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) } - } catch (e: Throwable) { - LOGGER.error("An error occurred trying to load an @EventBusSubscriber object {} for modid {}", mod.modId, e) - throw LoaderException(e) } } }