Upload via Git Manager GUI - PlayerStats.java

This commit is contained in:
2026-04-01 10:14:29 +00:00
parent c278bb50d8
commit d41260f255

View File

@@ -1,70 +1,70 @@
package net.viper.status.stats; package net.viper.status.stats;
import java.util.UUID; import java.util.UUID;
public class PlayerStats { public class PlayerStats {
public final UUID uuid; public final UUID uuid;
public String name; public String name;
public long firstSeen; public long firstSeen;
public long lastSeen; public long lastSeen;
public long totalPlaytime; public long totalPlaytime;
public long currentSessionStart; public long currentSessionStart;
public int joins; public int joins;
public PlayerStats(UUID uuid, String name) { public PlayerStats(UUID uuid, String name) {
this.uuid = uuid; this.uuid = uuid;
this.name = name; this.name = name;
long now = System.currentTimeMillis() / 1000L; long now = System.currentTimeMillis() / 1000L;
this.firstSeen = now; this.firstSeen = now;
this.lastSeen = now; this.lastSeen = now;
this.totalPlaytime = 0; this.totalPlaytime = 0;
this.currentSessionStart = 0; this.currentSessionStart = 0;
this.joins = 0; this.joins = 0;
} }
public synchronized void onJoin() { public synchronized void onJoin() {
long now = System.currentTimeMillis() / 1000L; long now = System.currentTimeMillis() / 1000L;
if (this.currentSessionStart == 0) this.currentSessionStart = now; if (this.currentSessionStart == 0) this.currentSessionStart = now;
this.lastSeen = now; this.lastSeen = now;
this.joins++; this.joins++;
if (this.firstSeen == 0) this.firstSeen = now; if (this.firstSeen == 0) this.firstSeen = now;
} }
public synchronized void onQuit() { public synchronized void onQuit() {
long now = System.currentTimeMillis() / 1000L; long now = System.currentTimeMillis() / 1000L;
if (this.currentSessionStart > 0) { if (this.currentSessionStart > 0) {
long session = now - this.currentSessionStart; long session = now - this.currentSessionStart;
if (session > 0) this.totalPlaytime += session; if (session > 0) this.totalPlaytime += session;
this.currentSessionStart = 0; this.currentSessionStart = 0;
} }
this.lastSeen = now; this.lastSeen = now;
} }
public synchronized long getPlaytimeWithCurrentSession() { public synchronized long getPlaytimeWithCurrentSession() {
long now = System.currentTimeMillis() / 1000L; long now = System.currentTimeMillis() / 1000L;
if (this.currentSessionStart > 0) return totalPlaytime + (now - currentSessionStart); if (this.currentSessionStart > 0) return totalPlaytime + (now - currentSessionStart);
return totalPlaytime; return totalPlaytime;
} }
public synchronized String toLine() { public synchronized String toLine() {
return uuid + "|" + name.replace("|", "_") + "|" + firstSeen + "|" + lastSeen + "|" + totalPlaytime + "|" + currentSessionStart + "|" + joins; return uuid + "|" + name.replace("|", "_") + "|" + firstSeen + "|" + lastSeen + "|" + totalPlaytime + "|" + currentSessionStart + "|" + joins;
} }
public static PlayerStats fromLine(String line) { public static PlayerStats fromLine(String line) {
String[] parts = line.split("\\|", -1); String[] parts = line.split("\\|", -1);
if (parts.length < 7) return null; if (parts.length < 7) return null;
try { try {
UUID uuid = UUID.fromString(parts[0]); UUID uuid = UUID.fromString(parts[0]);
String name = parts[1]; String name = parts[1];
PlayerStats ps = new PlayerStats(uuid, name); PlayerStats ps = new PlayerStats(uuid, name);
ps.firstSeen = Long.parseLong(parts[2]); ps.firstSeen = Long.parseLong(parts[2]);
ps.lastSeen = Long.parseLong(parts[3]); ps.lastSeen = Long.parseLong(parts[3]);
ps.totalPlaytime = Long.parseLong(parts[4]); ps.totalPlaytime = Long.parseLong(parts[4]);
ps.currentSessionStart = Long.parseLong(parts[5]); ps.currentSessionStart = Long.parseLong(parts[5]);
ps.joins = Integer.parseInt(parts[6]); ps.joins = Integer.parseInt(parts[6]);
return ps; return ps;
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
} }
} }