Add configuration of macros to the cloth config screen

This commit is contained in:
Username404 2021-05-31 15:18:06 +02:00
parent b84ace9d3c
commit e6f09a0bc2
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 44 additions and 18 deletions

View File

@ -1,8 +1,12 @@
package fr.username404.snowygui.config package fr.username404.snowygui.config
import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.gui.FontUtil 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.Colors
import fr.username404.snowygui.gui.feature.Macro
import fr.username404.snowygui.gui.feature.riskyCheatsEnabled import fr.username404.snowygui.gui.feature.riskyCheatsEnabled
import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
@ -16,6 +20,10 @@ var configScreenParent: Screen? = null
val SnowyConfigScreen: Screen get() { val SnowyConfigScreen: Screen get() {
return try { return try {
Class.forName("me.shedaniel.clothconfig2.api.ConfigBuilder") 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() me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground()
.setShouldListSmoothScroll(true) .setShouldListSmoothScroll(true)
.setTitle(translationComponent).apply { .setTitle(translationComponent).apply {
@ -23,11 +31,27 @@ val SnowyConfigScreen: Screen get() {
getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).build()) getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).build())
.addEntry( .addEntry(
startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheatsEnabled) startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheatsEnabled)
.setDefaultValue(false)
.requireRestart() .requireRestart()
.setSaveConsumer { .setSaveConsumer {
riskyCheatsEnabled = it 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, String>? = 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() }.build()
} catch (e: ClassNotFoundException) { } catch (e: ClassNotFoundException) {

View File

@ -4,7 +4,8 @@ import com.typesafe.config.*
import com.typesafe.config.ConfigFactory.* import com.typesafe.config.ConfigFactory.*
import fr.username404.snowygui.ClickGui import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy 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.Macro
import fr.username404.snowygui.gui.feature.riskyCheatsEnabled import fr.username404.snowygui.gui.feature.riskyCheatsEnabled
import io.github.config4k.extract import io.github.config4k.extract
@ -77,11 +78,11 @@ object Configuration {
putAll(obtained.extract("enabledFeatures")) putAll(obtained.extract("enabledFeatures"))
} }
} }
val macros: MutableSet<Macro> = run { val foundMacros: Set<Macro> = run {
if (!macroFile.exists()) macroFile.createNewFile() if (!macroFile.exists()) macroFile.createNewFile()
macroFile.readLines(Charset.forName("UTF-8")).filterNot { it.isBlank() }.map { macroFile.readLines(Charset.forName("UTF-8")).filterNot { it.isBlank() }.map {
Snowy.Gson.fromJson(it, Macro::class.java).copy() Snowy.Gson.fromJson(it, Macro::class.java).copy()
}.toMutableSet() }.toSet()
} }
init { init {
Runtime.getRuntime().addShutdownHook( Runtime.getRuntime().addShutdownHook(
@ -90,19 +91,16 @@ object Configuration {
ClickGui.boxContext { ClickGui.boxContext {
enabledFeatures.putAll( enabledFeatures.putAll(
buttons.map { buttons.map {
(it as ButtonImpl).run { it.run {
title to toggled title to toggled
} }
} }
) )
} }
macros.map { Snowy.Gson.toJson(it) }.forEachIndexed { index, s -> with(macroFile) {
with(macroFile) { writeText("")
"$s\n".let { (ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { Snowy.Gson.toJson(it as Macro) }.forEach { s ->
if (index == 0) { appendText("$s\n")
writeText(it)
} else appendText(it)
}
} }
} }
writeConfig(obtained.withFullModifiableValues()).join() writeConfig(obtained.withFullModifiableValues()).join()

View File

@ -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.buffer
import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc
import fr.username404.snowygui.gui.Renderable.Rendering.tessellator 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 fr.username404.snowygui.gui.feature.Colors
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -21,7 +23,8 @@ class ClickBox(
color: Colors = Colors.BLUE, color: Colors = Colors.BLUE,
val name: TranslatableComponent? = null val name: TranslatableComponent? = null
): ColoredElement(x, y, 80, 10, color, 0.5F) { ): ColoredElement(x, y, 80, 10, color, 0.5F) {
val buttons = mutableSetOf<ColoredElement>() fun isCategory(c: Category): Boolean = (name?.key == c.translationKey)
val buttons = mutableSetOf<ButtonImpl>()
override fun display(stack: PoseStack?) { override fun display(stack: PoseStack?) {
hidden = buttons.isEmpty() || hidden hidden = buttons.isEmpty() || hidden
super.display(stack) super.display(stack)

View File

@ -42,17 +42,18 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
} }
} }
addToButtons( addToButtons(
Snowy.annotatedButtons.map { Snowy.annotatedButtons.mapNotNull {
((try { ((try {
it.getConstructor().newInstance() it.getConstructor().newInstance()
} catch (e: NoSuchMethodException) { } catch (e: NoSuchMethodException) {
try { try {
it.getDeclaredField("INSTANCE").get(null) it.getDeclaredField("INSTANCE").get(null)
} catch (e: NoSuchFieldException) {} } catch (e: NoSuchFieldException) {
}
}) as? ButtonImpl) }) as? ButtonImpl)
}.filterNotNull().filterNot { }.filterNot {
(it.info.parent == Category.RISKY) && !riskyCheatsEnabled (it.info.parent == Category.RISKY) && !riskyCheatsEnabled
}.plus(Configuration.macros) }.plus(Configuration.foundMacros)
) )
} }
} }