the_big_one/src/main/java/jesse/keeblarcraft/ConfigMgr/GeneralConfig.java

103 lines
3.2 KiB
Java
Raw Normal View History

2024-11-29 09:00:14 +00:00
package jesse.keeblarcraft.ConfigMgr;
import java.util.ArrayList;
import java.util.List;
import jesse.keeblarcraft.Utils.DirectionalVec;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION;
public class GeneralConfig {
private static GeneralConfig static_inst;
ConfigManager cfgMgr = new ConfigManager();
private ImplementedConfig config;
public static GeneralConfig GetInstance() {
if (static_inst == null) {
static_inst = new GeneralConfig();
}
return static_inst;
}
// Guarentee to not be null
public ImplementedConfig GetConfig() {
if (config == null) {
config = new ImplementedConfig();
FlashConfig();
}
return config;
}
public GeneralConfig() {
Boolean existingFile = false;
try {
config = cfgMgr.GetJsonObjectFromFile("general.json", ImplementedConfig.class);
System.out.println("Read in config. Random value: " + config.global_spawn.x);
existingFile = true;
} catch(Exception e) {
System.out.println("FAILED TO READ IN GENERALCONFIG");
e.printStackTrace();
}
if (!existingFile) {
config = new ImplementedConfig();
FlashConfig();
}
if (config == null) {
config = new ImplementedConfig();
FlashConfig();
}
}
// Can return null; if null then do not spawn the player because global spawn wasn't set!
public DirectionalVec GetSpawnCoords() {
System.out.println("GetSpawnCoords called. is global_spawn null? " + (config.global_spawn == null ? "YES": "NO"));
return config.global_spawn;
}
public void SetSpawnCoords(DirectionalVec vec) {
config.global_spawn = vec;
FlashConfig();
}
// The idea behind this function is a one time check option. If you have the UUID, they must now be joining
// so we wrap adding them to the list in one check
public Boolean IsNewPlayer(String uuid) {
System.out.println("IsNewPlayer called. List has: " + config.playerList);
Boolean isNew = !config.playerList.contains(uuid);
System.out.println("Value of isNew is " + isNew);
if (isNew) {
config.playerList.add(uuid);
FlashConfig();
}
return isNew;
}
public String GetMOTD() {
return config.MOTD;
}
private class ImplementedConfig {
public String MOTD = "Welcome to the server! Have fun!";
// This is lazy, but this will fill with every unique UUID that has joined the server. This is how I am checking
// to see if a player has just joined the server for a first time or not
public List<String> playerList = new ArrayList<>();
DirectionalVec global_spawn;
}
private void FlashConfig() {
System.out.println("Attempting to write generalconfig to file. Is null? " + (config == null ? "YES" : "NO"));
try {
cfgMgr.WriteToJsonFile("general.json", config);
} catch (FILE_WRITE_EXCEPTION e) {
System.out.println("Caught FileWriteException from general config writing. uh oh!");
}
}
}