From 54dc2169fcfc6d8de22b533a568356604cf5ca1f Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Sat, 21 Feb 2026 22:15:59 +0100 Subject: [PATCH] Make Snowy.annotatedButtons lazy Fixes usage on neoforge Signed-off-by: Username404-59 --- .../kotlin/fr/username404/snowygui/Snowy.kt | 4 +-- .../username404/snowygui/fabric/FabricInit.kt | 26 ++++++++++++------- .../username404/snowygui/forge/ForgeInit.kt | 26 ++++++++++--------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index 537eed3..10878b1 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -14,7 +14,7 @@ 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> + abstract val annotatedButtons: Lazy>> companion object { val MissingComponent: Component = translatable("MISSING_COMPONENT") @Suppress("JVM_STATIC_ON_CONST_OR_JVM_FIELD") // See KT-39868 @@ -25,7 +25,7 @@ abstract class Snowy { @JvmField val logs: Logger = LogManager.getLogger() } - init { Companion.annotatedButtons = ::annotatedButtons::get } + init { Companion.annotatedButtons = { annotatedButtons.value } } private fun eventsInit() { onEvent("EndTick") { for (key in AddKeyMaps.list.keys) { 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 038eccc..06d44c8 100644 --- a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt +++ b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt @@ -7,6 +7,7 @@ import net.fabricmc.api.ClientModInitializer import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback.EVENT import net.fabricmc.loader.api.FabricLoader +import net.fabricmc.loader.launch.common.FabricLauncherBase import net.minecraft.client.DeltaTracker import net.minecraft.client.gui.GuiGraphics import kotlin.io.path.exists @@ -25,16 +26,21 @@ class FabricInit: Snowy(), ClientModInitializer { } ) } - override val annotatedButtons = FabricLoader.getInstance().allMods.mapNotNull { - it.findPath(FEATURE_PACKAGE.replace('.', '/')).getOrNull() - }.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)) + override val annotatedButtons = lazy { + FabricLoader.getInstance().allMods.mapNotNull { + it.findPath(FEATURE_PACKAGE.replace('.', '/')).getOrNull() + }.filter { it.exists() && it.isDirectory() }.flatMap { buttonsDirectory -> + buttonsDirectory.listDirectoryEntries("*.class").mapNotNull { file -> + @Suppress("DEPRECATION") + FabricLauncherBase.getClass( + file.pathString.drop(1) + .replace('/', '.').removeSuffix(".class") + ).let { foundClass -> + if (foundClass.isValidForButtonCollection()) + foundClass.asSubclass(ButtonImpl::class.java) + else null } } - } - }.toSet() + }.toSet() + } } \ No newline at end of file diff --git a/neoforge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt b/neoforge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt index 3d4e7fe..5373c02 100644 --- a/neoforge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/neoforge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -16,18 +16,20 @@ import net.neoforged.neoforgespi.language.ModFileScanData @Suppress("UNUSED_PARAMETER") class ForgeInit(container: ModContainer): Snowy() { private fun initSetup(event: FMLClientSetupEvent) = atInit() - override val annotatedButtons = ModList.get() // Forge-specific reflection - .allScanData - .flatMap { obj: ModFileScanData -> obj.classes } - .filter { data: ModFileScanData.ClassData? -> - data!!.clazz.className.startsWith(FEATURE_PACKAGE) - } - .mapNotNull { - Class.forName((it!!.clazz.className), false, this::class.java.classLoader).run { - takeIf { isValidForButtonCollection() } - }?.asSubclass(ButtonImpl::class.java) - } - .toSet() + override val annotatedButtons = lazy { // Forge-specific reflection + ModList.get() + .allScanData + .flatMap { obj: ModFileScanData -> obj.classes } + .filter { data: ModFileScanData.ClassData? -> + data!!.clazz.className.startsWith(FEATURE_PACKAGE) + } + .mapNotNull { + Class.forName((it!!.clazz.className), false, this::class.java.classLoader).run { + takeIf { isValidForButtonCollection() } + }?.asSubclass(ButtonImpl::class.java) + } + .toSet() + } init { container.eventBus!!.run { addListener(this@ForgeInit::initSetup)