diff --git a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt index e2e60c3..499b07b 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/ForgelinAutomaticEventSubscriber.kt @@ -16,84 +16,84 @@ import java.util.EnumSet import kotlin.reflect.full.companionObjectInstance object ForgelinAutomaticEventSubscriber { - private val DEFAULT_SUBSCRIPTION_SIDES = EnumSet.allOf(Side::class.java) - private val LOGGER = LogManager.getLogger(ForgelinAutomaticEventSubscriber::class.java) + 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) + fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) { + LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", mod.modId) - val modAnnotations = asm.getAnnotationsFor(mod) ?: return + val modAnnotations = asm.getAnnotationsFor(mod) ?: return - val containedMods = modAnnotations.get(Mod::class.java.name) - val subscribers = modAnnotations.get(Mod.EventBusSubscriber::class.java.name) - .filter { parseTargetSides(it).contains(currentSide) } + val containedMods = modAnnotations.get(Mod::class.java.name) + val subscribers = modAnnotations.get(Mod.EventBusSubscriber::class.java.name) + .filter { parseTargetSides(it).contains(currentSide) } - val loader = Loader.instance().modClassLoader + 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 - } + 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 - } + 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) + 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 + val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue + val kotlinClass = subscriberClass.kotlin + val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: 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) - } - } catch (e: Throwable) { - LOGGER.error("An error occurred trying to load an @EventBusSubscriber object {} for modid {}", mod.modId, e) - throw LoaderException(e) - } - } - } + 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 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 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()) { - return parsedModId - } + private fun parseModId(containedMods: MutableSet, subscriber: ASMData): String? { + val parsedModId: String? = subscriber.annotationInfo["modid"] as? String + if (parsedModId.isNullOrEmpty()) { + return parsedModId + } - return ASMDataTable.getOwnerModID(containedMods, subscriber) - } + return ASMDataTable.getOwnerModID(containedMods, subscriber) + } - private fun parseTargetSides(subscriber: ASMData): EnumSet { - val parsedSides: List? = subscriber.annotationInfo["value"] as? List - if (parsedSides != null) { - val targetSides = EnumSet.noneOf(Side::class.java) - for (parsed in parsedSides) { - targetSides.add(Side.valueOf(parsed.value)) - } - return targetSides - } - return DEFAULT_SUBSCRIPTION_SIDES - } + private fun parseTargetSides(subscriber: ASMData): EnumSet { + val parsedSides: List? = subscriber.annotationInfo["value"] as? List + if (parsedSides != null) { + val targetSides = EnumSet.noneOf(Side::class.java) + for (parsed in parsedSides) { + targetSides.add(Side.valueOf(parsed.value)) + } + return targetSides + } + return DEFAULT_SUBSCRIPTION_SIDES + } } diff --git a/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt b/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt index e98cb51..6cfcc1d 100644 --- a/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt +++ b/src/test/kotlin/net/shadowfacts/forgelin/AutomaticKtSubscriberTest.kt @@ -8,19 +8,19 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @Mod(modid = AutomaticKtSubscriberTest.MODID, modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter") object AutomaticKtSubscriberTest { - const val MODID = "ktsubtest" + const val MODID = "ktsubtest" - @EventBusSubscriber(modid = AutomaticKtSubscriberTest.MODID) - object EventSubscriber { - @SubscribeEvent - fun onRightClickBlock(event: RightClickBlock) { - println("Automatic KT subscriber: Right click ${event.pos}") - } + @EventBusSubscriber(modid = AutomaticKtSubscriberTest.MODID) + object EventSubscriber { + @SubscribeEvent + fun onRightClickBlock(event: RightClickBlock) { + println("Automatic KT subscriber: Right click ${event.pos}") + } - @JvmStatic - @SubscribeEvent - fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) { - println("Right click item") - } - } + @JvmStatic + @SubscribeEvent + fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) { + println("Right click item") + } + } }