81 lines
2.7 KiB
Java
81 lines
2.7 KiB
Java
package org.blz.adminmode.moderation;
|
||
|
||
import org.blz.adminmode.config.ConfigManager;
|
||
import org.bukkit.plugin.Plugin;
|
||
|
||
import java.io.File;
|
||
import java.io.FileWriter;
|
||
import java.io.IOException;
|
||
import java.time.Duration;
|
||
import java.time.LocalDateTime;
|
||
import java.time.format.DateTimeFormatter;
|
||
|
||
public class ModeSessionLogger {
|
||
|
||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||
private final Plugin plugin;
|
||
private final ConfigManager configManager;
|
||
|
||
public ModeSessionLogger(Plugin plugin, ConfigManager configManager) {
|
||
this.plugin = plugin;
|
||
this.configManager = configManager;
|
||
}
|
||
|
||
public void logEnter(ModeSession session) {
|
||
writeLine("ENTER_" + modeSuffix(session), session, "-");
|
||
}
|
||
|
||
public void logExit(ModeSession session) {
|
||
writeLine("EXIT_" + modeSuffix(session), session, formatDuration(session.getDurationMillis()));
|
||
}
|
||
|
||
public void logRecovered(ModeSession session) {
|
||
writeLine("RECOVER_" + modeSuffix(session), session, formatDuration(session.getDurationMillis()));
|
||
}
|
||
|
||
private void writeLine(String action, ModeSession session, String duration) {
|
||
if (!configManager.isSessionLoggingEnabled()) {
|
||
return;
|
||
}
|
||
|
||
File logFile = new File(plugin.getDataFolder(), configManager.getSessionLogFile());
|
||
File parent = logFile.getParentFile();
|
||
if (parent != null && !parent.exists()) {
|
||
parent.mkdirs();
|
||
}
|
||
|
||
String line = String.format(
|
||
"[%s] %s | Player: %s | UUID: %s | Duration: %s%n",
|
||
LocalDateTime.now().format(DATE_TIME_FORMATTER),
|
||
action,
|
||
session.getPlayerName(),
|
||
session.getPlayerId(),
|
||
duration);
|
||
|
||
try (FileWriter writer = new FileWriter(logFile, true)) {
|
||
writer.write(line);
|
||
} catch (IOException exception) {
|
||
plugin.getLogger().warning("Не удалось записать сессионный лог: " + exception.getMessage());
|
||
}
|
||
}
|
||
|
||
private String modeSuffix(ModeSession session) {
|
||
return session.getProfile().isHardProfile() ? "HARD" : "SOFT";
|
||
}
|
||
|
||
private String formatDuration(long millis) {
|
||
Duration duration = Duration.ofMillis(Math.max(0L, millis));
|
||
long hours = duration.toHours();
|
||
long minutes = duration.toMinutesPart();
|
||
long seconds = duration.toSecondsPart();
|
||
|
||
if (hours > 0) {
|
||
return hours + "h " + minutes + "m " + seconds + "s";
|
||
}
|
||
if (minutes > 0) {
|
||
return minutes + "m " + seconds + "s";
|
||
}
|
||
return seconds + "s";
|
||
}
|
||
}
|