package moe.nea.firmament.gui.config import com.mojang.serialization.Codec import io.github.notenoughupdates.moulconfig.gui.CloseEventListener import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper import io.github.notenoughupdates.moulconfig.gui.GuiContext import io.github.notenoughupdates.moulconfig.gui.component.CenterComponent import io.github.notenoughupdates.moulconfig.gui.component.ColumnComponent import io.github.notenoughupdates.moulconfig.gui.component.PanelComponent import io.github.notenoughupdates.moulconfig.gui.component.RowComponent import io.github.notenoughupdates.moulconfig.gui.component.ScrollPanelComponent import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import moe.nea.jarvis.api.Point import org.lwjgl.glfw.GLFW import kotlinx.serialization.encodeToString import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject import kotlin.io.path.createDirectories import kotlin.io.path.readText import kotlin.io.path.writeText import kotlin.time.Duration import net.minecraft.client.gui.screen.Screen import net.minecraft.text.Text import net.minecraft.util.StringIdentifiable import moe.nea.firmament.Firmament import moe.nea.firmament.gui.FirmButtonComponent import moe.nea.firmament.keybindings.SavedKeyBinding import moe.nea.firmament.util.ScreenUtil.setScreenLater import moe.nea.firmament.util.collections.InstanceList abstract class ManagedConfig( override val name: String, val category: Category, // TODO: allow vararg secondaryCategories: Category, ) : ManagedConfigElement() { enum class Category { // Böse Kategorie, nicht benutzten lol MISC, CHAT, INVENTORY, MINING, EVENTS, INTEGRATIONS, META, DEV, ; val labelText: Text = Text.translatable("firmament.config.category.${name.lowercase()}") val description: Text = Text.translatable("firmament.config.category.${name.lowercase()}.description") val configs: MutableList = mutableListOf() } companion object { val allManagedConfigs = InstanceList("ManagedConfig") } interface OptionHandler { fun initOption(opt: ManagedOption) {} fun toJson(element: T): JsonElement? fun fromJson(element: JsonElement): T fun emitGuiElements(opt: ManagedOption, guiAppender: GuiAppender) } init { allManagedConfigs.getAll().forEach { require(it.name != name) { "Duplicate name '$name' used for config" } } allManagedConfigs.add(this) category.configs.add(this) } val file = Firmament.CONFIG_DIR.resolve("$name.json") val data: JsonObject by lazy { try { Firmament.json.decodeFromString( file.readText() ) } catch (e: Exception) { Firmament.logger.info("Could not read config $name. Loading empty config.") JsonObject(mutableMapOf()) } } fun save() { val data = JsonObject(allOptions.mapNotNull { (key, value) -> value.toJson()?.let { key to it } }.toMap()) file.parent.createDirectories() file.writeText(Firmament.json.encodeToString(data)) } val allOptions = mutableMapOf>() val sortedOptions = mutableListOf>() private var latestGuiAppender: GuiAppender? = null protected fun option( propertyName: String, default: () -> T, handler: OptionHandler ): ManagedOption { if (propertyName in allOptions) error("Cannot register the same name twice") return ManagedOption(this, propertyName, default, handler).also { it.handler.initOption(it) it.load(data) allOptions[propertyName] = it sortedOptions.add(it) } } protected fun toggle(propertyName: String, default: () -> Boolean): ManagedOption { return option(propertyName, default, BooleanHandler(this)) } protected fun choice( propertyName: String, universe: List, default: () -> E ): ManagedOption where E : Enum, E : StringIdentifiable { return option(propertyName, default, ChoiceHandler(universe)) } // TODO: wait on https://youtrack.jetbrains.com/issue/KT-73434 // protected inline fun choice( // propertyName: String, // noinline default: () -> E // ): ManagedOption where E : Enum, E : StringIdentifiable { // return choice( // propertyName, // enumEntries().toList(), // StringIdentifiable.createCodec { enumValues() }, // EnumRenderer.default(), // default // ) // } protected fun duration( propertyName: String, min: Duration, max: Duration, default: () -> Duration, ): ManagedOption { return option(propertyName, default, DurationHandler(this, min, max)) } protected fun position( propertyName: String, width: Int, height: Int, default: () -> Point, ): ManagedOption { val label = Text.translatable("firmament.config.${name}.${propertyName}") return option(propertyName, { val p = default() HudMeta(HudPosition(p.x, p.y, 1F), label, width, height) }, HudMetaHandler(this, label, width, height)) } protected fun keyBinding( propertyName: String, default: () -> Int, ): ManagedOption = keyBindingWithOutDefaultModifiers(propertyName) { SavedKeyBinding(default()) } protected fun keyBindingWithOutDefaultModifiers( propertyName: String, default: () -> SavedKeyBinding, ): ManagedOption { return option(propertyName, default, KeyBindingHandler("firmament.config.${name}.${propertyName}", this)) } protected fun keyBindingWithDefaultUnbound( propertyName: String, ): ManagedOption { return keyBindingWithOutDefaultModifiers(propertyName) { SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN) } } protected fun integer( propertyName: String, min: Int, max: Int, default: () -> Int, ): ManagedOption { return option(propertyName, default, IntegerHandler(this, min, max)) } protected fun button(propertyName: String, runnable: () -> Unit): ManagedOption { return option(propertyName, { }, ClickHandler(this, runnable)) } protected fun string(propertyName: String, default: () -> String): ManagedOption { return option(propertyName, default, StringHandler(this)) } fun reloadGui() { latestGuiAppender?.reloadables?.forEach { it() } } val translationKey get() = "firmament.config.${name}" val labelText: Text = Text.translatable(translationKey) fun getConfigEditor(parent: Screen? = null): Screen { var screen: Screen? = null val guiapp = GuiAppender(400) { requireNotNull(screen) { "Screen Accessor called too early" } } latestGuiAppender = guiapp guiapp.appendFullRow(RowComponent( FirmButtonComponent(TextComponent("←")) { if (parent != null) { save() setScreenLater(parent) } else { AllConfigsGui.showAllGuis() } } )) sortedOptions.forEach { it.appendToGui(guiapp) } guiapp.reloadables.forEach { it() } val component = CenterComponent(PanelComponent(ScrollPanelComponent(400, 300, ColumnComponent(guiapp.panel)), 10, PanelComponent.DefaultBackgroundRenderer.VANILLA)) screen = object : GuiComponentWrapper(GuiContext(component)) { override fun close() { if (context.onBeforeClose() == CloseEventListener.CloseAction.NO_OBJECTIONS_TO_CLOSE) { client!!.setScreen(parent) } } } return screen } fun showConfigEditor(parent: Screen? = null) { setScreenLater(getConfigEditor(parent)) } }