feat: persist elytra toggle by saving config upon exit

This commit is contained in:
Erica Marigold 2024-08-16 17:09:00 +05:30
parent 0a82274e84
commit 80369478bc
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
6 changed files with 33 additions and 7 deletions

View file

@ -1,5 +1,6 @@
package xyz.devcomp.elytralock.integrations; package xyz.devcomp.elytralock.integrations;
import xyz.devcomp.elytralock.ElytraLock;
import xyz.devcomp.elytralock.config.ConfigHandler; import xyz.devcomp.elytralock.config.ConfigHandler;
import xyz.devcomp.elytralock.config.ConfigUtil; import xyz.devcomp.elytralock.config.ConfigUtil;
@ -12,7 +13,7 @@ public class ModMenuIntegration implements ModMenuApi {
return (parent) -> { return (parent) -> {
if (!ConfigUtil.isYaclLoaded()) if (!ConfigUtil.isYaclLoaded())
return parent; return parent;
return new ConfigHandler().showGui(parent); return ElytraLock.config.showGui(parent);
}; };
} }
} }

View file

@ -1,7 +1,9 @@
package xyz.devcomp.elytralock; package xyz.devcomp.elytralock;
import xyz.devcomp.elytralock.config.ConfigHandler; import xyz.devcomp.elytralock.config.ConfigHandler;
import xyz.devcomp.elytralock.config.ConfigModel;
import xyz.devcomp.elytralock.config.ConfigUtil; import xyz.devcomp.elytralock.config.ConfigUtil;
import xyz.devcomp.elytralock.events.ClientExitHandler;
import xyz.devcomp.elytralock.events.ClientTickEndHandler; import xyz.devcomp.elytralock.events.ClientTickEndHandler;
import xyz.devcomp.elytralock.events.HudRenderHandler; import xyz.devcomp.elytralock.events.HudRenderHandler;
@ -11,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.fabricmc.api.ClientModInitializer; 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.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
@ -26,6 +29,7 @@ public class ElytraLock implements ClientModInitializer {
private static KeyBinding lockKeybind; private static KeyBinding lockKeybind;
private static boolean locked = false; private static boolean locked = false;
public static MinecraftClient client; public static MinecraftClient client;
public static ConfigHandler config;
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
@ -39,15 +43,16 @@ public class ElytraLock implements ClientModInitializer {
if (ConfigUtil.isYaclLoaded()) { if (ConfigUtil.isYaclLoaded()) {
LOGGER.info("YACL_v3 is loaded, loading elytra toggle"); LOGGER.info("YACL_v3 is loaded, loading elytra toggle");
locked = new ConfigHandler().getInstance().toggle; config = new ConfigHandler();
locked = config.getInstance().toggle;
} else { } else {
LOGGER.warn("YACL_v3 is not loaded, not persisting elytra toggle"); LOGGER.warn("YACL_v3 is not loaded, not persisting elytra toggle");
} }
HudRenderCallback.EVENT.register(new HudRenderHandler()); HudRenderCallback.EVENT.register(new HudRenderHandler());
ClientTickEvents.END_CLIENT_TICK.register(new ClientTickEndHandler()); ClientTickEvents.END_CLIENT_TICK.register(new ClientTickEndHandler());
ClientLifecycleEvents.CLIENT_STOPPING.register(new ClientExitHandler());
LOGGER.info("Registered HUD_RENDER & END_CLIENT_TICK events successfully!"); LOGGER.info("Registered HUD_RENDER, END_CLIENT_TICK and CLIENT_STOPPING events successfully!");
} }
public static boolean isLocked() { public static boolean isLocked() {

View file

@ -25,6 +25,10 @@ public class ConfigHandler {
} }
} }
public static void saveConfig() {
HANDLER.save();
}
public Screen showGui(Screen parent) { public Screen showGui(Screen parent) {
this.loadConfig(); this.loadConfig();
return HANDLER.generateGui().generateScreen(parent); return HANDLER.generateGui().generateScreen(parent);

View file

@ -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();
}
}

View file

@ -28,6 +28,8 @@ public class ClientTickEndHandler implements EndTick {
}); });
public void onEndTick(MinecraftClient client) { public void onEndTick(MinecraftClient client) {
if (client.player != null) {
impl.run(client); impl.run(client);
} }
}
} }

View file

@ -1,6 +1,6 @@
package xyz.devcomp.elytralock.integrations; package xyz.devcomp.elytralock.integrations;
import xyz.devcomp.elytralock.config.ConfigHandler; import xyz.devcomp.elytralock.ElytraLock;
import xyz.devcomp.elytralock.config.ConfigUtil; import xyz.devcomp.elytralock.config.ConfigUtil;
import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ConfigScreenFactory;
@ -12,7 +12,7 @@ public class ModMenuIntegration implements ModMenuApi {
return (parent) -> { return (parent) -> {
if (!ConfigUtil.isYaclLoaded()) if (!ConfigUtil.isYaclLoaded())
return parent; return parent;
return new ConfigHandler().showGui(parent); return ElytraLock.config.showGui(parent);
}; };
} }
} }