Only attempt to register event handlers for mods that use KotlinAdapter
Closes #41
This commit is contained in:
parent
9e8935d4cf
commit
9809314759
|
@ -19,9 +19,10 @@ object ForgelinAutomaticEventSubscriber {
|
||||||
private val DEFAULT_SUBSCRIPTION_SIDES = EnumSet.allOf(Side::class.java)
|
private val DEFAULT_SUBSCRIPTION_SIDES = EnumSet.allOf(Side::class.java)
|
||||||
private val LOGGER = LogManager.getLogger(ForgelinAutomaticEventSubscriber::class.java)
|
private val LOGGER = LogManager.getLogger(ForgelinAutomaticEventSubscriber::class.java)
|
||||||
|
|
||||||
fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) {
|
private val unregistered = mutableSetOf<Class<*>>()
|
||||||
LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", mod.modId)
|
private val registered = mutableSetOf<Any>()
|
||||||
|
|
||||||
|
fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) {
|
||||||
val modAnnotations = asm.getAnnotationsFor(mod) ?: return
|
val modAnnotations = asm.getAnnotationsFor(mod) ?: return
|
||||||
|
|
||||||
val containedMods = modAnnotations.get(Mod::class.java.name)
|
val containedMods = modAnnotations.get(Mod::class.java.name)
|
||||||
|
@ -30,39 +31,51 @@ object ForgelinAutomaticEventSubscriber {
|
||||||
|
|
||||||
val loader = Loader.instance().modClassLoader
|
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) {
|
for (subscriber in subscribers) {
|
||||||
try {
|
try {
|
||||||
val ownerModId = parseModId(containedMods, subscriber)
|
val ownerModId = parseModId(containedMods, subscriber)
|
||||||
if (ownerModId.isNullOrEmpty()) {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mod.modId != ownerModId) {
|
if (containedModId != ownerModId) {
|
||||||
LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", subscriber.className, mod.modId)
|
LOGGER.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", subscriber.className, containedModId)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.debug("Registering @EventBusSubscriber object for {} for mod {}", subscriber.className, mod.modId)
|
|
||||||
|
|
||||||
val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue
|
val subscriberClass = Class.forName(subscriber.className, false, loader) ?: continue
|
||||||
val kotlinClass = subscriberClass.kotlin
|
val kotlinClass = subscriberClass.kotlin
|
||||||
val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue
|
val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue
|
||||||
|
|
||||||
if (!hasStaticEventHandlers(subscriberClass)) {
|
if (!hasStaticEventHandlers(subscriberClass) && subscriberClass !in unregistered) {
|
||||||
MinecraftForge.EVENT_BUS.unregister(subscriberClass)
|
MinecraftForge.EVENT_BUS.unregister(subscriberClass)
|
||||||
|
unregistered += subscriberClass
|
||||||
LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className)
|
LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className)
|
||||||
}
|
}
|
||||||
if (hasObjectEventHandlers(objectInstance)) {
|
if (hasObjectEventHandlers(objectInstance) && objectInstance !in registered) {
|
||||||
MinecraftForge.EVENT_BUS.register(objectInstance)
|
MinecraftForge.EVENT_BUS.register(objectInstance)
|
||||||
|
registered += objectInstance
|
||||||
LOGGER.debug("Registered @EventBusSubscriber object instance {}", subscriber.className)
|
LOGGER.debug("Registered @EventBusSubscriber object instance {}", subscriber.className)
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
LOGGER.error("An error occurred trying to load an @EventBusSubscriber object {} for modid {}", mod.modId, e)
|
LOGGER.error("An error occurred trying to load an @EventBusSubscriber object {} for modid {}", mod.modId, e)
|
||||||
throw LoaderException(e)
|
throw LoaderException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun hasObjectEventHandlers(objectInstance: Any): Boolean {
|
private fun hasObjectEventHandlers(objectInstance: Any): Boolean {
|
||||||
return objectInstance.javaClass.methods.any {
|
return objectInstance.javaClass.methods.any {
|
||||||
|
|
Loading…
Reference in New Issue