From e6f09a0bc2a71d6f556c020dc3e5003d50f2a1ba Mon Sep 17 00:00:00 2001 From: Username404 Date: Mon, 31 May 2021 15:18:06 +0200 Subject: [PATCH] Add configuration of macros to the cloth config screen --- .../snowygui/config/ConfigScreen.kt | 28 +++++++++++++++++-- .../snowygui/config/Configuration.kt | 20 ++++++------- .../snowygui/gui/elements/ClickBox.kt | 5 +++- .../snowygui/gui/feature/ButtonImpl.kt | 9 +++--- 4 files changed, 44 insertions(+), 18 deletions(-) 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 cf9dc29..b162a1d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt @@ -1,8 +1,12 @@ package fr.username404.snowygui.config import com.mojang.blaze3d.vertex.PoseStack +import fr.username404.snowygui.ClickGui import fr.username404.snowygui.gui.FontUtil +import fr.username404.snowygui.gui.elements.ClickBox +import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Colors +import fr.username404.snowygui.gui.feature.Macro import fr.username404.snowygui.gui.feature.riskyCheatsEnabled import net.minecraft.client.gui.screens.Screen import net.minecraft.network.chat.Component @@ -16,6 +20,10 @@ var configScreenParent: Screen? = null val SnowyConfigScreen: Screen get() { return try { Class.forName("me.shedaniel.clothconfig2.api.ConfigBuilder") + val macrosBox: ClickBox = ClickGui.components.find { + (it is ClickBox) && it.isCategory(Category.MACROS) + } as ClickBox + val macrosButtons = macrosBox.buttons me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground() .setShouldListSmoothScroll(true) .setTitle(translationComponent).apply { @@ -23,11 +31,27 @@ val SnowyConfigScreen: Screen get() { getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).build()) .addEntry( startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheatsEnabled) + .setDefaultValue(false) .requireRestart() .setSaveConsumer { riskyCheatsEnabled = it - }.build() - ) + }.setTooltip(Component.nullToEmpty("WARNING: Do not use this on servers or you might get banned.")).build() + ).addEntry(startStrList( + TranslatableComponent(Category.MACROS.translationKey), + macrosButtons.map { (it as Macro).run { "$title: $command" } } + ).setDefaultValue(listOf()).setSaveConsumer { it.forEach { string -> + val splitString: Pair? = string.split(":").map { toTrim -> toTrim.trimStart() }.let { element -> + if (element.size == 2) element.component1() to element.component2() else null + }; if (splitString != null) { + Macro(title = splitString.first, command = splitString.second).let { newMacro -> + macrosButtons.removeIf { existingMacro -> + existingMacro.title == newMacro.title + } + macrosButtons.add(newMacro) + } + } + } + }.build()) } }.build() } catch (e: ClassNotFoundException) { 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 8e8b788..3aaf4a4 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -4,7 +4,8 @@ import com.typesafe.config.* import com.typesafe.config.ConfigFactory.* import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy -import fr.username404.snowygui.gui.feature.ButtonImpl +import fr.username404.snowygui.gui.elements.ClickBox +import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Macro import fr.username404.snowygui.gui.feature.riskyCheatsEnabled import io.github.config4k.extract @@ -77,11 +78,11 @@ object Configuration { putAll(obtained.extract("enabledFeatures")) } } - val macros: MutableSet = run { + val foundMacros: Set = run { if (!macroFile.exists()) macroFile.createNewFile() macroFile.readLines(Charset.forName("UTF-8")).filterNot { it.isBlank() }.map { Snowy.Gson.fromJson(it, Macro::class.java).copy() - }.toMutableSet() + }.toSet() } init { Runtime.getRuntime().addShutdownHook( @@ -90,19 +91,16 @@ object Configuration { ClickGui.boxContext { enabledFeatures.putAll( buttons.map { - (it as ButtonImpl).run { + it.run { title to toggled } } ) } - macros.map { Snowy.Gson.toJson(it) }.forEachIndexed { index, s -> - with(macroFile) { - "$s\n".let { - if (index == 0) { - writeText(it) - } else appendText(it) - } + with(macroFile) { + writeText("") + (ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { Snowy.Gson.toJson(it as Macro) }.forEach { s -> + appendText("$s\n") } } writeConfig(obtained.withFullModifiableValues()).join() 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 05a6693..d0bfd63 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 @@ -7,6 +7,8 @@ import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.Renderable.Rendering.buffer import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc import fr.username404.snowygui.gui.Renderable.Rendering.tessellator +import fr.username404.snowygui.gui.feature.ButtonImpl +import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Colors import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -21,7 +23,8 @@ class ClickBox( color: Colors = Colors.BLUE, val name: TranslatableComponent? = null ): ColoredElement(x, y, 80, 10, color, 0.5F) { - val buttons = mutableSetOf() + fun isCategory(c: Category): Boolean = (name?.key == c.translationKey) + val buttons = mutableSetOf() override fun display(stack: PoseStack?) { hidden = buttons.isEmpty() || hidden super.display(stack) 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 f6ac169..33c43cc 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 @@ -42,17 +42,18 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { } } addToButtons( - Snowy.annotatedButtons.map { + Snowy.annotatedButtons.mapNotNull { ((try { it.getConstructor().newInstance() } catch (e: NoSuchMethodException) { try { it.getDeclaredField("INSTANCE").get(null) - } catch (e: NoSuchFieldException) {} + } catch (e: NoSuchFieldException) { + } }) as? ButtonImpl) - }.filterNotNull().filterNot { + }.filterNot { (it.info.parent == Category.RISKY) && !riskyCheatsEnabled - }.plus(Configuration.macros) + }.plus(Configuration.foundMacros) ) } }