diff --git a/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java b/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java index 341ef7a..3a3fab9 100644 --- a/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java +++ b/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java @@ -1,6 +1,7 @@ package fr.username404.snowygui.mixins; import fr.username404.snowygui.ClickGui; +import fr.username404.snowygui.gui.feature.ButtonImpl; import net.minecraft.client.gui.screens.TitleScreen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -10,5 +11,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(TitleScreen.class) public class TitleScreenMixin { @Inject(method = "createNormalMenuOptions", at = @At("RETURN")) - public void createNormalMenuOptions(int i, int j, CallbackInfo ci) { ClickGui.INSTANCE.tick(); } + public void createNormalMenuOptions(int i, int j, CallbackInfo ci) { + ClickGui.INSTANCE.tick(); + if (!ButtonImpl.Companion.getDone()) ButtonImpl.Companion.initialize(); + } } diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index e37b24b..108b2df 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -2,7 +2,6 @@ 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 @@ -28,15 +27,6 @@ abstract class Snowy { } fun atInit() { if (displayInitMessage) logs.info("Init point of SnowyGUI hit.") - reflections.getTypesAnnotatedWith(ButtonInfo::class.java).forEach { // Initializes every button - logs.info( - try { - it.getConstructor().newInstance() - } catch (e: NoSuchMethodException) { - it.kotlin.objectInstance - } - ) - } eventsInit() } } \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index 07147fe..dc92b23 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -5,7 +5,9 @@ import com.typesafe.config.ConfigException import com.typesafe.config.ConfigFactory.* import com.typesafe.config.ConfigRenderOptions import com.typesafe.config.ConfigValueFactory +import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy +import fr.username404.snowygui.gui.feature.ButtonImpl import io.github.config4k.extract import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch @@ -69,15 +71,15 @@ object Configuration { Runtime.getRuntime().addShutdownHook( Thread { runBlocking { - /** ClickGui.boxContext { enabledFeatures.putAll( - this.buttons.map { - (it::class.java.getDeclaredField("name").get(this) as String) to (it::class.java.getDeclaredMethod("getToggled").invoke(it)) as Boolean // TODO Fix button configuration + buttons.map { + (it as ButtonImpl).run { + title.toString() to toggled + } } ) } - **/ writeConfig(obtained.withValue("enabledFeatures", ConfigValueFactory.fromMap(enabledFeatures))).join() // TODO Fix formatting of enabledFeatures } } 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 f5238dd..23d7aa4 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 @@ -3,6 +3,7 @@ package fr.username404.snowygui.gui.feature import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.PoseStack import fr.username404.snowygui.ClickGui +import fr.username404.snowygui.Snowy import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.FontUtil @@ -12,6 +13,30 @@ import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type import kotlin.reflect.full.findAnnotation abstract class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { + companion object { + var done: Boolean = false; private set + fun initialize() { + done = true + Snowy.reflections.getTypesAnnotatedWith(ButtonInfo::class.java).forEach { // Initializes every button + (try { + it.getConstructor().newInstance() + } catch (e: NoSuchMethodException) { + it.kotlin.objectInstance + } as ButtonImpl).let { impl -> + with(impl) { + if (info.kind == ButtonInfo.Companion.Type.TOGGLE) { + Configuration.enabledFeatures[title]?.let { bool -> + toggled = bool + } + } + } + ClickGui.components.filterIsInstance().find { box -> + box.name!!.key == impl.info.parent.translationKey + }?.buttons!!.add(impl) + } + } + } + } private val info = this::class.findAnnotation() ?: throw Exception("Missing @Button annotaton") override var color = info.parent.categoryColor val title = this@ButtonImpl::class.simpleName @@ -50,14 +75,4 @@ abstract class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { FontUtil.drawScaled(poseStack, title, x + 1, y + 1, 0.75F) } } - init { - if (info.kind == Type.TOGGLE) { - Configuration.enabledFeatures[title]?.let { - toggled = it - } - } - ClickGui.components.filterIsInstance().find { - it.name!!.key == info.parent.translationKey - }?.buttons!!.add(this) - } }