This commit is contained in:
116
src/main/java/de/npid7/serverlite/Configs/PlayerConfig.java
Normal file
116
src/main/java/de/npid7/serverlite/Configs/PlayerConfig.java
Normal file
@@ -0,0 +1,116 @@
|
||||
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 = 0;
|
||||
|
||||
public static class PlayerEntry {
|
||||
public String status = "";
|
||||
public String name = "";
|
||||
public PlayerEntry() {}
|
||||
public PlayerEntry(String s, String n) {
|
||||
status = s;
|
||||
name = n;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
if (pver != cfg_version) {
|
||||
// Use PVER to not create a complete new config
|
||||
file.delete();
|
||||
return false;
|
||||
}
|
||||
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();
|
||||
player_map.put(key, pe);
|
||||
}
|
||||
} 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
142
src/main/java/de/npid7/serverlite/Configs/PluginConfig.java
Normal file
142
src/main/java/de/npid7/serverlite/Configs/PluginConfig.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package de.npid7.serverlite.Configs;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import de.npid7.serverlite.Helpers.Wordlist;
|
||||
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.Vector;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class PluginConfig {
|
||||
File file = null;
|
||||
|
||||
int cfg_version = 0;
|
||||
Wordlist wordBlacklist = new Wordlist();
|
||||
|
||||
String defaultStatus = "";
|
||||
// Maybe the name is missleading but i refer to the docs for that
|
||||
Vector<String> defaultStatusList = new Vector<>();
|
||||
HashMap<String, String> tablistStatusColors = new HashMap<>();
|
||||
|
||||
public PluginConfig(String path) {
|
||||
file = new File(path);
|
||||
Load();
|
||||
}
|
||||
|
||||
public boolean Load() {
|
||||
tablistStatusColors.clear();
|
||||
if (!file.exists()) {
|
||||
CreateNew(0);
|
||||
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();
|
||||
if (pver != cfg_version) {
|
||||
// Use PVER to not create a complete new config
|
||||
CreateNew(pver);
|
||||
return false;
|
||||
}
|
||||
wordBlacklist.fromJsonArray(js.get("word_blacklist").getAsJsonArray());
|
||||
defaultStatus = js.get("default_status").getAsString();
|
||||
JsonArray default_status = js.get("default_status_list").getAsJsonArray();
|
||||
defaultStatusList.clear();
|
||||
for (JsonElement e : default_status) {
|
||||
defaultStatusList.add(e.getAsString());
|
||||
}
|
||||
JsonObject color_map =
|
||||
js.get("tablist").getAsJsonObject().get("status_colors").getAsJsonObject();
|
||||
for (Map.Entry<String, JsonElement> e : color_map.entrySet()) {
|
||||
tablistStatusColors.put(e.getKey(), color_map.get(e.getKey()).getAsString());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean Save() {
|
||||
JsonObject js = new JsonObject();
|
||||
js.addProperty("version", cfg_version);
|
||||
js.add("word_blacklist", wordBlacklist.toJsonArray());
|
||||
JsonObject tablist = new JsonObject();
|
||||
JsonObject color_table = new JsonObject();
|
||||
for (Map.Entry<String, String> e : tablistStatusColors.entrySet()) {
|
||||
color_table.addProperty(e.getKey(), e.getValue());
|
||||
}
|
||||
tablist.add("status_colors", color_table);
|
||||
JsonArray default_status = new JsonArray();
|
||||
for (String s : defaultStatusList) {
|
||||
default_status.add(s);
|
||||
}
|
||||
js.addProperty("default_status", defaultStatus);
|
||||
js.add("default_status_list", default_status);
|
||||
js.add("tablist", tablist);
|
||||
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;
|
||||
}
|
||||
|
||||
void CreateNew(int prev_ver) {
|
||||
if (prev_ver < 1) {
|
||||
wordBlacklist.add("ADMIN");
|
||||
wordBlacklist.add("HOST");
|
||||
wordBlacklist.add("MOD");
|
||||
wordBlacklist.add("TEAM");
|
||||
wordBlacklist.add("STAFF");
|
||||
defaultStatus = "";
|
||||
defaultStatusList.add("Online");
|
||||
defaultStatusList.add("AFK");
|
||||
defaultStatusList.add("Beschäftigt");
|
||||
tablistStatusColors.put("D7", ChatColor.GOLD.toString());
|
||||
tablistStatusColors.put("Online", ChatColor.GREEN.toString());
|
||||
tablistStatusColors.put("AFK", ChatColor.GOLD.toString());
|
||||
tablistStatusColors.put("Beschäftigt", ChatColor.RED.toString());
|
||||
}
|
||||
// Make sure default latest plugin config version is set
|
||||
cfg_version = 0;
|
||||
this.Save();
|
||||
}
|
||||
|
||||
public Wordlist getWordBlacklist() {
|
||||
return this.wordBlacklist;
|
||||
}
|
||||
|
||||
public String getDefaultStatus() {
|
||||
return this.defaultStatus;
|
||||
}
|
||||
public void setDefaultStatus(String value) {
|
||||
this.defaultStatus = value;
|
||||
}
|
||||
|
||||
public Vector<String> getDefaultStatusList() {
|
||||
return this.defaultStatusList;
|
||||
}
|
||||
|
||||
public String getStatusColor(String s) {
|
||||
if (tablistStatusColors.containsKey(s)) {
|
||||
return tablistStatusColors.get(s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user