diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index a530330..a02f5e4 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -13,17 +13,19 @@ abstract class Snowy { protected fun Class<*>.isValidForButtonCollection(): Boolean = !Modifier.isAbstract(modifiers) && declaredAnnotations.any { it is ButtonInfo && !it.ignored } private val displayInitMessage: Boolean by Configuration + abstract val annotatedButtons: Set> companion object { @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") object MissingComponent: TranslatableComponent(null) { override fun getString(): String = "MISSING_COMPONENT" } @Suppress("JVM_STATIC_ON_CONST_OR_JVM_FIELD") // See KT-39868 @JvmStatic protected const val FeaturePackage: String = "fr.username404.snowygui.gui.feature" - lateinit var annotatedButtons: Set> + lateinit var annotatedButtons: () -> Set> fun onEvent(e: Any, lambda: argsLambda) = EventSnowy[e.toString()].add(lambda) @JvmField val logs: Logger = LogManager.getLogger() } + init { Companion.annotatedButtons = ::annotatedButtons::get } private fun eventsInit() { onEvent("EndTick") { for (Key in AddKeyMaps.list.keys) { 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 9c1cfc1..c7f534a 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 @@ -38,7 +38,7 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { } @JvmStatic fun initButtons() = addButtons( - *Snowy.annotatedButtons.mapNotNull { + *Snowy.annotatedButtons().mapNotNull { try { it.getConstructor().newInstance() } catch (_: NoSuchMethodException) { diff --git a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt index 59fb24f..9f073c4 100644 --- a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt +++ b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt @@ -23,21 +23,16 @@ class FabricInit: Snowy(), ClientModInitializer { } ) } - init { - val modsPaths = FabricLoader.getInstance().allMods.map { - it.getPath(FeaturePackage.replace('.', '/')) - }.filter { it.exists() && it.isDirectory() } - annotatedButtons = modsPaths.flatMap { buttonsDirectory -> - val classSet = mutableSetOf>() - buttonsDirectory.run { - listDirectoryEntries("*.class").forEach { file -> - @Suppress("DEPRECATION") net.fabricmc.loader.launch.common. - FabricLauncherBase.getClass(file.pathString.drop(1).replace('/', '.').removeSuffix(".class")).let { foundClass -> - if (foundClass.isValidForButtonCollection()) classSet.add(foundClass.asSubclass(ButtonImpl::class.java)) - } + override val annotatedButtons = FabricLoader.getInstance().allMods.map { + it.getPath(FeaturePackage.replace('.', '/')) + }.filter { it.exists() && it.isDirectory() }.flatMap { buttonsDirectory -> + mutableSetOf>().apply { + buttonsDirectory.listDirectoryEntries("*.class").forEach { file -> + @Suppress("DEPRECATION") net.fabricmc.loader.launch.common. + FabricLauncherBase.getClass(file.pathString.drop(1).replace('/', '.').removeSuffix(".class")).let { foundClass -> + if (foundClass.isValidForButtonCollection()) add(foundClass.asSubclass(ButtonImpl::class.java)) } } - classSet - }.toSet() - } + } + }.toSet() } \ No newline at end of file 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 376838f..df6744a 100644 --- a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -14,7 +14,6 @@ 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.stream.Collectors @Mod("snowygui") @Suppress("UNUSED_PARAMETER") @@ -25,19 +24,17 @@ class ForgeInit: Snowy() { ExtensionPoint.CONFIGGUIFACTORY ) { BiFunction { _, parent -> configScreenParent = parent; SnowyConfigScreen } } } - 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 { classname -> - classname.startsWith(FeaturePackage) && Class.forName(classname).isValidForButtonCollection() - } + override val annotatedButtons = ModList.get() // Forge-specific reflection + .allScanData + .flatMap { obj: ModFileScanData -> obj.classes } + .filter { data: ModFileScanData.ClassData? -> + (data!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(data) as Type).className.let { classname -> + classname.startsWith(FeaturePackage) && Class.forName(classname).isValidForButtonCollection() } - .map { Class.forName((it!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(it) as Type).className).asSubclass(ButtonImpl::class.java)} - .collect(Collectors.toSet()) + } + .map { Class.forName((it!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(it) as Type).className).asSubclass(ButtonImpl::class.java)} + .toSet() + init { with(MOD_BUS) { addListener(this@ForgeInit::initSetup) addListener(this@ForgeInit::configSetup)