Only attempt to register event handlers for mods that use KotlinAdapter

Closes #41
This commit is contained in:
Shadowfacts 2018-06-14 19:14:55 -04:00
parent 9e8935d4cf
commit 9809314759
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 40 additions and 27 deletions

View File

@ -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<Class<*>>()
private val registered = mutableSetOf<Any>()
fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) {
val modAnnotations = asm.getAnnotationsFor(mod) ?: return
val containedMods = modAnnotations.get(Mod::class.java.name)
@ -30,39 +31,51 @@ object ForgelinAutomaticEventSubscriber {
val loader = Loader.instance().modClassLoader
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
}
LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", containedModId)
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)
LOGGER.debug("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)
if (containedModId != ownerModId) {
LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", subscriber.className, containedModId)
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
if (!hasStaticEventHandlers(subscriberClass)) {
if (!hasStaticEventHandlers(subscriberClass) && subscriberClass !in unregistered) {
MinecraftForge.EVENT_BUS.unregister(subscriberClass)
unregistered += subscriberClass
LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className)
}
if (hasObjectEventHandlers(objectInstance)) {
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)
}
}
}
}
private fun hasObjectEventHandlers(objectInstance: Any): Boolean {
return objectInstance.javaClass.methods.any {