Convert indents to tabs
This commit is contained in:
parent
7cebe692e7
commit
7b885ea4cd
|
@ -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<ASMData>, subscriber: ASMData): String? {
|
||||
val parsedModId: String? = subscriber.annotationInfo["modid"] as? String
|
||||
if (parsedModId.isNullOrEmpty()) {
|
||||
return parsedModId
|
||||
}
|
||||
private fun parseModId(containedMods: MutableSet<ASMData>, 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<Side> {
|
||||
val parsedSides: List<EnumHolder>? = subscriber.annotationInfo["value"] as? List<EnumHolder>
|
||||
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<Side> {
|
||||
val parsedSides: List<EnumHolder>? = subscriber.annotationInfo["value"] as? List<EnumHolder>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue