From cdc6cf7299d1588c6f18ef43f788cce2689bf594 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 6 May 2024 15:57:01 +0530 Subject: [PATCH] fix: load & apply config explicitly --- .../client/TouchGrassReminderClient.java | 13 +++++++++---- .../config/ConfigHandler.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/devcomp/touch_grass_reminder/client/TouchGrassReminderClient.java b/src/main/java/xyz/devcomp/touch_grass_reminder/client/TouchGrassReminderClient.java index 4dce3eb..ab08732 100644 --- a/src/main/java/xyz/devcomp/touch_grass_reminder/client/TouchGrassReminderClient.java +++ b/src/main/java/xyz/devcomp/touch_grass_reminder/client/TouchGrassReminderClient.java @@ -2,6 +2,7 @@ import java.util.UUID; +import xyz.devcomp.touch_grass_reminder.config.ConfigHandler; import xyz.devcomp.touch_grass_reminder.config.ConfigModel; import xyz.devcomp.touch_grass_reminder.utils.PlayDurationHandler; @@ -16,7 +17,7 @@ public class TouchGrassReminderClient implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger("Touch Grass Reminder"); - private ConfigModel config = new ConfigModel(); + private ConfigModel config = new ConfigHandler().getInstance(); private Thread thread; @Override @@ -28,12 +29,16 @@ public void onInitializeClient(ModContainer mod) { if (config.isEnabled) { ClientPlayConnectionEvents.JOIN.register((net, packet, client) -> { UUID sessionId = net.getId(); - ServerData serverInfo = net.getServerData() == null ? new ServerData("Unknown", "Unknown", false) - : net.getServerData(); + ServerData serverInfo = net.getServerData(); - LOGGER.info( + if (serverInfo != null) { + LOGGER.info( "Player initiated connection; sessionId={}, name={}, version={}, protocolVersion={}, isLocal={}", sessionId, serverInfo.name, serverInfo.version, serverInfo.protocol, serverInfo.isLan()); + } else { + LOGGER.warn("Could not get details about joined server."); + } + PlayDurationHandler worker = new PlayDurationHandler(client, System.currentTimeMillis(), config.reminderFrequency * 60 * 60 * 1000); diff --git a/src/main/java/xyz/devcomp/touch_grass_reminder/config/ConfigHandler.java b/src/main/java/xyz/devcomp/touch_grass_reminder/config/ConfigHandler.java index c785be9..cf7b0af 100644 --- a/src/main/java/xyz/devcomp/touch_grass_reminder/config/ConfigHandler.java +++ b/src/main/java/xyz/devcomp/touch_grass_reminder/config/ConfigHandler.java @@ -2,6 +2,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.resources.ResourceLocation; +import xyz.devcomp.touch_grass_reminder.client.TouchGrassReminderClient; import org.quiltmc.loader.api.QuiltLoader; @@ -9,6 +10,7 @@ import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder; public class ConfigHandler { + private boolean isLoaded = false; public static final ConfigClassHandler HANDLER = ConfigClassHandler.createBuilder(ConfigModel.class) .id(new ResourceLocation("touchgrassreminder", "config")) .serializer(config -> GsonConfigSerializerBuilder.create(config) @@ -17,7 +19,20 @@ public class ConfigHandler { .build()) .build(); + private void loadConfig() { + if (!this.isLoaded) { + this.isLoaded = HANDLER.load(); + TouchGrassReminderClient.LOGGER.info("Config not loaded. Attempting to load."); + } + } + public Screen showGui(Screen parent) { + this.loadConfig(); return HANDLER.generateGui().generateScreen(parent); } + + public ConfigModel getInstance() { + this.loadConfig(); + return HANDLER.instance(); + } }