Convert indents to tabs

This commit is contained in:
Shadowfacts 2018-06-11 17:22:36 -04:00
parent 7cebe692e7
commit 7b885ea4cd
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 79 additions and 79 deletions

View File

@ -16,84 +16,84 @@ import java.util.EnumSet
import kotlin.reflect.full.companionObjectInstance import kotlin.reflect.full.companionObjectInstance
object ForgelinAutomaticEventSubscriber { 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) { fun subscribeAutomatic(mod: ModContainer, asm: ASMDataTable, currentSide: Side) {
LOGGER.debug("Attempting to register Kotlin @EventBusSubscriber objects for {}", mod.modId) 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 containedMods = modAnnotations.get(Mod::class.java.name)
val subscribers = modAnnotations.get(Mod.EventBusSubscriber::class.java.name) val subscribers = modAnnotations.get(Mod.EventBusSubscriber::class.java.name)
.filter { parseTargetSides(it).contains(currentSide) } .filter { parseTargetSides(it).contains(currentSide) }
val loader = Loader.instance().modClassLoader val loader = Loader.instance().modClassLoader
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.warn("Could not determine owning mod for @EventBusSubscriber on {} for mod {}", subscriber.className, mod.modId)
continue continue
} }
if (mod.modId != ownerModId) { if (mod.modId != 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, mod.modId)
continue 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 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)) {
MinecraftForge.EVENT_BUS.unregister(subscriberClass) MinecraftForge.EVENT_BUS.unregister(subscriberClass)
LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className) LOGGER.debug("Unregistered static @EventBusSubscriber class {}", subscriber.className)
} }
if (hasObjectEventHandlers(objectInstance)) { if (hasObjectEventHandlers(objectInstance)) {
MinecraftForge.EVENT_BUS.register(objectInstance) MinecraftForge.EVENT_BUS.register(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 {
!Modifier.isStatic(it.modifiers) && it.isAnnotationPresent(SubscribeEvent::class.java) !Modifier.isStatic(it.modifiers) && it.isAnnotationPresent(SubscribeEvent::class.java)
} }
} }
private fun hasStaticEventHandlers(clazz: Class<*>): Boolean { private fun hasStaticEventHandlers(clazz: Class<*>): Boolean {
return clazz.methods.any { return clazz.methods.any {
Modifier.isStatic(it.modifiers) && it.isAnnotationPresent(SubscribeEvent::class.java) 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()) {
return parsedModId return parsedModId
} }
return ASMDataTable.getOwnerModID(containedMods, subscriber) return ASMDataTable.getOwnerModID(containedMods, subscriber)
} }
private fun parseTargetSides(subscriber: ASMData): EnumSet<Side> { private fun parseTargetSides(subscriber: ASMData): EnumSet<Side> {
val parsedSides: List<EnumHolder>? = subscriber.annotationInfo["value"] as? List<EnumHolder> val parsedSides: List<EnumHolder>? = subscriber.annotationInfo["value"] as? List<EnumHolder>
if (parsedSides != null) { if (parsedSides != null) {
val targetSides = EnumSet.noneOf(Side::class.java) val targetSides = EnumSet.noneOf(Side::class.java)
for (parsed in parsedSides) { for (parsed in parsedSides) {
targetSides.add(Side.valueOf(parsed.value)) targetSides.add(Side.valueOf(parsed.value))
} }
return targetSides return targetSides
} }
return DEFAULT_SUBSCRIPTION_SIDES return DEFAULT_SUBSCRIPTION_SIDES
} }
} }

View File

@ -8,19 +8,19 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
@Mod(modid = AutomaticKtSubscriberTest.MODID, modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter") @Mod(modid = AutomaticKtSubscriberTest.MODID, modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter")
object AutomaticKtSubscriberTest { object AutomaticKtSubscriberTest {
const val MODID = "ktsubtest" const val MODID = "ktsubtest"
@EventBusSubscriber(modid = AutomaticKtSubscriberTest.MODID) @EventBusSubscriber(modid = AutomaticKtSubscriberTest.MODID)
object EventSubscriber { object EventSubscriber {
@SubscribeEvent @SubscribeEvent
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 @JvmStatic
@SubscribeEvent @SubscribeEvent
fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) { fun onRightClickItem(event: PlayerInteractEvent.RightClickItem) {
println("Right click item") println("Right click item")
} }
} }
} }