diff --git a/build.gradle.kts b/build.gradle.kts index 5511679..52c97b5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,7 +51,7 @@ subprojects { val shadowC by configurations.creating repositories { maven(url = "https://jitpack.io"); mavenCentral() } dependencies { - implementation("$kotlinX:kotlinx-coroutines-core:${rootProject.property("kotlin_coroutines_version")}") + implementation("$kotlinX:kotlinx-coroutines-jdk8:${rootProject.property("kotlin_coroutines_version")}") implementation("$kotlinX:kotlinx-serialization-core:$serializationVer") implementation("$kotlinX:kotlinx-serialization-json:$serializationVer") listOf( @@ -176,7 +176,7 @@ allprojects { tasks { withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class) { with(kotlinOptions) { - freeCompilerArgs = listOf("-Xjvm-default=all", "-Xlambdas=indy", "-progressive") + freeCompilerArgs = listOf("-Xjvm-default=all", "-Xlambdas=indy", "-Xopt-in=kotlin.RequiresOptIn", "-progressive") jvmTarget = if (javaVer.toInt() < 9) "1.$javaVer" else javaVer languageVersion = (kotlinBaseVer.toDouble() + 0.1).toString() apiVersion = kotlinBaseVer 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 3a3fab9..90ecc98 100644 --- a/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java +++ b/common/src/main/java/fr/username404/snowygui/mixins/TitleScreenMixin.java @@ -10,9 +10,14 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(TitleScreen.class) public class TitleScreenMixin { - @Inject(method = "createNormalMenuOptions", at = @At("RETURN")) + private static boolean buttonsInitialized = false; + private static synchronized void setButtonsInitialized() { buttonsInitialized = true; } + @Inject(method = "createNormalMenuOptions", at = @At(value = "RETURN")) public void createNormalMenuOptions(int i, int j, CallbackInfo ci) { - ClickGui.INSTANCE.tick(); - if (!ButtonImpl.Companion.getDone()) ButtonImpl.Companion.initialize(); + if (!buttonsInitialized) { + ClickGui.INSTANCE.tick(); + ButtonImpl.initButtons(); + setButtonsInitialized(); + } } } diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index bc8f26d..1b2318b 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -5,6 +5,7 @@ import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.feature.ButtonImpl import fr.username404.snowygui.gui.feature.Ignored import fr.username404.snowygui.misc.AddKeyMaps +import net.minecraft.network.chat.TranslatableComponent import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger import java.lang.reflect.Modifier @@ -13,6 +14,7 @@ abstract class Snowy { protected fun Class<*>.isValidForButtonCollection(): Boolean = (!((Modifier.isAbstract(javaClass.modifiers)) || javaClass.isAnnotationPresent(Ignored::class.java))) private val displayInitMessage: Boolean by Configuration companion object { + val MissingComponent: TranslatableComponent = object: TranslatableComponent(null) { override fun getString(): String = "MISSING_COMPONENT" } @JvmStatic protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature" lateinit var annotatedButtons: Set> diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt index 7a1edfc..82d538c 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt @@ -55,9 +55,7 @@ val SnowyConfigScreen: Screen get() { ClickGui.clickBoxes.map { box -> startColorField(box.name, box.color).setSaveConsumer { box.color = it - }.setDefaultValue(Category.values().find { - it.translationKey == box.name?.key - }?.categoryColor ?: box.color).build() + }.setDefaultValue(Category.fromBox(box)?.categoryColor ?: box.color).build() } ) }.build()).addEntry(startStrList( 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 defa356..9ac445d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -7,6 +7,7 @@ import com.typesafe.config.ConfigRenderOptions import com.typesafe.config.ConfigValueFactory import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy +import fr.username404.snowygui.Snowy.Companion.MissingComponent import fr.username404.snowygui.gui.elements.ClickBox import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Macro @@ -35,9 +36,9 @@ object Configuration { } }, "box_colors" to mapOf(*ClickGui.clickBoxes.filter { box -> - box.name != null && Category.values().find { category -> box.name.key == category.translationKey }?.categoryColor != box.color + (box.name.key != MissingComponent.key) && Category.fromBox(box)?.categoryColor != box.color }.map { - it.name!!.key to it.color + it.name.key to it.color }.toTypedArray()), ) } diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt index 29b19c1..ab77c20 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt @@ -3,6 +3,7 @@ package fr.username404.snowygui.gui.elements import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.DefaultVertexFormat import com.mojang.blaze3d.vertex.PoseStack +import fr.username404.snowygui.Snowy.Companion.MissingComponent import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.Renderable.Rendering.buffer @@ -24,11 +25,9 @@ import org.lwjgl.opengl.GL20 class ClickBox( x: Double, y: Double, override var color: Int = Colors.BLUE.hexValue, - val name: TranslatableComponent? = null + val name: TranslatableComponent = MissingComponent ): ColoredElement(x, y, 80, 10, color, 0.5F) { - constructor(x: Double, y: Double, color: Colors, name: TranslatableComponent? = null): this(x, y, color.hexValue, name) - - fun isCategory(c: Category): Boolean = (name?.key == c.translationKey) + fun isCategory(c: Category): Boolean = (name.key == c.translationKey) val buttons: MutableSet = if (sortAlphabetically) sortedSetOf(compareBy(String.CASE_INSENSITIVE_ORDER) { it.title }) else mutableSetOf() override fun display(stack: PoseStack?) { hidden = buttons.isEmpty() || hidden @@ -119,14 +118,14 @@ class ClickBox( }.display(poseStack) } } else null - if ((name != null) && (poseStack != null)) { + if (poseStack != null) { Minecraft.getInstance().font.draw(poseStack, name.string, x.toFloat() + 5, y.toFloat() + 1.5F, Colors.TRANSPARENT.hexValue) renderButtons?.join() } } } init { - savedColors?.get(name?.key)?.let { + savedColors?.get(name.key)?.let { this.color = it } } diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt index 1a0d898..1e58266 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt @@ -24,12 +24,20 @@ annotation class ButtonInfo( MISC("snowy.clickbox.misc", Colors.BLUE), RISKY("snowy.clickbox.risky", Colors.RED), MACROS("snowy.clickbox.macros", Colors.GREEN); - val box = ClickBox( - x = 4.0 + (if (ordinal >= 1 && !riskyCheats) ordinal - 1 else ordinal) * 86, y = 4.0, + companion object { + fun fromBox(box: ClickBox): Category? = values().find { + box.isCategory(it) + } + } + var box: ClickBox = ClickBox( + x = 4.0 + (ordinal - ClickGui.clickBoxes.count { category -> + category.hidden + }) * 86, y = 4.0, name = TranslatableComponent(translationKey), color = categoryColor - ) + ); private set constructor(translationKey: String, categoryColor: Colors): this(translationKey, categoryColor.hexValue) + constructor(box: ClickBox): this(translationKey = box.name.key, categoryColor = box.color) { this.box = box } init { ClickGui.components.add(box) } 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 891bf28..8abb662 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 @@ -8,53 +8,50 @@ import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.FontUtil import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc -import fr.username404.snowygui.gui.elements.ClickBox import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type import kotlin.reflect.full.findAnnotation sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { - companion object { - var done: Boolean = false; private set - fun initialize() { - done = true - fun addToButtons(buttons: Collection) { - buttons.groupBy { - ClickGui.components.filterIsInstance().find { box -> - it.let { impl -> - with(impl) { - if (info.kind == Type.TOGGLE) { - Configuration.enabledFeatures[title]?.let { bool -> - toggled = bool - } + internal companion object { + private fun addButtons(vararg buttons: ButtonImpl) { + buttons.groupBy { + ClickGui.clickBoxes.find { box -> + it.let { impl -> + with(impl) { + if (info.kind == Type.TOGGLE) { + Configuration.enabledFeatures[title]?.let { bool -> + toggled = bool } } - box.name?.key ?: box.name.toString() == impl.info.parent.translationKey } + box.isCategory(impl.info.parent) } - }.entries.forEach { entry -> - with(entry) { - value.forEach { - if (!it.isDisabled) { - key?.buttons!!.add(it) - } + } + }.entries.forEach { entry -> + with(entry) { + key?.buttons?.clear() + value.forEach { + if (!it.isDisabled) { + key?.buttons?.add(it) } } } } - addToButtons( - Snowy.annotatedButtons.mapNotNull { - ((try { - it.getConstructor().newInstance() - } catch (e: NoSuchMethodException) { - try { - it.getDeclaredField("INSTANCE").get(null) - } catch (e: NoSuchFieldException) {} - }) as? ButtonImpl) - }.filterNot { - (it.info.parent == Category.RISKY) && !riskyCheats - }.plus(Configuration.foundMacros) - ) } + @JvmStatic + fun initButtons() = addButtons( + *Snowy.annotatedButtons.mapNotNull { + ((try { + it.getConstructor().newInstance() + } catch (e: NoSuchMethodException) { + try { + it.getDeclaredField("INSTANCE").get(null) + } catch (e: NoSuchFieldException) {} + }) as? ButtonImpl) + }.filterNot { + (it.info.parent == Category.RISKY) && !riskyCheats + }.plus(Configuration.foundMacros).toTypedArray() + ) } var isDisabled: Boolean = false; set(value) { field = value diff --git a/common/src/main/resources/assets/snowygui/lang/en_us.json b/common/src/main/resources/assets/snowygui/lang/en_us.json index 69b2a23..8e37ed7 100644 --- a/common/src/main/resources/assets/snowygui/lang/en_us.json +++ b/common/src/main/resources/assets/snowygui/lang/en_us.json @@ -1,4 +1,5 @@ { + "snowy.missing.key": "MISSING_KEY", "screen.snowy.gui": "Snowy Interface", "screen.snowy.config": "Snowy configuration screen", "screen.snowy.config.general": "General",