Skip to content

Commit

Permalink
session refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
anviaan committed Jul 28, 2024
1 parent acd3e96 commit 2d3b95a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 2d3b95a

Please sign in to comment.