From 2d3b95a26e6872dfc719d8277173fbbf6048e7c1 Mon Sep 17 00:00:00 2001 From: anviaan Date: Sun, 28 Jul 2024 00:29:46 -0500 Subject: [PATCH] session refactor --- .../SodiumExtraInformationClient.java | 78 +++++++------------ .../util/SessionManager.java | 62 +++++++++++++++ 2 files changed, 91 insertions(+), 49 deletions(-) create mode 100644 src/main/java/net/anvian/sodiumextrainformation/util/SessionManager.java diff --git a/src/main/java/net/anvian/sodiumextrainformation/SodiumExtraInformationClient.java b/src/main/java/net/anvian/sodiumextrainformation/SodiumExtraInformationClient.java index 2fb6b6e..7bfed96 100644 --- a/src/main/java/net/anvian/sodiumextrainformation/SodiumExtraInformationClient.java +++ b/src/main/java/net/anvian/sodiumextrainformation/SodiumExtraInformationClient.java @@ -1,9 +1,11 @@ package net.anvian.sodiumextrainformation; import net.anvian.sodiumextrainformation.options.SodiumExtraInformationGameOptions; +import net.anvian.sodiumextrainformation.util.SessionManager; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,76 +15,54 @@ public class SodiumExtraInformationClient implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); private static SodiumExtraInformationGameOptions CONFIG; - - private static long sessionStartTime; - private static long totalTimePlayed; - private static boolean inSession; - private static boolean isPaused; + public static final SessionManager SESSION_MANAGER = new SessionManager(); public static SodiumExtraInformationGameOptions options() { if (CONFIG == null) { CONFIG = loadConfig(); } - return CONFIG; } @Override public void onInitializeClient() { - LOGGER.info("Hello from " + MOD_NAME + "!"); + LOGGER.info("Hello from {}!", MOD_NAME); - sessionStartTime = 0; - totalTimePlayed = 0; - inSession = false; - isPaused = false; + SESSION_MANAGER.resetSession(); ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (client.world != null && !inSession) { - startSession(); - } else if (client.world == null && inSession) { - endSession(); - } - - if (inSession) { - if (client.isPaused()) { - if (!isPaused) { - pauseSession(); - } - } else { - if (isPaused) { - resumeSession(); - } else { - long currentTime = System.currentTimeMillis(); - totalTimePlayed += (currentTime - sessionStartTime); - sessionStartTime = currentTime; - } - } + try { + handleClientTick(client); + } catch (Exception e) { + LOGGER.error("Error during client tick", e); } }); } - private void startSession() { - sessionStartTime = System.currentTimeMillis(); - inSession = true; - isPaused = false; - } - - private void endSession() { - inSession = false; - } - - private void pauseSession() { - isPaused = true; - totalTimePlayed += (System.currentTimeMillis() - sessionStartTime); - } + private void handleClientTick(MinecraftClient client) { + if (client.world != null && !SESSION_MANAGER.isInSession()) { + SESSION_MANAGER.startSession(); + } else if (client.world == null && SESSION_MANAGER.isInSession()) { + SESSION_MANAGER.endSession(); + } - private void resumeSession() { - isPaused = false; - sessionStartTime = System.currentTimeMillis(); + if (SESSION_MANAGER.isInSession()) { + if (client.isPaused()) { + if (!SESSION_MANAGER.isPaused()) { + SESSION_MANAGER.pauseSession(); + } + } else { + if (SESSION_MANAGER.isPaused()) { + SESSION_MANAGER.resumeSession(); + } else { + SESSION_MANAGER.updateSessionTime(); + } + } + } } public static long getTotalTimePlayed() { - return totalTimePlayed / 1000; + return SESSION_MANAGER.getTotalTimePlayed(); } private static SodiumExtraInformationGameOptions loadConfig() { diff --git a/src/main/java/net/anvian/sodiumextrainformation/util/SessionManager.java b/src/main/java/net/anvian/sodiumextrainformation/util/SessionManager.java new file mode 100644 index 0000000..4908d91 --- /dev/null +++ b/src/main/java/net/anvian/sodiumextrainformation/util/SessionManager.java @@ -0,0 +1,62 @@ +package net.anvian.sodiumextrainformation.util; + +import net.anvian.sodiumextrainformation.SodiumExtraInformationClient; +import org.slf4j.Logger; + +public class SessionManager { + private static final Logger LOGGER = SodiumExtraInformationClient.LOGGER; + + private long sessionStartTime; + private long totalTimePlayed; + private boolean inSession; + private boolean isPaused; + + public synchronized void startSession() { + sessionStartTime = System.currentTimeMillis(); + inSession = true; + isPaused = false; + LOGGER.info("Session started"); + } + + public synchronized void endSession() { + inSession = false; + LOGGER.info("Session ended"); + } + + public synchronized void pauseSession() { + isPaused = true; + totalTimePlayed += (System.currentTimeMillis() - sessionStartTime); + LOGGER.info("Session paused"); + } + + public synchronized void resumeSession() { + isPaused = false; + sessionStartTime = System.currentTimeMillis(); + LOGGER.info("Session resumed"); + } + + public synchronized void updateSessionTime() { + long currentTime = System.currentTimeMillis(); + totalTimePlayed += (currentTime - sessionStartTime); + sessionStartTime = currentTime; + } + + public void resetSession() { + sessionStartTime = 0; + totalTimePlayed = 0; + inSession = false; + isPaused = false; + } + + public boolean isInSession() { + return inSession; + } + + public boolean isPaused() { + return isPaused; + } + + public long getTotalTimePlayed() { + return totalTimePlayed / 1000; + } +}