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.elements.ClickBox.Companion.buttonsMax import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically 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.riskyCheats import net.minecraft.client.gui.screens.Screen import net.minecraft.network.chat.Component import net.minecraft.network.chat.TranslatableComponent import java.util.* private const val confPrefix: String = "screen.snowy.config" private val translationComponent = TranslatableComponent(confPrefix) var configScreenParent: Screen? = null private fun supplyComponent(string: String?): Optional = string?.run { Optional.of(TranslatableComponent(string)) } ?: Optional.empty() 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 @Suppress("UNCHECKED_CAST") val macrosButtons = macrosBox.buttons as MutableSet fun Collection.getTitleCommand(): MutableList = map { it.run { "$title: $command" } }.toMutableList() me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground() .setShouldListSmoothScroll(true) .setTitle(translationComponent).apply { with(entryBuilder()) { getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).apply { add(startBooleanToggle(TranslatableComponent("$confPrefix.behavior.sortalphabetically"), sortAlphabetically) .setDefaultValue(true).requireRestart() .setSaveConsumer { sortAlphabetically = it }.build() ) }.build()) .addEntry( startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheats) .setDefaultValue(false) .requireRestart() .setSaveConsumer { riskyCheats = it }.setTooltip(Component.nullToEmpty("WARNING: Do not use this on servers or you might get banned.")).build() ).addEntry(startSubCategory(TranslatableComponent("$confPrefix.colors")).also { builder -> builder.addAll( ClickGui.clickBoxes.map { box -> startColorField(box.name, box.color).setSaveConsumer { box.color = it }.setDefaultValue(Category.fromBox(box)?.categoryColor ?: box.color).build() } ) }.build()).addEntry(startStrList( TranslatableComponent(Category.MACROS.translationKey), macrosButtons.getTitleCommand() ).setInsertInFront(false).setDefaultValue(Configuration.foundMacros.getTitleCommand()).setErrorSupplier { list -> supplyComponent(if (list.size > buttonsMax) "$confPrefix.general.macros.toomuchbuttons" else null) }.setCellErrorSupplier { with(it.split(":")) { supplyComponent( when { isEmpty() || !it.contains(":") -> "$confPrefix.general.macros.missingdelimiter" size < 2 -> "$confPrefix.general.macros.missingelement" size > 2 -> "$confPrefix.general.macros.toomuchdelimiters" first().count() > 16 -> "$confPrefix.general.macros.toomuchcharacters" else -> null } ) } }.setTooltip(TranslatableComponent("$confPrefix.general.macros.tooltip")).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) || (existingMacro.command == newMacro.command) || !it.any { currentString -> currentString.startsWith(existingMacro.title) } // Needed to remove duplicates from the config screen } macrosButtons.add(newMacro) } } } if (it.isEmpty().also { empty -> macrosBox.hidden = empty }) macrosButtons.clear() }.build()) } }.build() } catch (e: ClassNotFoundException) { object: Screen(translationComponent) { override fun isPauseScreen(): Boolean = false override fun render(poseStack: PoseStack, i: Int, j: Int, f: Float) { super.renderBackground(poseStack) FontUtil.drawScaled(poseStack, text = "An appropriate version of the Cloth Config mod is required for the configuration of snowygui.", 16.0, 16.0, color = Colors.WHITE, scaleFactor = 0.85F ) } override fun onClose() { minecraft?.screen = configScreenParent } } } }