From 80369478bc701b28f3a476dfbebcaaa73e0808e7 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 16 Aug 2024 17:09:00 +0530 Subject: [PATCH] feat: persist elytra toggle by saving config upon exit --- .../integrations/ModMenuIntegration.java | 3 ++- .../java/xyz/devcomp/elytralock/ElytraLock.java | 11 ++++++++--- .../devcomp/elytralock/config/ConfigHandler.java | 4 ++++ .../elytralock/events/ClientExitHandler.java | 14 ++++++++++++++ .../elytralock/events/ClientTickEndHandler.java | 4 +++- .../integrations/ModMenuIntegration.java | 4 ++-- 6 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/main/java/xyz/devcomp/elytralock/events/ClientExitHandler.java diff --git a/remappedSrc/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java b/remappedSrc/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java index 12253a4..9206e6e 100644 --- a/remappedSrc/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java +++ b/remappedSrc/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java @@ -1,5 +1,6 @@ package xyz.devcomp.elytralock.integrations; +import xyz.devcomp.elytralock.ElytraLock; import xyz.devcomp.elytralock.config.ConfigHandler; import xyz.devcomp.elytralock.config.ConfigUtil; @@ -12,7 +13,7 @@ public class ModMenuIntegration implements ModMenuApi { return (parent) -> { if (!ConfigUtil.isYaclLoaded()) return parent; - return new ConfigHandler().showGui(parent); + return ElytraLock.config.showGui(parent); }; } } diff --git a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java index 3be9cd0..51ec88e 100644 --- a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java +++ b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java @@ -1,7 +1,9 @@ package xyz.devcomp.elytralock; import xyz.devcomp.elytralock.config.ConfigHandler; +import xyz.devcomp.elytralock.config.ConfigModel; import xyz.devcomp.elytralock.config.ConfigUtil; +import xyz.devcomp.elytralock.events.ClientExitHandler; import xyz.devcomp.elytralock.events.ClientTickEndHandler; import xyz.devcomp.elytralock.events.HudRenderHandler; @@ -11,6 +13,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; @@ -26,6 +29,7 @@ public class ElytraLock implements ClientModInitializer { private static KeyBinding lockKeybind; private static boolean locked = false; public static MinecraftClient client; + public static ConfigHandler config; @Override public void onInitializeClient() { @@ -39,15 +43,16 @@ public class ElytraLock implements ClientModInitializer { if (ConfigUtil.isYaclLoaded()) { LOGGER.info("YACL_v3 is loaded, loading elytra toggle"); - locked = new ConfigHandler().getInstance().toggle; + config = new ConfigHandler(); + locked = config.getInstance().toggle; } else { LOGGER.warn("YACL_v3 is not loaded, not persisting elytra toggle"); } HudRenderCallback.EVENT.register(new HudRenderHandler()); ClientTickEvents.END_CLIENT_TICK.register(new ClientTickEndHandler()); - - LOGGER.info("Registered HUD_RENDER & END_CLIENT_TICK events successfully!"); + ClientLifecycleEvents.CLIENT_STOPPING.register(new ClientExitHandler()); + LOGGER.info("Registered HUD_RENDER, END_CLIENT_TICK and CLIENT_STOPPING events successfully!"); } public static boolean isLocked() { diff --git a/src/main/java/xyz/devcomp/elytralock/config/ConfigHandler.java b/src/main/java/xyz/devcomp/elytralock/config/ConfigHandler.java index 0d7bfc8..c7e7345 100644 --- a/src/main/java/xyz/devcomp/elytralock/config/ConfigHandler.java +++ b/src/main/java/xyz/devcomp/elytralock/config/ConfigHandler.java @@ -25,6 +25,10 @@ public class ConfigHandler { } } + public static void saveConfig() { + HANDLER.save(); + } + public Screen showGui(Screen parent) { this.loadConfig(); return HANDLER.generateGui().generateScreen(parent); diff --git a/src/main/java/xyz/devcomp/elytralock/events/ClientExitHandler.java b/src/main/java/xyz/devcomp/elytralock/events/ClientExitHandler.java new file mode 100644 index 0000000..b51651b --- /dev/null +++ b/src/main/java/xyz/devcomp/elytralock/events/ClientExitHandler.java @@ -0,0 +1,14 @@ +package xyz.devcomp.elytralock.events; + +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents.ClientStopping; +import net.minecraft.client.MinecraftClient; +import xyz.devcomp.elytralock.ElytraLock; +import xyz.devcomp.elytralock.config.ConfigHandler; + +public class ClientExitHandler implements ClientStopping { + @Override + public void onClientStopping(MinecraftClient client) { + ElytraLock.config.getInstance().toggle = ElytraLock.isLocked(); + ConfigHandler.saveConfig(); + } +} diff --git a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java index 870b3ec..1b008a4 100644 --- a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java +++ b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java @@ -28,6 +28,8 @@ public class ClientTickEndHandler implements EndTick { }); public void onEndTick(MinecraftClient client) { - impl.run(client); + if (client.player != null) { + impl.run(client); + } } } diff --git a/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java b/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java index 12253a4..1489a5e 100644 --- a/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java +++ b/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java @@ -1,6 +1,6 @@ package xyz.devcomp.elytralock.integrations; -import xyz.devcomp.elytralock.config.ConfigHandler; +import xyz.devcomp.elytralock.ElytraLock; import xyz.devcomp.elytralock.config.ConfigUtil; import com.terraformersmc.modmenu.api.ConfigScreenFactory; @@ -12,7 +12,7 @@ public class ModMenuIntegration implements ModMenuApi { return (parent) -> { if (!ConfigUtil.isYaclLoaded()) return parent; - return new ConfigHandler().showGui(parent); + return ElytraLock.config.showGui(parent); }; } }