feat: add default mode setting

This commit is contained in:
moonleay 2024-06-13 18:39:27 +02:00
parent 5252d9abdf
commit d59cf8f9f0
Signed by: moonleay
GPG key ID: 82667543CCD715FB
11 changed files with 78 additions and 18 deletions

View file

@ -46,6 +46,9 @@ internal object ClientMain : ClientModInitializer {
val gimbalConfigPath = FabricLoader.getInstance().configDir.resolve(BuildConstants.modId + "/client.json") val gimbalConfigPath = FabricLoader.getInstance().configDir.resolve(BuildConstants.modId + "/client.json")
CONFIG = ClientConfigHolder(gimbalConfigPath) CONFIG = ClientConfigHolder(gimbalConfigPath)
LOGGER.info("Config has been loaded.") 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.") LOGGER.info("Gimbal has been initialized on the client side.")
} }

View file

@ -22,6 +22,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream import kotlinx.serialization.json.encodeToStream
import net.moonleay.gimbal.client.editor.ClientEditor
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.* import kotlin.io.path.*
@ -36,6 +37,7 @@ class ClientConfigHolder(private val path: Path) {
fun updateConfig(new: GimbalClientConfig) { fun updateConfig(new: GimbalClientConfig) {
this.config = new this.config = new
this.save(config) this.save(config)
ClientEditor.applyConfig(config)
} }
@OptIn(ExperimentalSerializationApi::class) @OptIn(ExperimentalSerializationApi::class)

View file

@ -20,10 +20,12 @@ package net.moonleay.gimbal.client.config
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.moonleay.gimbal.client.config.enums.ToastSettings import net.moonleay.gimbal.client.config.enums.ToastSettings
import net.moonleay.gimbal.editor.state.mode.Mode
@Serializable @Serializable
data class GimbalClientConfig( data class GimbalClientConfig(
val guiSettings: GimbalGuiSettings = GimbalGuiSettings(), val guiSettings: GimbalGuiSettings = GimbalGuiSettings(),
val toastSettings: ToastSettings = ToastSettings.ALL, val toastSettings: ToastSettings = ToastSettings.ALL,
val playerFlySpeed: Int = 100, val playerFlySpeed: Int = 100,
val defaultMode: Mode = Mode.NORMAL
) )

View file

@ -40,7 +40,7 @@ import org.apache.logging.log4j.LogManager
object ClientEditor { object ClientEditor {
private var POLICY = GimbalPolicyType.NOT_PRESENT 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 var TEMP_DISABLED_MODE = Mode.UNKNOWN
private val CURRENT_MODE_MODIFIER = mutableListOf<ModeModifier>() private val CURRENT_MODE_MODIFIER = mutableListOf<ModeModifier>()
@ -50,6 +50,13 @@ object ClientEditor {
private val LOGGER = LogManager.getLogger(BuildConstants.modName) 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() { fun onConnectedToNewWorld() {
POLICY = GimbalPolicyType.NOT_PRESENT POLICY = GimbalPolicyType.NOT_PRESENT
@ -106,7 +113,7 @@ object ClientEditor {
} }
fun isInNonDefaultMode(): Boolean { fun isInNonDefaultMode(): Boolean {
return CURRENT_MODE != Mode.NORMAL return CURRENT_MODE != ClientMain.CONFIG.config.defaultMode
} }
/* /*

View file

@ -87,7 +87,8 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig
hudOptions = oldConf.guiSettings.hudOptions hudOptions = oldConf.guiSettings.hudOptions
), ),
toastSettings = oldConf.toastSettings, toastSettings = oldConf.toastSettings,
playerFlySpeed = oldConf.playerFlySpeed playerFlySpeed = oldConf.playerFlySpeed,
defaultMode = oldConf.defaultMode
) )
cfg.updateConfig(newConf) cfg.updateConfig(newConf)

View file

@ -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.screen.widgets.GimbalSliderWidget
import net.moonleay.gimbal.client.util.NumberUtil import net.moonleay.gimbal.client.util.NumberUtil
import net.moonleay.gimbal.constants.TranslationKeys import net.moonleay.gimbal.constants.TranslationKeys
import net.moonleay.gimbal.editor.state.mode.Mode
class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfigHolder) : class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfigHolder) :
Screen(Text.translatable(TranslationKeys.Gui.Config.SCREEN_NAME)) { 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 hudOptions: HudOptions
private var toastSettings: ToastSettings private var toastSettings: ToastSettings
private var playerFlySpeed: Int private var playerFlySpeed: Int
private var defaultMode: Mode
init { init {
this.hudOptions = cfg.config.guiSettings.hudOptions this.hudOptions = cfg.config.guiSettings.hudOptions
this.toastSettings = cfg.config.toastSettings this.toastSettings = cfg.config.toastSettings
this.playerFlySpeed = cfg.config.playerFlySpeed this.playerFlySpeed = cfg.config.playerFlySpeed
this.defaultMode = cfg.config.defaultMode
} }
override fun init() { override fun init() {
@ -70,7 +73,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi
val newConf = GimbalClientConfig( val newConf = GimbalClientConfig(
guiSettings = newGui, guiSettings = newGui,
toastSettings = cfg.config.toastSettings, toastSettings = cfg.config.toastSettings,
playerFlySpeed = cfg.config.playerFlySpeed playerFlySpeed = cfg.config.playerFlySpeed,
defaultMode = cfg.config.defaultMode
) )
cfg.updateConfig(newConf) cfg.updateConfig(newConf)
}) })
@ -129,7 +133,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi
hudOptions = this.hudOptions hudOptions = this.hudOptions
), ),
toastSettings = oldConfig.toastSettings, toastSettings = oldConfig.toastSettings,
playerFlySpeed = oldConfig.playerFlySpeed playerFlySpeed = oldConfig.playerFlySpeed,
defaultMode = oldConfig.defaultMode
) )
cfg.updateConfig(newConfig) cfg.updateConfig(newConfig)
} }
@ -173,7 +178,8 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi
val newConfig = GimbalClientConfig( val newConfig = GimbalClientConfig(
guiSettings = oldConfig.guiSettings, guiSettings = oldConfig.guiSettings,
toastSettings = this.toastSettings, toastSettings = this.toastSettings,
playerFlySpeed = oldConfig.playerFlySpeed playerFlySpeed = oldConfig.playerFlySpeed,
defaultMode = oldConfig.defaultMode
) )
cfg.updateConfig(newConfig) cfg.updateConfig(newConfig)
} }
@ -199,11 +205,47 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi
val newConfig = GimbalClientConfig( val newConfig = GimbalClientConfig(
guiSettings = oldConfig.guiSettings, guiSettings = oldConfig.guiSettings,
toastSettings = this.toastSettings, toastSettings = this.toastSettings,
playerFlySpeed = this.playerFlySpeed playerFlySpeed = this.playerFlySpeed,
defaultMode = oldConfig.defaultMode
) )
cfg.updateConfig(newConfig) cfg.updateConfig(newConfig)
}) })
this.addDrawableChild<CyclingButtonWidget<Mode>>(
CyclingButtonWidget.builder<Mode> { 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<Mode>?, 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 // Done button
this.addDrawableChild(ButtonWidget( this.addDrawableChild(ButtonWidget(
this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE

View file

@ -32,6 +32,7 @@ object TranslationKeys {
const val GENERIC_DISABLED = "${BASE}disabled" const val GENERIC_DISABLED = "${BASE}disabled"
const val PLAYER_FLY_SPEED = "${BASE}flySpeed" const val PLAYER_FLY_SPEED = "${BASE}flySpeed"
const val DEFAULT_MODE = "${BASE}defaultMode"
object Hud { object Hud {
const val BASE = "${TranslationKeys.Gui.Config.BASE}hud." const val BASE = "${TranslationKeys.Gui.Config.BASE}hud."

View file

@ -28,6 +28,6 @@ internal class DataGenerator : DataGeneratorEntrypoint {
override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) { override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) {
LOGGER.info("Starting Data Generation") LOGGER.info("Starting Data Generation")
fabricDataGenerator.addProvider(En_us_GimbalLanguageProvider(fabricDataGenerator)) fabricDataGenerator.addProvider(EnUsLanguageProvider(fabricDataGenerator))
} }
} }

View file

@ -22,7 +22,7 @@ import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider
import net.moonleay.gimbal.constants.TranslationKeys import net.moonleay.gimbal.constants.TranslationKeys
class En_us_GimbalLanguageProvider(dataGenerator: FabricDataGenerator?) : class EnUsLanguageProvider(dataGenerator: FabricDataGenerator?) :
FabricLanguageProvider(dataGenerator, "en_us") { FabricLanguageProvider(dataGenerator, "en_us") {
override fun generateTranslations(translationBuilder: TranslationBuilder?) { override fun generateTranslations(translationBuilder: TranslationBuilder?) {
@ -56,6 +56,7 @@ class En_us_GimbalLanguageProvider(dataGenerator: FabricDataGenerator?) :
// Gimbal Generic Settings // Gimbal Generic Settings
translationBuilder.add(TranslationKeys.Gui.Config.PLAYER_FLY_SPEED, "Fly Speed") translationBuilder.add(TranslationKeys.Gui.Config.PLAYER_FLY_SPEED, "Fly Speed")
translationBuilder.add(TranslationKeys.Gui.Config.DEFAULT_MODE, "Default Mode")
// Gimbal Generic Gui // Gimbal Generic Gui
translationBuilder.add(TranslationKeys.Gui.Config.GENERIC_ENABLED, "Enabled") translationBuilder.add(TranslationKeys.Gui.Config.GENERIC_ENABLED, "Enabled")

View file

@ -18,10 +18,11 @@
package net.moonleay.gimbal.editor.state.mode package net.moonleay.gimbal.editor.state.mode
enum class Mode(val displayName: String, val color: Int, val incompatibleModifiers: List<ModeModifier>){ enum class Mode(val displayName: String, val color: Int, val hidden: Boolean, val incompatibleModifiers: List<ModeModifier>){
UNKNOWN("UNKNOWN", 0x000000, listOf()), // Unknown mode. This mode cannot be entered UNKNOWN("UNKNOWN", 0x000000, true, listOf()), // Unknown mode. This mode cannot be entered
NORMAL("NORMAL", 0x90a959, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing NORMAL("NORMAL", 0x90a959, false, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing
INSERT("INSERT", 0xf4bf75, listOf()), // Place and break blocks INSERT("INSERT", 0xf4bf75, false, listOf()), // Place and break blocks
REPLACE("REPLACE", 0xac4242, listOf(ModeModifier.NO_UPDATES)), // Replace blocks REPLACE("REPLACE", 0xac4242, false, listOf(ModeModifier.NO_UPDATES)), // Replace blocks
VISUAL("VISUAL", 0x6a9fb5, listOf(ModeModifier.BULLDOZER)), // Do fancy stuff with WE // Add hidden modes after this comment
VISUAL("VISUAL", 0x6a9fb5, true, listOf(ModeModifier.BULLDOZER)), // Do fancy stuff with WE
} }

View file

@ -21,9 +21,9 @@ package net.moonleay.gimbal.mixin;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.GameOptions;
import net.moonleay.gimbal.client.ClientMain;
import net.moonleay.gimbal.client.editor.ClientEditor; import net.moonleay.gimbal.client.editor.ClientEditor;
import net.moonleay.gimbal.editor.state.mode.Capability; import net.moonleay.gimbal.editor.state.mode.Capability;
import net.moonleay.gimbal.editor.state.mode.Mode;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@ -43,12 +43,12 @@ public class NormalModeMixin {
public ClientPlayerEntity player; public ClientPlayerEntity player;
@Inject(method = "openPauseMenu", at = @At("HEAD"), cancellable = true) @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()) { if (ClientEditor.INSTANCE.isInNonDefaultMode() && ClientEditor.INSTANCE.isAllowed()) {
assert this.player != null; assert this.player != null;
if (this.player.isCreative()) { if (this.player.isCreative()) {
// Set the editor mode to normal // Set the editor mode to normal
ClientEditor.INSTANCE.setMode(Mode.NORMAL); ClientEditor.INSTANCE.setMode(ClientMain.CONFIG.getConfig().getDefaultMode());
ci.cancel(); ci.cancel();
} }
} }