85 lines
3.0 KiB
Java
85 lines
3.0 KiB
Java
package me.viper.teamplugin.manager;
|
|
|
|
import me.viper.teamplugin.Main;
|
|
import me.viper.teamplugin.util.Utils;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Writes and reads audit entries in log.yml.
|
|
*
|
|
* Entry format:
|
|
* 2026-03-08T12:00:00Z | ADD | by:AdminName | Steve → Owner
|
|
*
|
|
* Supported action strings (use the constants below for consistency):
|
|
* ADD, REMOVE, MOVE, APPLY_ACCEPT, APPLY_DENY
|
|
*/
|
|
public class AuditLog {
|
|
|
|
public static final String ADD = "ADD";
|
|
public static final String REMOVE = "REMOVE";
|
|
public static final String MOVE = "MOVE";
|
|
public static final String APPLY_ACCEPT = "APPLY_ACCEPT";
|
|
public static final String APPLY_DENY = "APPLY_DENY";
|
|
|
|
private static File file;
|
|
private static FileConfiguration cfg;
|
|
|
|
// ── File management ───────────────────────────────────────────────
|
|
|
|
private static void ensureLoaded() {
|
|
if (cfg != null) return;
|
|
file = new File(Main.getInstance().getDataFolder(), "log.yml");
|
|
try { if (!file.exists()) file.createNewFile(); }
|
|
catch (IOException e) { e.printStackTrace(); }
|
|
cfg = YamlConfiguration.loadConfiguration(file);
|
|
}
|
|
|
|
private static void save() {
|
|
try { if (cfg != null && file != null) cfg.save(file); }
|
|
catch (IOException e) { e.printStackTrace(); }
|
|
}
|
|
|
|
public static void reload() { cfg = null; }
|
|
|
|
// ── API ───────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Appends an audit entry.
|
|
*
|
|
* @param action one of the constants above, e.g. AuditLog.ADD
|
|
* @param performedBy name of the command sender (player or "CONSOLE")
|
|
* @param detail human-readable summary, e.g. "Steve → Owner"
|
|
*/
|
|
public static void log(String action, String performedBy, String detail) {
|
|
ensureLoaded();
|
|
String entry = Utils.formatIsoNow()
|
|
+ " | " + action
|
|
+ " | by:" + performedBy
|
|
+ " | " + detail;
|
|
|
|
List<String> entries = new ArrayList<>(cfg.getStringList("entries"));
|
|
entries.add(entry);
|
|
|
|
int max = Main.getInstance().getConfig().getInt("audit.keep", 500);
|
|
if (entries.size() > max) entries = entries.subList(entries.size() - max, entries.size());
|
|
|
|
cfg.set("entries", entries);
|
|
save();
|
|
}
|
|
|
|
/**
|
|
* Returns the last {@code limit} entries, most recent last.
|
|
*/
|
|
public static List<String> getEntries(int limit) {
|
|
ensureLoaded();
|
|
List<String> all = cfg.getStringList("entries");
|
|
int from = Math.max(0, all.size() - limit);
|
|
return new ArrayList<>(all.subList(from, all.size()));
|
|
}
|
|
} |