Starterkit/src/main/java/de/npid7/mc/Configs/PluginConfig.java
tobid7 420b5d2685
Some checks failed
Build Plugin / build-test (push) Failing after 42s
Initial Code
2024-11-22 18:38:23 +01:00

82 lines
2.0 KiB
Java

package de.npid7.mc.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;
import java.util.Vector;
import org.bukkit.inventory.ItemStack;
public class PluginConfig {
File file = null;
int cfg_version = 0;
HashMap<UUID, String> players_joined = new HashMap<>();
public PluginConfig(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) {
file.delete();
return false;
}
for (Map.Entry<String, JsonElement> e :
js.get("players_joined").getAsJsonObject().entrySet()) {
players_joined.put(UUID.fromString(e.getKey()), e.getValue().getAsString());
}
} catch (IOException e) {
e.printStackTrace();
return true;
}
return false;
}
public boolean Check(UUID id, String name) {
if (players_joined.containsKey(id)) {
return true;
}
players_joined.put(id, name);
Save();
return false;
}
public boolean Save() {
JsonObject js = new JsonObject();
js.addProperty("version", cfg_version);
JsonObject pjm = new JsonObject();
for (Map.Entry<UUID, String> e : players_joined.entrySet()) {
pjm.addProperty(e.getKey().toString(), e.getValue());
}
js.add("players_joined", pjm);
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;
}
}