From d59cf8f9f0f12ec8c87a8607484d42810266ef26 Mon Sep 17 00:00:00 2001 From: moonleay Date: Thu, 13 Jun 2024 18:39:27 +0200 Subject: [PATCH] feat: add default mode setting --- .../net/moonleay/gimbal/client/ClientMain.kt | 3 ++ .../client/config/ClientConfigHolder.kt | 2 + .../client/config/GimbalClientConfig.kt | 2 + .../gimbal/client/editor/ClientEditor.kt | 11 +++- .../gimbal/client/screen/GimbalEditHudGui.kt | 3 +- .../gimbal/client/screen/GimbalSettingsGui.kt | 50 +++++++++++++++++-- .../gimbal/constants/TranslationKeys.kt | 1 + .../moonleay/gimbal/datagen/DataGenerator.kt | 2 +- ...ageProvider.kt => EnUsLanguageProvider.kt} | 3 +- .../moonleay/gimbal/editor/state/mode/Mode.kt | 13 ++--- .../gimbal/mixin/NormalModeMixin.java | 6 +-- 11 files changed, 78 insertions(+), 18 deletions(-) rename src/main/java/net/moonleay/gimbal/datagen/{En_us_GimbalLanguageProvider.kt => EnUsLanguageProvider.kt} (96%) diff --git a/src/main/java/net/moonleay/gimbal/client/ClientMain.kt b/src/main/java/net/moonleay/gimbal/client/ClientMain.kt index 2af8b59..d3f1823 100644 --- a/src/main/java/net/moonleay/gimbal/client/ClientMain.kt +++ b/src/main/java/net/moonleay/gimbal/client/ClientMain.kt @@ -46,6 +46,9 @@ internal object ClientMain : ClientModInitializer { val gimbalConfigPath = FabricLoader.getInstance().configDir.resolve(BuildConstants.modId + "/client.json") CONFIG = ClientConfigHolder(gimbalConfigPath) LOGGER.info("Config has been loaded.") + LOGGER.info("Applying Config to client.") + ClientEditor.applyConfig(CONFIG.config) + LOGGER.info("Config has been applied.") LOGGER.info("Gimbal has been initialized on the client side.") } diff --git a/src/main/java/net/moonleay/gimbal/client/config/ClientConfigHolder.kt b/src/main/java/net/moonleay/gimbal/client/config/ClientConfigHolder.kt index 0b7769e..4ad504d 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/ClientConfigHolder.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/ClientConfigHolder.kt @@ -22,6 +22,7 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.encodeToStream +import net.moonleay.gimbal.client.editor.ClientEditor import java.nio.file.Path import kotlin.io.path.* @@ -36,6 +37,7 @@ class ClientConfigHolder(private val path: Path) { fun updateConfig(new: GimbalClientConfig) { this.config = new this.save(config) + ClientEditor.applyConfig(config) } @OptIn(ExperimentalSerializationApi::class) diff --git a/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt b/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt index 8cd6fd9..f84b5f3 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt @@ -20,10 +20,12 @@ package net.moonleay.gimbal.client.config import kotlinx.serialization.Serializable import net.moonleay.gimbal.client.config.enums.ToastSettings +import net.moonleay.gimbal.editor.state.mode.Mode @Serializable data class GimbalClientConfig( val guiSettings: GimbalGuiSettings = GimbalGuiSettings(), val toastSettings: ToastSettings = ToastSettings.ALL, val playerFlySpeed: Int = 100, + val defaultMode: Mode = Mode.NORMAL ) diff --git a/src/main/java/net/moonleay/gimbal/client/editor/ClientEditor.kt b/src/main/java/net/moonleay/gimbal/client/editor/ClientEditor.kt index 303996f..00c908e 100644 --- a/src/main/java/net/moonleay/gimbal/client/editor/ClientEditor.kt +++ b/src/main/java/net/moonleay/gimbal/client/editor/ClientEditor.kt @@ -40,7 +40,7 @@ import org.apache.logging.log4j.LogManager object ClientEditor { private var POLICY = GimbalPolicyType.NOT_PRESENT - private var CURRENT_MODE = Mode.NORMAL + private lateinit var CURRENT_MODE: Mode private var TEMP_DISABLED_MODE = Mode.UNKNOWN private val CURRENT_MODE_MODIFIER = mutableListOf() @@ -50,6 +50,13 @@ object ClientEditor { private val LOGGER = LogManager.getLogger(BuildConstants.modName) + fun applyConfig(config: GimbalClientConfig, isBoot: Boolean = true) { + if (isBoot) { + this.CURRENT_MODE = config.defaultMode + return + } + this.setMode(config.defaultMode) + } fun onConnectedToNewWorld() { POLICY = GimbalPolicyType.NOT_PRESENT @@ -106,7 +113,7 @@ object ClientEditor { } fun isInNonDefaultMode(): Boolean { - return CURRENT_MODE != Mode.NORMAL + return CURRENT_MODE != ClientMain.CONFIG.config.defaultMode } /* diff --git a/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt b/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt index c0ec0be..3e84af2 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt @@ -87,7 +87,8 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig hudOptions = oldConf.guiSettings.hudOptions ), toastSettings = oldConf.toastSettings, - playerFlySpeed = oldConf.playerFlySpeed + playerFlySpeed = oldConf.playerFlySpeed, + defaultMode = oldConf.defaultMode ) cfg.updateConfig(newConf) diff --git a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt index 8e23ee9..f5e169f 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt @@ -32,6 +32,7 @@ import net.moonleay.gimbal.client.config.enums.ToastSettings import net.moonleay.gimbal.client.screen.widgets.GimbalSliderWidget import net.moonleay.gimbal.client.util.NumberUtil import net.moonleay.gimbal.constants.TranslationKeys +import net.moonleay.gimbal.editor.state.mode.Mode class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfigHolder) : Screen(Text.translatable(TranslationKeys.Gui.Config.SCREEN_NAME)) { @@ -39,11 +40,13 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi private var hudOptions: HudOptions private var toastSettings: ToastSettings private var playerFlySpeed: Int + private var defaultMode: Mode init { this.hudOptions = cfg.config.guiSettings.hudOptions this.toastSettings = cfg.config.toastSettings this.playerFlySpeed = cfg.config.playerFlySpeed + this.defaultMode = cfg.config.defaultMode } override fun init() { @@ -70,7 +73,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi val newConf = GimbalClientConfig( guiSettings = newGui, toastSettings = cfg.config.toastSettings, - playerFlySpeed = cfg.config.playerFlySpeed + playerFlySpeed = cfg.config.playerFlySpeed, + defaultMode = cfg.config.defaultMode ) cfg.updateConfig(newConf) }) @@ -129,7 +133,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi hudOptions = this.hudOptions ), toastSettings = oldConfig.toastSettings, - playerFlySpeed = oldConfig.playerFlySpeed + playerFlySpeed = oldConfig.playerFlySpeed, + defaultMode = oldConfig.defaultMode ) cfg.updateConfig(newConfig) } @@ -173,7 +178,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi val newConfig = GimbalClientConfig( guiSettings = oldConfig.guiSettings, toastSettings = this.toastSettings, - playerFlySpeed = oldConfig.playerFlySpeed + playerFlySpeed = oldConfig.playerFlySpeed, + defaultMode = oldConfig.defaultMode ) cfg.updateConfig(newConfig) } @@ -199,11 +205,47 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi val newConfig = GimbalClientConfig( guiSettings = oldConfig.guiSettings, toastSettings = this.toastSettings, - playerFlySpeed = this.playerFlySpeed + playerFlySpeed = this.playerFlySpeed, + defaultMode = oldConfig.defaultMode ) cfg.updateConfig(newConfig) }) + this.addDrawableChild>( + CyclingButtonWidget.builder { value: Mode? -> + if (value == null) { + return@builder Text.literal(Mode.UNKNOWN.displayName) + } + return@builder Text.literal(value.displayName) + } + .values(Mode.entries) + .initially(this.defaultMode) + .build( + this.width / 2 - 155 + 160, + this.height / 6 + 24 * 2, + 150, + 20, + Text.translatable(TranslationKeys.Gui.Config.DEFAULT_MODE) + ) { b: CyclingButtonWidget?, requestedMode: Mode -> + if (requestedMode.hidden){ + b!!.value = Mode.NORMAL + this.defaultMode = Mode.NORMAL + } + else { + this.defaultMode = requestedMode + } + + val oldConfig = cfg.config + val newConfig = GimbalClientConfig( + guiSettings = oldConfig.guiSettings, + toastSettings = oldConfig.toastSettings, + playerFlySpeed = oldConfig.playerFlySpeed, + defaultMode = this.defaultMode + ) + cfg.updateConfig(newConfig) + } + ) + // Done button this.addDrawableChild(ButtonWidget( this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE diff --git a/src/main/java/net/moonleay/gimbal/constants/TranslationKeys.kt b/src/main/java/net/moonleay/gimbal/constants/TranslationKeys.kt index d820f70..5aab860 100644 --- a/src/main/java/net/moonleay/gimbal/constants/TranslationKeys.kt +++ b/src/main/java/net/moonleay/gimbal/constants/TranslationKeys.kt @@ -32,6 +32,7 @@ object TranslationKeys { const val GENERIC_DISABLED = "${BASE}disabled" const val PLAYER_FLY_SPEED = "${BASE}flySpeed" + const val DEFAULT_MODE = "${BASE}defaultMode" object Hud { const val BASE = "${TranslationKeys.Gui.Config.BASE}hud." diff --git a/src/main/java/net/moonleay/gimbal/datagen/DataGenerator.kt b/src/main/java/net/moonleay/gimbal/datagen/DataGenerator.kt index b328d97..bd2452e 100644 --- a/src/main/java/net/moonleay/gimbal/datagen/DataGenerator.kt +++ b/src/main/java/net/moonleay/gimbal/datagen/DataGenerator.kt @@ -28,6 +28,6 @@ internal class DataGenerator : DataGeneratorEntrypoint { override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) { LOGGER.info("Starting Data Generation") - fabricDataGenerator.addProvider(En_us_GimbalLanguageProvider(fabricDataGenerator)) + fabricDataGenerator.addProvider(EnUsLanguageProvider(fabricDataGenerator)) } } diff --git a/src/main/java/net/moonleay/gimbal/datagen/En_us_GimbalLanguageProvider.kt b/src/main/java/net/moonleay/gimbal/datagen/EnUsLanguageProvider.kt similarity index 96% rename from src/main/java/net/moonleay/gimbal/datagen/En_us_GimbalLanguageProvider.kt rename to src/main/java/net/moonleay/gimbal/datagen/EnUsLanguageProvider.kt index 3251f74..2dc5d6b 100644 --- a/src/main/java/net/moonleay/gimbal/datagen/En_us_GimbalLanguageProvider.kt +++ b/src/main/java/net/moonleay/gimbal/datagen/EnUsLanguageProvider.kt @@ -22,7 +22,7 @@ import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider import net.moonleay.gimbal.constants.TranslationKeys -class En_us_GimbalLanguageProvider(dataGenerator: FabricDataGenerator?) : +class EnUsLanguageProvider(dataGenerator: FabricDataGenerator?) : FabricLanguageProvider(dataGenerator, "en_us") { override fun generateTranslations(translationBuilder: TranslationBuilder?) { @@ -56,6 +56,7 @@ class En_us_GimbalLanguageProvider(dataGenerator: FabricDataGenerator?) : // Gimbal Generic Settings translationBuilder.add(TranslationKeys.Gui.Config.PLAYER_FLY_SPEED, "Fly Speed") + translationBuilder.add(TranslationKeys.Gui.Config.DEFAULT_MODE, "Default Mode") // Gimbal Generic Gui translationBuilder.add(TranslationKeys.Gui.Config.GENERIC_ENABLED, "Enabled") diff --git a/src/main/java/net/moonleay/gimbal/editor/state/mode/Mode.kt b/src/main/java/net/moonleay/gimbal/editor/state/mode/Mode.kt index 157cfa3..5481b22 100644 --- a/src/main/java/net/moonleay/gimbal/editor/state/mode/Mode.kt +++ b/src/main/java/net/moonleay/gimbal/editor/state/mode/Mode.kt @@ -18,10 +18,11 @@ package net.moonleay.gimbal.editor.state.mode -enum class Mode(val displayName: String, val color: Int, val incompatibleModifiers: List){ - UNKNOWN("UNKNOWN", 0x000000, listOf()), // Unknown mode. This mode cannot be entered - NORMAL("NORMAL", 0x90a959, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing - INSERT("INSERT", 0xf4bf75, listOf()), // Place and break blocks - REPLACE("REPLACE", 0xac4242, listOf(ModeModifier.NO_UPDATES)), // Replace blocks - VISUAL("VISUAL", 0x6a9fb5, listOf(ModeModifier.BULLDOZER)), // Do fancy stuff with WE +enum class Mode(val displayName: String, val color: Int, val hidden: Boolean, val incompatibleModifiers: List){ + UNKNOWN("UNKNOWN", 0x000000, true, listOf()), // Unknown mode. This mode cannot be entered + NORMAL("NORMAL", 0x90a959, false, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing + INSERT("INSERT", 0xf4bf75, false, listOf()), // Place and break blocks + REPLACE("REPLACE", 0xac4242, false, listOf(ModeModifier.NO_UPDATES)), // Replace blocks + // Add hidden modes after this comment + VISUAL("VISUAL", 0x6a9fb5, true, listOf(ModeModifier.BULLDOZER)), // Do fancy stuff with WE } diff --git a/src/main/java/net/moonleay/gimbal/mixin/NormalModeMixin.java b/src/main/java/net/moonleay/gimbal/mixin/NormalModeMixin.java index 7dc6232..023efa8 100644 --- a/src/main/java/net/moonleay/gimbal/mixin/NormalModeMixin.java +++ b/src/main/java/net/moonleay/gimbal/mixin/NormalModeMixin.java @@ -21,9 +21,9 @@ package net.moonleay.gimbal.mixin; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.option.GameOptions; +import net.moonleay.gimbal.client.ClientMain; import net.moonleay.gimbal.client.editor.ClientEditor; import net.moonleay.gimbal.editor.state.mode.Capability; -import net.moonleay.gimbal.editor.state.mode.Mode; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -43,12 +43,12 @@ public class NormalModeMixin { public ClientPlayerEntity player; @Inject(method = "openPauseMenu", at = @At("HEAD"), cancellable = true) - private void setNormalMode(boolean pause, CallbackInfo ci) { + private void setDefaultMode(boolean pause, CallbackInfo ci) { if (ClientEditor.INSTANCE.isInNonDefaultMode() && ClientEditor.INSTANCE.isAllowed()) { assert this.player != null; if (this.player.isCreative()) { // Set the editor mode to normal - ClientEditor.INSTANCE.setMode(Mode.NORMAL); + ClientEditor.INSTANCE.setMode(ClientMain.CONFIG.getConfig().getDefaultMode()); ci.cancel(); } }