Files
ServerLite/src/main/java/de/npid7/serverlite/Configs/PlayerConfig.java

135 lines
3.3 KiB
Java
Raw Normal View History

2024-11-23 19:14:39 +01:00
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;
2024-11-23 19:14:39 +01:00
public static class PlayerEntry {
public String status = "";
public String name = "";
public boolean timer = false;
public PlayerEntry() {
}
public PlayerEntry(String s, String n, boolean t) {
2024-11-23 19:14:39 +01:00
status = s;
name = n;
timer = t;
2024-11-23 19:14:39 +01:00
}
}
HashMap<UUID, PlayerEntry> 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<String, JsonElement> 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();
}
2024-11-23 19:14:39 +01:00
player_map.put(key, pe);
}
if (pver != cfg_version) {
// Use PVER to update Config
Save();
return false;
}
2024-11-23 19:14:39 +01:00
} catch (IOException e) {
e.printStackTrace();
return true;
}
return false;
}
public boolean Save() {
JsonObject js = new JsonObject();
js.addProperty("version", cfg_version);
for (Map.Entry<UUID, PlayerEntry> 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);
2024-11-23 19:14:39 +01:00
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;
}
2024-11-23 19:14:39 +01:00
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;
}
2024-11-23 19:14:39 +01:00
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;
}
2024-11-23 19:14:39 +01:00
}