75 lines
1.9 KiB
Java
75 lines
1.9 KiB
Java
package party._2a03.mc.util;
|
|
|
|
import com.google.common.collect.Maps;
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Path;
|
|
import java.util.Map;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONArray;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import party._2a03.mc.MinecraftTweaks2a03;
|
|
import party._2a03.mc.util.PlayerData;
|
|
import party._2a03.mc.util.PlayerPosition;
|
|
|
|
public class Config {
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
private static JSONObject json;
|
|
private static File config;
|
|
|
|
public static void initConfig(File configDir) {
|
|
config = new File(configDir, "2a03.json");
|
|
}
|
|
|
|
public static void loadConfig() throws Exception {
|
|
LOGGER.info("Loading 2a03.party configuration");
|
|
if (config.exists()) {
|
|
InputStream is = new FileInputStream(config);
|
|
String jsonRaw = IOUtils.toString(is, "UTF-8");
|
|
json = new JSONObject(jsonRaw);
|
|
} else {
|
|
json = new JSONObject();
|
|
}
|
|
|
|
setDefault("persistentFlight", true);
|
|
setDefault("disableFlyCommand", true);
|
|
setDefault("disableTntExplosions", false);
|
|
setDefault("disableRespawnAnchorExplosions", false);
|
|
setDefault("iphubApiKey", "");
|
|
setDefault("spawn", (new PlayerPosition()).getJSON());
|
|
saveConfig();
|
|
|
|
LOGGER.info("Configuration loaded");
|
|
}
|
|
|
|
public static JSONArray getData(String key) {
|
|
return json.getJSONArray(key);
|
|
}
|
|
|
|
public static boolean getBool(String key) {
|
|
return json.getBoolean(key);
|
|
}
|
|
|
|
public static void setData(String key, JSONArray data) {
|
|
json.put(key, data);
|
|
saveConfig();
|
|
}
|
|
|
|
public static void setDefault(String key, Object data) {
|
|
if (!json.has(key))
|
|
json.put(key, data);
|
|
}
|
|
|
|
private static void saveConfig() {
|
|
try (FileWriter file = new FileWriter(config)) {
|
|
file.write(json.toString(2));
|
|
} catch (Exception e) {
|
|
LOGGER.error("Failed to save config file");
|
|
}
|
|
}
|
|
}
|