Only unregister static/register object event handlers unless appropriate handler methods are detected
This commit is contained in:
parent
26b895b6ce
commit
7cebe692e7
|
@ -8,8 +8,10 @@ import net.minecraftforge.fml.common.ModContainer
|
||||||
import net.minecraftforge.fml.common.discovery.ASMDataTable
|
import net.minecraftforge.fml.common.discovery.ASMDataTable
|
||||||
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData
|
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData
|
||||||
import net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder
|
import net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder
|
||||||
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
|
||||||
import net.minecraftforge.fml.relauncher.Side
|
import net.minecraftforge.fml.relauncher.Side
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
|
import java.lang.reflect.Modifier
|
||||||
import java.util.EnumSet
|
import java.util.EnumSet
|
||||||
import kotlin.reflect.full.companionObjectInstance
|
import kotlin.reflect.full.companionObjectInstance
|
||||||
|
|
||||||
|
@ -45,17 +47,35 @@ object ForgelinAutomaticEventSubscriber {
|
||||||
|
|
||||||
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 subscriberInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue
|
val objectInstance = kotlinClass.objectInstance ?: kotlinClass.companionObjectInstance ?: continue
|
||||||
|
|
||||||
|
if (!hasStaticEventHandlers(subscriberClass)) {
|
||||||
MinecraftForge.EVENT_BUS.unregister(subscriberClass)
|
MinecraftForge.EVENT_BUS.unregister(subscriberClass)
|
||||||
MinecraftForge.EVENT_BUS.register(subscriberInstance)
|
LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className)
|
||||||
LOGGER.debug("Registered @EventBusSubscriber object {}", subscriber.className)
|
}
|
||||||
|
if (hasObjectEventHandlers(objectInstance)) {
|
||||||
|
MinecraftForge.EVENT_BUS.register(objectInstance)
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<ASMData>, subscriber: ASMData): String? {
|
private fun parseModId(containedMods: MutableSet<ASMData>, subscriber: ASMData): String? {
|
||||||
val parsedModId: String? = subscriber.annotationInfo["modid"] as? String
|
val parsedModId: String? = subscriber.annotationInfo["modid"] as? String
|
||||||
if (parsedModId.isNullOrEmpty()) {
|
if (parsedModId.isNullOrEmpty()) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package net.shadowfacts.forgelin
|
package net.shadowfacts.forgelin
|
||||||
|
|
||||||
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent
|
||||||
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock
|
||||||
import net.minecraftforge.fml.common.Mod
|
import net.minecraftforge.fml.common.Mod
|
||||||
import net.minecraftforge.fml.common.Mod.EventBusSubscriber
|
import net.minecraftforge.fml.common.Mod.EventBusSubscriber
|
||||||
|
@ -15,5 +16,11 @@ object AutomaticKtSubscriberTest {
|
||||||
fun onRightClickBlock(event: RightClickBlock) {
|
fun onRightClickBlock(event: RightClickBlock) {
|
||||||
println("Automatic KT subscriber: Right click ${event.pos}")
|
println("Automatic KT subscriber: Right click ${event.pos}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@SubscribeEvent
|
||||||
|
fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) {
|
||||||
|
println("Right click item")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue