diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index 55a15e6..99d04f6 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -2,6 +2,7 @@ package fr.username404.snowygui import fr.username404.snowygui.EventSnowy.Companion.useKey import fr.username404.snowygui.config.Configuration.obtained +import fr.username404.snowygui.gui.feature.ButtonInfo import fr.username404.snowygui.misc.AddKeyMaps import io.github.config4k.getValue import org.apache.logging.log4j.LogManager @@ -15,19 +16,14 @@ import org.reflections8.util.ConfigurationBuilder abstract class Snowy { private val displayInitMessage: Boolean by obtained companion object { - val reflections = Reflections(ConfigurationBuilder() + var annotatedButtons: Set> = Reflections(ConfigurationBuilder() .setUrls(ClasspathHelper.forClassLoader()) .setScanners(SubTypesScanner(false), TypeAnnotationsScanner()) .useParallelExecutor() - ) + ).getTypesAnnotatedWith(ButtonInfo::class.java) fun onEvent(e: String, lambda: argsLambda) = useKey(e).add(lambda) @JvmField val logs: Logger = LogManager.getLogger() - init { - reflections.allTypes.forEach { - if (it.contains("fr.username404")) logs.info(it) - } - } } private fun eventsInit() { onEvent("EndTick") { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt index 21c4993..29c7864 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt @@ -17,7 +17,7 @@ abstract class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { var done: Boolean = false; private set fun initialize() { done = true - Snowy.reflections.getTypesAnnotatedWith(ButtonInfo::class.java).forEach { // Initializes every button + Snowy.annotatedButtons.forEach { // Initializes every button ((try { it.getConstructor().newInstance() } catch (e: NoSuchMethodException) { diff --git a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt index 79a7cfd..3f2a4f1 100644 --- a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -2,14 +2,19 @@ package fr.username404.snowygui.forge import fr.username404.snowygui.Snowy import fr.username404.snowygui.config.SnowyConfigScreen +import fr.username404.snowygui.gui.feature.ButtonInfo import net.minecraftforge.fml.ExtensionPoint +import net.minecraftforge.fml.ModList import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent +import net.minecraftforge.forgespi.language.ModFileScanData +import org.objectweb.asm.Type import thedarkcolour.kotlinforforge.forge.FORGE_BUS import thedarkcolour.kotlinforforge.forge.LOADING_CONTEXT import thedarkcolour.kotlinforforge.forge.MOD_BUS import java.util.function.BiFunction import java.util.function.Supplier +import java.util.stream.Collectors @Mod("snowygui") @Suppress("UNUSED_PARAMETER") @@ -28,6 +33,22 @@ class ForgeInit: Snowy() { ) } init { + annotatedButtons = ModList.get() // Forge-specific reflection + .allScanData + .stream() + .map { obj: ModFileScanData -> obj.classes } + .flatMap { obj: Set -> obj.stream() } + .filter { data: ModFileScanData.ClassData? -> + (data!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(data) as Type).className.let { + if (it.contains("fr.username404") && !it.contains("mixins")) { + Class.forName(it).declaredAnnotations.any { annotation -> + annotation is ButtonInfo + } + } else false + } + } + .map { Class.forName((it!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(it) as Type).className) } + .collect(Collectors.toSet()) with(MOD_BUS) { addListener(this@ForgeInit::initSetup) addListener(this@ForgeInit::configSetup)