From 34de53beee563d82c43b1123efb06beaeb43e5a4 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Thu, 24 Jun 2021 13:59:26 +0200 Subject: [PATCH] Shorten SnowyConfigScreen in ConfigScreen.kt --- .../snowygui/config/ConfigScreen.kt | 147 +++++++++--------- 1 file changed, 72 insertions(+), 75 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 9e41c02..08451e8 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt @@ -24,86 +24,83 @@ 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() +val SnowyConfigScreen: Screen = 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 } +}; get() = 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( - 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 { cell -> + with(cell.split(":")) { + supplyComponent( + when { + isEmpty() || !cell.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 } ) - }.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 { cell -> - with(cell.split(":")) { - supplyComponent( - when { - isEmpty() || !cell.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) + } + }.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 + }; if (it.isEmpty().also { empty -> macrosBox.hidden = empty }) + macrosButtons.clear() + }.build() ) } - override fun onClose() { minecraft?.screen = configScreenParent } - } - } -} \ No newline at end of file + }.build() +} catch (e: ClassNotFoundException) { field } \ No newline at end of file