the_big_one/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java

168 lines
6.4 KiB
Java

/*
*
* FactionManager
*
* Class is responsible for keeping track of factions chosen by the players in the game and saves to the configuration
* file for persistent data storage. Class handles checks as well for eligibility purposes (making sure players can join, etc)
*
*/
package jesse.keeblarcraft.FactionMgr;
import java.util.List;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.ChatUtil;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public class FactionManager {
private static String FACTION_CFG_FILE = "factions/factions.json";
ConfigManager config = new ConfigManager();
private static FactionManager static_inst;
public static FactionManager GetInstance() {
if (static_inst == null) {
static_inst = new FactionManager();
}
return static_inst;
}
private class FactionConfigClassWrapper {
FactionConfig factions = new FactionConfig();
}
FactionConfigClassWrapper factionConfig;// = new FactionConfigClassWrapper();
// Constructor
public FactionManager() {
// Read in config at start of object
System.out.println("FACTIONMANAGER CONSTRUCTOR CALLED");
Boolean existingFile = false;
factionConfig = new FactionConfigClassWrapper();
Boolean tmpchck = factionConfig == null;
System.out.println("Is factionConfig null still? " + (tmpchck ? "YES" : "NO"));
try {
factionConfig = config.GetJsonObjectFromFile(FACTION_CFG_FILE, FactionConfigClassWrapper.class);
tmpchck = factionConfig == null;
System.out.println("Is factionconfig null after trying to load stuff? " + (tmpchck ? "YES" : "NO"));
existingFile = true;
} catch (Exception e) {
// Do nothing
}
// Create the file if it didn't exist before
if (!existingFile)
{
try {
config.CreateDirectory("bank/" + "factions.json");
FlashConfig();
} catch (Exception e) {
System.out.println(ChatUtil.ColoredString("Could not write to file", CONSOLE_COLOR.RED));
}
}
if (factionConfig == null) {
// the only way for this to be possible is if the read-in was bad. flash config file then try again
factionConfig = new FactionConfigClassWrapper();
//TODO: Add safe-guard in here to check if default faction dir exists and move it to OLD/CORRUPTED (so data is not nuked from orbit)
factionConfig.factions = new FactionConfig();
FlashConfig();
}
}
public Boolean LeaveFaction(ServerPlayerEntity player) {
Boolean success = false;
String playerFac = factionConfig.factions.FindFactionOfPlayer(player.getUuidAsString());
if (playerFac != "") {
success = factionConfig.factions.LeaveFaction(playerFac, player.getUuidAsString(), player.getDisplayName().toString());
player.sendMessage(Text.of("[Factions]: You left your faction!"));
} else {
player.sendMessage(Text.of("[Factions]: You are not in a faction!"));
}
return success;
}
public Boolean CreateFaction(String factionName, ServerPlayerEntity creator) {
Boolean success = false;
String facOfPlayer = factionConfig.factions.FindFactionOfPlayer(creator.getUuidAsString());
if (facOfPlayer == "") {
success = factionConfig.factions.CreateFaction(factionName, creator.getUuidAsString(), creator.getDisplayName().toString());
if (!success) {
creator.sendMessage(Text.of("[Factions]: Could not create faction - faction already exists."));
} else {
creator.sendMessage(Text.of("[Factions]: Successfully created faction!"));
FlashConfig();
}
} else {
creator.sendMessage(Text.of("[Factions]: You are already in a faction! You cannot create one."));
}
return success;
}
public Boolean DeleteFaction(String factionName, ServerPlayerEntity caller) {
Boolean success = factionConfig.factions.DeleteFaction(factionName, caller.getUuidAsString());
if (!success) {
caller.sendMessage(Text.of("[Factions]: Could not delete faction. You must be owner & faction must exist."));
} else {
caller.sendMessage(Text.of("[Factions]: Successfully deleted faction."));
FlashConfig();
}
return success;
}
public List<String> ListOfFactions() {
System.out.println("Callthrough of listoffactions");
return factionConfig.factions.ListOfAllFactions();
}
public String GetFactionOfPlayer(String playerUuid) {
return factionConfig.factions.FindFactionOfPlayer(playerUuid);
}
public Boolean PromotePlayer(ServerPlayerEntity caller, String promoteeUuid, String promoteeDisplayName) {
Boolean success = factionConfig.factions.PromotePlayer(GetFactionOfPlayer(caller.getUuidAsString()), caller.getUuidAsString(), promoteeUuid, promoteeDisplayName);
if (!success) {
caller.sendMessage(Text.of("[Factions]: Could not promote player - you need to be a higher rank than them and they cannot be promoted to your level!"));
} else {
caller.sendMessage(Text.of("[Factions]: Successfully promoted player!"));
}
return success;
}
public Boolean DemotePlayer(ServerPlayerEntity caller, String promoteeUuid, String promoteeDisplayName) {
Boolean success = factionConfig.factions.DemotePlayer(GetFactionOfPlayer(caller.getUuidAsString()), caller.getUuidAsString(), promoteeUuid, promoteeDisplayName);
if (!success) {
caller.sendMessage(Text.of("[Factions]: Could not demote player - you need to be a higher rank than them to demote them!"));
} else {
caller.sendMessage(Text.of("[Factions]: Successfully demoted player!"));
}
return success;
}
public void FlashConfig() {
try {
config.WriteToJsonFile(FACTION_CFG_FILE, factionConfig);
} catch (FILE_WRITE_EXCEPTION e) {
System.out.println("config writing of faction file failed. oh well!");
}
}
}