143 lines
4.2 KiB
Java
143 lines
4.2 KiB
Java
|
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;
|
||
|
}
|
||
|
}
|