SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt
Username404 c14ef7bef1
Remove "Risky Cheats"
Signed-off-by: Username404 <w.iron.zombie@gmail.com>
2022-03-25 18:22:13 +01:00

113 lines
6.6 KiB
Kotlin

package fr.username404.snowygui.config
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.utils.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.Keystrokes
import fr.username404.snowygui.gui.feature.Zoom
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TranslatableComponent
import java.util.Optional
private const val confPrefix: String = "screen.snowy.config"
private val translationComponent = TranslatableComponent(confPrefix)
var configScreenParent: Screen? = null
private fun supplyComponent(string: String?): Optional<Component> = string?.run {
Optional.of(TranslatableComponent(string))
} ?: Optional.empty()
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 ?: LinkedHashSet<Macro>()) as MutableSet<Macro>
fun Collection<Macro>.getTitleCommand(): MutableList<String> = 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 {
addAll(
setOf(
startBooleanToggle(TranslatableComponent("$confPrefix.behavior.sortalphabetically"), sortAlphabetically)
.setDefaultValue(true).requireRestart()
.setSaveConsumer { sortAlphabetically = it }.build(),
startDoubleField(TranslatableComponent("$confPrefix.behavior.zoom.factor"), Zoom.zoomFactor).setSaveConsumer {
Zoom.zoomFactor = it
}.setMin(1.1).build(),
startBooleanToggle(TranslatableComponent("$confPrefix.behavior.zoom.smoothcamera"), Zoom.smoothCameraOnZoom).setSaveConsumer {
Zoom.smoothCameraOnZoom = it
Zoom.execAction()
}.build(),
startEnumSelector(TranslatableComponent("$confPrefix.behavior.keystrokes.position"), Keystrokes.Position::class.java, Keystrokes.Position.values().find {
it.value == Keystrokes.currentPosition
}).setSaveConsumer {
Keystrokes.currentPosition = it.value
}.build()
)
)
}.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
}
)
}
}.setTooltip(TranslatableComponent("$confPrefix.general.macros.tooltip")).setSaveConsumer { it.forEach { string ->
with(string.split(":")) {
if (size == 2) Macro(title = component1().trimStart(), command = component2().trim()).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 (_: ClassNotFoundException) { field }