From fad2c7455b15f8b72c961a1574d1f143ec8ac5e4 Mon Sep 17 00:00:00 2001 From: plastoid501 Date: Thu, 18 Jul 2024 23:38:50 +0900 Subject: [PATCH] v1.0.1 --- build.gradle | 4 +- gradle.properties | 5 +- .../plastoid501/movement/MovementInGUI.java | 7 +- .../plastoid501/movement/config/Configs.java | 35 ++++ .../movement/config/ModConfig.java | 22 +++ .../movement/config/ToggleConfig.java | 30 ++++ .../movement/config/json/JToggleConfig.java | 17 ++ .../movement/gui/ConfigScreen.java | 57 ++++++ .../movement/gui/ModMenuIntegration.java | 13 ++ .../movement/gui/widget/ConfigWidget.java | 170 ++++++++++++++++++ .../mixin/ClientPlayerEntityMixin.java | 32 +++- .../movement/mixin/KeyboardInputMixin.java | 31 +++- .../plastoid501/movement/util/FileUtil.java | 133 ++++++++++++++ src/main/resources/fabric.mod.json | 3 + 14 files changed, 547 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/plastoid501/movement/config/Configs.java create mode 100644 src/main/java/net/plastoid501/movement/config/ModConfig.java create mode 100644 src/main/java/net/plastoid501/movement/config/ToggleConfig.java create mode 100644 src/main/java/net/plastoid501/movement/config/json/JToggleConfig.java create mode 100644 src/main/java/net/plastoid501/movement/gui/ConfigScreen.java create mode 100644 src/main/java/net/plastoid501/movement/gui/ModMenuIntegration.java create mode 100644 src/main/java/net/plastoid501/movement/gui/widget/ConfigWidget.java create mode 100644 src/main/java/net/plastoid501/movement/util/FileUtil.java diff --git a/build.gradle b/build.gradle index ef65712..9c9291c 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,8 @@ repositories { // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. // See https://docs.gradle.org/current/userguide/declaring_repositories.html // for more information about repositories. + + maven { url "https://maven.terraformersmc.com/releases/" } } dependencies { @@ -26,7 +28,7 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - + modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}" } processResources { diff --git a/gradle.properties b/gradle.properties index 5c49dd5..e1d1028 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,9 +9,10 @@ yarn_mappings=1.19.4+build.2 loader_version=0.15.11 # Mod Properties -mod_version=1.0.0 +mod_version=1.0.1 maven_group=net.plastoid501.movement archives_base_name=movement-in-gui-mc1.19.x # Dependencies -fabric_version=0.87.2+1.19.4 \ No newline at end of file +fabric_version=0.87.2+1.19.4 +modmenu_version=6.3.1 \ No newline at end of file diff --git a/src/main/java/net/plastoid501/movement/MovementInGUI.java b/src/main/java/net/plastoid501/movement/MovementInGUI.java index b134833..fc466f1 100644 --- a/src/main/java/net/plastoid501/movement/MovementInGUI.java +++ b/src/main/java/net/plastoid501/movement/MovementInGUI.java @@ -1,7 +1,7 @@ package net.plastoid501.movement; import net.fabricmc.api.ModInitializer; - +import net.plastoid501.movement.util.FileUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,13 +9,14 @@ public class MovementInGUI implements ModInitializer { // This logger is used to write text to the console and the log file. // It is considered best practice to use your mod id as the logger's name. // That way, it's clear which mod wrote info, warnings, and errors. - public static final Logger LOGGER = LoggerFactory.getLogger("movement-in-gui"); + public static final String MOD_ID = "movement-in-gui"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. - + FileUtil.generateClientModConfig(); } } \ No newline at end of file diff --git a/src/main/java/net/plastoid501/movement/config/Configs.java b/src/main/java/net/plastoid501/movement/config/Configs.java new file mode 100644 index 0000000..5888f25 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/config/Configs.java @@ -0,0 +1,35 @@ +package net.plastoid501.movement.config; + +import net.plastoid501.movement.config.json.JToggleConfig; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class Configs { + public static Map toggles = new LinkedHashMap<>(); + public static Map jToggles = new LinkedHashMap<>(); + + public static ToggleConfig modEnable = new ToggleConfig("modEnable", "If true, this mod is enable", true); + public static ToggleConfig inCreative = new ToggleConfig("inCreative", "If true, this mod is enable when creative mode.", true); + + public static ModConfig config; + + static { + toggles.put(modEnable.getId(), modEnable); + toggles.put(inCreative.getId(), inCreative); + jToggles.put(modEnable.getId(), new JToggleConfig(modEnable.isEnable())); + jToggles.put(inCreative.getId(), new JToggleConfig(inCreative.isEnable())); + + config = new ModConfig(jToggles); + + } + + + public static Map getToggles() { + return toggles; + } + + public static Map getJToggles() { + return jToggles; + } +} diff --git a/src/main/java/net/plastoid501/movement/config/ModConfig.java b/src/main/java/net/plastoid501/movement/config/ModConfig.java new file mode 100644 index 0000000..1311043 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/config/ModConfig.java @@ -0,0 +1,22 @@ +package net.plastoid501.movement.config; + +import net.plastoid501.movement.config.json.JToggleConfig; + +import java.util.Map; + +public class ModConfig { + private Map Toggles; + + public ModConfig(Map Toggles) { + this.Toggles = Toggles; + } + + public Map getToggles() { + return Toggles; + } + + public void setToggles(Map toggles) { + Toggles = toggles; + } +} + diff --git a/src/main/java/net/plastoid501/movement/config/ToggleConfig.java b/src/main/java/net/plastoid501/movement/config/ToggleConfig.java new file mode 100644 index 0000000..cf95561 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/config/ToggleConfig.java @@ -0,0 +1,30 @@ +package net.plastoid501.movement.config; + +import net.plastoid501.movement.config.json.JToggleConfig; + +public class ToggleConfig extends JToggleConfig { + private String id; + private String narrator; + + public ToggleConfig(String id, String narrator, Boolean enable) { + super(enable); + this.id = id; + this.narrator = narrator; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNarrator() { + return narrator; + } + + public void setNarrator(String narrator) { + this.narrator = narrator; + } +} diff --git a/src/main/java/net/plastoid501/movement/config/json/JToggleConfig.java b/src/main/java/net/plastoid501/movement/config/json/JToggleConfig.java new file mode 100644 index 0000000..25bdf47 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/config/json/JToggleConfig.java @@ -0,0 +1,17 @@ +package net.plastoid501.movement.config.json; + +public class JToggleConfig { + private boolean enable; + + public JToggleConfig(boolean enable) { + this.enable = enable; + } + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } +} diff --git a/src/main/java/net/plastoid501/movement/gui/ConfigScreen.java b/src/main/java/net/plastoid501/movement/gui/ConfigScreen.java new file mode 100644 index 0000000..fecefc4 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/gui/ConfigScreen.java @@ -0,0 +1,57 @@ +package net.plastoid501.movement.gui; + +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.screen.ScreenTexts; +import net.minecraft.text.Text; +import net.plastoid501.movement.gui.widget.ConfigWidget; + +public class ConfigScreen extends Screen { + private ConfigWidget configList; + private final Screen parent; + + public ConfigScreen(Screen parent) { + super(Text.literal("Movement In GUI")); + this.parent = parent; + } + + @Override + protected void init() { + this.configList = new ConfigWidget(this, this.client); + this.addSelectableChild(this.configList); + this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (button) -> { + this.close(); + }).dimensions(this.width / 2 - 100, this.height - 27, 200, 20).build()); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + this.configList.render(matrices, mouseX, mouseY, delta); + drawCenteredTextWithShadow(matrices, this.textRenderer, this.title, this.width / 2, 8, 0xFFFFFF); + super.render(matrices, mouseX, mouseY, delta); + } + + @Override + public boolean shouldPause() { + return false; + } + + @Override + public void close() { + if (this.client != null) { + this.client.setScreen(this.parent); + } + } + + /* + @Override + public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) { + if (this.client == null || this.client.player == null || this.client.world == null) { + this.renderBackgroundTexture(context); + } + } + + */ + +} diff --git a/src/main/java/net/plastoid501/movement/gui/ModMenuIntegration.java b/src/main/java/net/plastoid501/movement/gui/ModMenuIntegration.java new file mode 100644 index 0000000..56c703c --- /dev/null +++ b/src/main/java/net/plastoid501/movement/gui/ModMenuIntegration.java @@ -0,0 +1,13 @@ +package net.plastoid501.movement.gui; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.plastoid501.movement.util.FileUtil; + +public class ModMenuIntegration implements ModMenuApi { + @Override + public ConfigScreenFactory getModConfigScreenFactory() { + FileUtil.updateConfigs(); + return ConfigScreen::new; + } +} diff --git a/src/main/java/net/plastoid501/movement/gui/widget/ConfigWidget.java b/src/main/java/net/plastoid501/movement/gui/widget/ConfigWidget.java new file mode 100644 index 0000000..7116cdf --- /dev/null +++ b/src/main/java/net/plastoid501/movement/gui/widget/ConfigWidget.java @@ -0,0 +1,170 @@ +package net.plastoid501.movement.gui.widget; + +import com.google.common.collect.ImmutableList; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.client.gui.widget.TextWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.plastoid501.movement.config.Configs; +import net.plastoid501.movement.config.ModConfig; +import net.plastoid501.movement.config.ToggleConfig; +import net.plastoid501.movement.config.json.JToggleConfig; +import net.plastoid501.movement.gui.ConfigScreen; +import net.plastoid501.movement.util.FileUtil; + +import java.awt.*; +import java.util.List; + + +@Environment(EnvType.CLIENT) +public class ConfigWidget extends ElementListWidget { + final ConfigScreen parent; + private final MinecraftClient client; + private final ModConfig CONFIG = Configs.config; + + public ConfigWidget(ConfigScreen parent, MinecraftClient client) { + super(client, parent.width + 155, parent.height, 20, parent.height - 32, 23); + //super(client, parent.width + 155, parent.height - 52, 20, 23); + this.parent = parent; + this.client = client; + this.initEntries(client); + } + + private void initEntries(MinecraftClient client) { + if (CONFIG != null) { + this.addEntry(new CategoryEntry(Text.literal("-- Toggle --"), client.textRenderer)); + Configs.getToggles().keySet().forEach((key) -> this.addEntry(new ToggleEntry(key, client.textRenderer, CONFIG))); + this.addEntry(new CategoryEntry(Text.literal(""), client.textRenderer)); + } + } + + @Override + protected int getScrollbarPositionX() { + return super.getScrollbarPositionX() + 85; + } + + public int getRowWidth() { + return super.getRowWidth() + 150; + } + + public void update() { + this.updateChildren(); + } + + public void updateChildren() { + this.children().forEach(Entry::update); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + if (this.client == null || this.client.player == null || this.client.world == null) { + this.setRenderBackground(true); + } else { + this.setRenderBackground(false); + } + super.render(matrices, mouseX, mouseY, delta); + } + + public class CategoryEntry extends Entry { + private final TextWidget text; + private final int textWidth; + CategoryEntry(Text CategoryName, TextRenderer textRenderer) { + this.text = new TextWidget(CategoryName, textRenderer); + this.textWidth = this.text.getWidth(); + } + + @Override + public List children() { + return ImmutableList.of(this.text); + } + + @Override + public List selectableChildren() { + return ImmutableList.of(this.text); + } + + @Override + public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + this.text.setPosition(ConfigWidget.this.client.currentScreen.width / 2 - this.textWidth / 2, y + 5); + this.text.render(matrices, mouseX, mouseY, tickDelta); + } + + @Override + void update() { + } + + } + + public class ToggleEntry extends Entry { + private final ToggleConfig defaultConfig; + private boolean enable; + private final TextWidget text; + private final ButtonWidget enableButton; + private final ButtonWidget resetButton; + + ToggleEntry(String key, TextRenderer textRenderer, ModConfig config) { + this.defaultConfig = Configs.getToggles().get(key); + this.enable = config.getToggles().get(key).isEnable(); + this.text = new TextWidget(Text.literal(key), textRenderer); + this.text.setTooltip(Tooltip.of(Text.literal(this.defaultConfig.getNarrator()))); + this.enableButton = ButtonWidget.builder(Text.literal(this.enable ? "ON" : "OFF").setStyle(Style.EMPTY.withColor(this.enable ? Color.GREEN.getRGB() : Color.red.getRGB())), button -> { + this.enable = !this.enable; + FileUtil.updateToggleConfig(key, new JToggleConfig(this.enable)); + this.update(); + }).size(60, 20).build(); + this.resetButton = ButtonWidget.builder(Text.literal("RESET"), button -> { + this.enable = this.defaultConfig.isEnable(); + FileUtil.updateToggleConfig(key, new JToggleConfig(this.enable)); + this.update(); + }).size(40, 20).build(); + this.resetButton.active = this.defaultConfig.isEnable() != this.enable; + + } + + @Override + public List children() { + return ImmutableList.of(this.text, this.enableButton, this.resetButton); + } + + @Override + public List selectableChildren() { + return ImmutableList.of(this.text, this.enableButton, this.resetButton); + } + + @Override + public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + this.text.setPosition(x - 60, y + 5); + this.text.render(matrices, mouseX, mouseY, tickDelta); + this.enableButton.setPosition(x + 190, y); + this.enableButton.render(matrices, mouseX, mouseY, tickDelta); + this.resetButton.setPosition(x + 253, y); + this.resetButton.render(matrices, mouseX, mouseY, tickDelta); + } + + @Override + void update() { + this.enableButton.setMessage(Text.literal(this.enable ? "ON" : "OFF").setStyle(Style.EMPTY.withColor(this.enable ? Color.GREEN.getRGB() : Color.red.getRGB()))); + this.resetButton.active = this.defaultConfig.isEnable() != this.enable; + } + + } + + @Environment(EnvType.CLIENT) + public abstract static class Entry extends ElementListWidget.Entry { + public Entry() { + } + + abstract void update(); + } +} + + diff --git a/src/main/java/net/plastoid501/movement/mixin/ClientPlayerEntityMixin.java b/src/main/java/net/plastoid501/movement/mixin/ClientPlayerEntityMixin.java index 0b71a1a..052770c 100644 --- a/src/main/java/net/plastoid501/movement/mixin/ClientPlayerEntityMixin.java +++ b/src/main/java/net/plastoid501/movement/mixin/ClientPlayerEntityMixin.java @@ -1,13 +1,22 @@ package net.plastoid501.movement.mixin; +import com.terraformersmc.modmenu.gui.ModsScreen; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.*; +import net.minecraft.client.gui.screen.option.CreditsAndAttributionScreen; +import net.minecraft.client.gui.screen.option.GameOptionsScreen; +import net.minecraft.client.gui.screen.option.OptionsScreen; +import net.minecraft.client.gui.screen.option.TelemetryInfoScreen; +import net.minecraft.client.gui.screen.pack.PackScreen; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +import net.plastoid501.movement.config.Configs; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; + @Mixin(ClientPlayerEntity.class) public class ClientPlayerEntityMixin { @Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/KeyBinding;isPressed()Z")) @@ -15,16 +24,33 @@ private boolean modifyTickMovement(KeyBinding instance) { if (instance.isPressed()) { return true; } + if (!Configs.modEnable.isEnable()) { + return false; + } MinecraftClient client = MinecraftClient.getInstance(); - if (client.currentScreen == null) { + if (client.currentScreen == null || + client.currentScreen instanceof ChatScreen || + client.currentScreen instanceof GameOptionsScreen || + client.currentScreen instanceof GameMenuScreen || + client.currentScreen instanceof OptionsScreen || + client.currentScreen instanceof TelemetryInfoScreen || + client.currentScreen instanceof StatsScreen || + client.currentScreen instanceof OpenToLanScreen || + client.currentScreen instanceof ConfirmLinkScreen || + client.currentScreen instanceof PackScreen || + client.currentScreen instanceof CreditsAndAttributionScreen || + client.currentScreen instanceof CreditsScreen || + client.currentScreen instanceof ModsScreen + ) { + return false; + } + if (!Configs.inCreative.isEnable() && client.player != null && client.player.isCreative()) { return false; } InputUtil.Key key = ((IKeyBindingMixin) instance).getBoundKey(); if (!InputUtil.isKeyPressed(client.getWindow().getHandle(), key.getCode())) { return false; } - KeyBinding.setKeyPressed(key, true); - KeyBinding.onKeyPressed(key); return true; } diff --git a/src/main/java/net/plastoid501/movement/mixin/KeyboardInputMixin.java b/src/main/java/net/plastoid501/movement/mixin/KeyboardInputMixin.java index f35b76d..b81ca75 100644 --- a/src/main/java/net/plastoid501/movement/mixin/KeyboardInputMixin.java +++ b/src/main/java/net/plastoid501/movement/mixin/KeyboardInputMixin.java @@ -1,9 +1,17 @@ package net.plastoid501.movement.mixin; +import com.terraformersmc.modmenu.gui.ModsScreen; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.*; +import net.minecraft.client.gui.screen.option.CreditsAndAttributionScreen; +import net.minecraft.client.gui.screen.option.GameOptionsScreen; +import net.minecraft.client.gui.screen.option.OptionsScreen; +import net.minecraft.client.gui.screen.option.TelemetryInfoScreen; +import net.minecraft.client.gui.screen.pack.PackScreen; import net.minecraft.client.input.KeyboardInput; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +import net.plastoid501.movement.config.Configs; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; @@ -15,16 +23,33 @@ private boolean modifyTick(KeyBinding instance) { if (instance.isPressed()) { return true; } + if (!Configs.modEnable.isEnable()) { + return false; + } MinecraftClient client = MinecraftClient.getInstance(); - if (client.currentScreen == null) { + if (client.currentScreen == null || + client.currentScreen instanceof ChatScreen || + client.currentScreen instanceof GameOptionsScreen || + client.currentScreen instanceof GameMenuScreen || + client.currentScreen instanceof OptionsScreen || + client.currentScreen instanceof TelemetryInfoScreen || + client.currentScreen instanceof StatsScreen || + client.currentScreen instanceof OpenToLanScreen || + client.currentScreen instanceof ConfirmLinkScreen || + client.currentScreen instanceof PackScreen || + client.currentScreen instanceof CreditsAndAttributionScreen || + client.currentScreen instanceof CreditsScreen || + client.currentScreen instanceof ModsScreen + ) { + return false; + } + if (!Configs.inCreative.isEnable() && client.player != null && client.player.isCreative()) { return false; } InputUtil.Key key = ((IKeyBindingMixin) instance).getBoundKey(); if (!InputUtil.isKeyPressed(client.getWindow().getHandle(), key.getCode())) { return false; } - KeyBinding.setKeyPressed(key, true); - KeyBinding.onKeyPressed(key); return true; } diff --git a/src/main/java/net/plastoid501/movement/util/FileUtil.java b/src/main/java/net/plastoid501/movement/util/FileUtil.java new file mode 100644 index 0000000..1936e85 --- /dev/null +++ b/src/main/java/net/plastoid501/movement/util/FileUtil.java @@ -0,0 +1,133 @@ +package net.plastoid501.movement.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; +import net.fabricmc.loader.api.FabricLoader; +import net.plastoid501.movement.MovementInGUI; +import net.plastoid501.movement.config.Configs; +import net.plastoid501.movement.config.ModConfig; +import net.plastoid501.movement.config.ToggleConfig; +import net.plastoid501.movement.config.json.JToggleConfig; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +public class FileUtil { + public static Path getConfigPath() { + return FabricLoader.getInstance().getConfigDir(); + } + + public static void generateClientModConfig() { + Path path = FileUtil.getConfigPath().resolve(MovementInGUI.MOD_ID + ".json"); + if (Files.exists(path)){ + return; + } + + Map toggles = Configs.getJToggles(); + + ModConfig config = new ModConfig(toggles); + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String json = gson.toJson(config); + + + try (FileWriter writer = new FileWriter(path.toString())) { + writer.write(json); + } catch (IOException e) { + MovementInGUI.LOGGER.error(e.getMessage()); + } + } + + public static void generateClientModConfig(ModConfig config) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String json = gson.toJson(config); + + Path path = FileUtil.getConfigPath().resolve(MovementInGUI.MOD_ID + ".json"); + + if (Files.notExists(path)){ + try (FileWriter writer = new FileWriter(path.toString())) { + writer.write(json); + } catch (IOException e) { + MovementInGUI.LOGGER.error(e.getMessage()); + } + } + } + + public static ModConfig readConfig() { + Path path = FileUtil.getConfigPath().resolve(MovementInGUI.MOD_ID + ".json"); + Gson gson = new Gson(); + ModConfig config; + + try { + String jsonContent = Files.readString(path); + config = gson.fromJson(jsonContent, ModConfig.class); + } catch (IOException | JsonSyntaxException e) { + MovementInGUI.LOGGER.error(e.getMessage()); + return Configs.config; + } + + if (config == null) { + return Configs.config; + } + + return config; + } + + public static void saveConfig(ModConfig config) { + Path path = FileUtil.getConfigPath().resolve(MovementInGUI.MOD_ID + ".json"); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + try (Writer writer = Files.newBufferedWriter(path)) { + gson.toJson(config, writer); + } catch (IOException e) { + MovementInGUI.LOGGER.error(e.getMessage()); + } + + FileUtil.updateConfigs(); + } + + public static void updateToggleConfig(String target, JToggleConfig toggle) { + ModConfig config = readConfig(); + Map toggleConfigMap = config.getToggles(); + toggleConfigMap.replace(target, toggle); + saveConfig(config); + } + + public static void updateConfigs() { + ModConfig config = FileUtil.readConfig(); + if (config != null) { + if (config.getToggles() == null) { + return; + } + + boolean flag = false; + + JToggleConfig toggleConfig = config.getToggles().get(Configs.modEnable.getId()); + if (toggleConfig == null) { + flag = true; + toggleConfig = new JToggleConfig(Configs.modEnable.isEnable()); + config.getToggles().put(Configs.modEnable.getId(), toggleConfig); + } + Configs.modEnable = new ToggleConfig(Configs.modEnable.getId(), Configs.modEnable.getNarrator(), toggleConfig.isEnable()); + + toggleConfig = config.getToggles().get(Configs.inCreative.getId()); + if (toggleConfig == null) { + flag = true; + toggleConfig = new JToggleConfig(Configs.inCreative.isEnable()); + config.getToggles().put(Configs.inCreative.getId(), toggleConfig); + } + Configs.inCreative = new ToggleConfig(Configs.inCreative.getId(), Configs.inCreative.getNarrator(), toggleConfig.isEnable()); + + if (flag) { + return; + } + Configs.config = config; + + } + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index a1474d0..266ea46 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,6 +17,9 @@ "entrypoints": { "main": [ "net.plastoid501.movement.MovementInGUI" + ], + "modmenu": [ + "net.plastoid501.movement.gui.ModMenuIntegration" ] }, "mixins": [