package de.npid7.serverlite.Configs; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class PlayerConfig { File file = null; int cfg_version = 1; public static class PlayerEntry { public String status = ""; public String name = ""; public boolean timer = false; public PlayerEntry() { } public PlayerEntry(String s, String n, boolean t) { status = s; name = n; timer = t; } } HashMap player_map = new HashMap<>(); public PlayerConfig(String path) { file = new File(path); Load(); } public boolean Load() { if (!file.exists()) { return false; } try { FileReader r = new FileReader(file); JsonParser prs = new JsonParser(); JsonObject js = prs.parse(r).getAsJsonObject(); r.close(); int pver = js.get("version").getAsInt(); player_map.clear(); // Load Data for (Map.Entry e : js.entrySet()) { if (e.getKey().equals("version")) { continue; } UUID key = UUID.fromString(e.getKey()); PlayerEntry pe = new PlayerEntry(); pe.name = e.getValue().getAsJsonObject().get("name").getAsString(); pe.status = e.getValue().getAsJsonObject().get("status").getAsString(); if (pver >= 1) { pe.timer = e.getValue().getAsJsonObject().get("timer").getAsBoolean(); } player_map.put(key, pe); } if (pver != cfg_version) { // Use PVER to update Config Save(); return false; } } catch (IOException e) { e.printStackTrace(); return true; } return false; } public boolean Save() { JsonObject js = new JsonObject(); js.addProperty("version", cfg_version); for (Map.Entry e : player_map.entrySet()) { JsonObject o = new JsonObject(); o.addProperty("name", e.getValue().name); o.addProperty("status", e.getValue().status); o.addProperty("timer", e.getValue().timer); js.add(e.getKey().toString(), o); } try { FileWriter w = new FileWriter(file); Gson g = new GsonBuilder().setPrettyPrinting().create(); g.toJson(js, w); w.close(); } catch (IOException e) { e.printStackTrace(); return true; } return false; } public PlayerEntry Find(UUID ref) { if (player_map.containsKey(ref)) { return player_map.get(ref); } return null; } public void Add(UUID k, PlayerEntry e) { player_map.put(k, e); } public String getStatus(UUID ref) { return this.player_map.get(ref).status; } public void setStatus(UUID ref, String value) { this.player_map.get(ref).status = value; } public String getName(UUID ref) { return this.player_map.get(ref).name; } public void setName(UUID ref, String value) { this.player_map.get(ref).name = value; } public boolean getTimer(UUID ref) { return this.player_map.get(ref).timer; } public void setTimer(UUID ref, boolean value) { this.player_map.get(ref).timer = value; } }