package jesse.keeblarcraft.BankMgr; import java.util.HashMap; import java.util.Map.Entry; import jesse.CommonServerUtils; 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.MinecraftServer; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; public class BankManager { BankManagerFile bankInfo = new BankManagerFile(); // not sure why we make another one but i guess so we ConfigManager config = new ConfigManager(); // for read and write privs CommonServerUtils commonServerUtils = new CommonServerUtils(); public BankManager(String uuid) { Boolean existingFile = false; try { bankInfo = config.GetJsonObjectFromFile("bank/" + uuid + ".json", BankManagerFile.class); existingFile = true; } catch (Exception e) { // Do nothing. This means the file does not exist } // In the event the above code failed out, this means a new file has to be // created for the player's uuid if (!existingFile) { System.out.println(ChatUtil.ColoredString("Trying to create new file", CONSOLE_COLOR.BLUE)); try { FlashConfig(bankInfo.uuid); } catch (Exception e) { System.out.println(ChatUtil.ColoredString("Could not write to file", CONSOLE_COLOR.RED)); } } else { System.out.println(ChatUtil.ColoredString("Moving on", CONSOLE_COLOR.BLUE)); } if ("".equals(bankInfo.uuid)) { System.out.println(ChatUtil.ColoredString("Assigning new config file for this uuid. No previous existing", CONSOLE_COLOR.BLUE)); bankInfo.uuid = uuid; } } //this class is the structure for the hashmap in BankManagerFile private class BankManagerMetaData { public BankManagerMetaData(long money, String reason, long payment, String otherParty, Integer time) { this.balance = money; this.reason = reason; this.payment = payment; this.otherParty = otherParty; this.time = time; } long balance = 0; String reason; //not sure why my compiler is saying unused long payment; String otherParty; Integer time; } // This is the general bank account of the read-in config for the player uuid ||| the class that gets converted into a json for the players file public class BankManagerFile { // Players uuid is the name of the file String uuid; // Contents of file /* * Example: * player_uuid_here: * { * "1": * { * "balance": "10"; * "reason": "tax evasion"; * "payment": $-44 * "other party": "jt"; * "time": "30"; * } * "2": * { * Etc. * } * } */ public HashMap bank = new HashMap(); } public long GetBalance() { long ret = 0; for (Entry entry : bankInfo.bank.entrySet()) { ret = entry.getValue().balance; } return ret; } //https://maven.fabricmc.net/docs/fabric-api-0.34.8+1.17/net/fabricmc/fabric/api/networking/v1/PlayerLookup.html maybe this for getting the players im still not sure public void SetBalance(Integer newBalance, String reason, String otherParty) { Integer transactionNumber = bankInfo.bank.size(); bankInfo.bank.put(transactionNumber.toString(), new BankManagerMetaData(newBalance, reason, newBalance, otherParty, 0)); FlashConfig(PlayerListNameChecker(otherParty)); } public void AddMoney(String reason, long payment, String otherParty) { if (bankInfo.bank.size() > 0) { for (Entry entry : bankInfo.bank.entrySet()) { entry.getValue().balance += payment; entry.getValue().reason = "SERVER: " + reason; entry.getValue().payment = payment; entry.getValue().otherParty = otherParty; entry.getValue().time = 0; } } else { bankInfo.bank.put(bankInfo.uuid, new BankManagerMetaData(payment, reason, payment, otherParty, 0)); } FlashConfig(PlayerListNameChecker(otherParty)); } public void SubtractBalance(String reason, long payment, String otherParty) { if (bankInfo.bank.size() > 0) { for (Entry entry : bankInfo.bank.entrySet()) { entry.getValue().balance -= payment;//not working? entry.getValue().reason = "SERVER: " + reason; entry.getValue().payment = payment; entry.getValue().otherParty = otherParty; entry.getValue().time = 0; } } else { bankInfo.bank.put(bankInfo.uuid, new BankManagerMetaData(0, reason, payment, otherParty, 0)); } FlashConfig(PlayerListNameChecker(otherParty)); } public void Wire(String reason, long payment, String otherParty) { if (bankInfo.bank.size() > 0) { for (Entry entry : bankInfo.bank.entrySet()) { entry.getValue().balance -= payment; entry.getValue().reason = reason; entry.getValue().payment = payment; entry.getValue().otherParty = otherParty; entry.getValue().time = 0; if (payment <= 0) { // add a error for the PLAYER not the server return; } } // make a server instance MinecraftServer server = CommonServerUtils.GetServerInstance(); String[] playerList = server.getPlayerNames(); //NOT SURE IF THIS FOR LOOP IS ONE OFF COULD POSSIBLEY BE SO IF NO ONE IS GETTING MONEY DOUBLE CHECK FOR LOOP for (int i = 0; i < playerList.length; i++) { System.out.println(ChatUtil.ColoredString("PLAYERS: " + playerList, CONSOLE_COLOR.YELLOW)); if (playerList[i] == otherParty) { System.out.println(ChatUtil.ColoredString("Found Player: " + otherParty, CONSOLE_COLOR.GREEN)); // we will use getuuidbyname then set the other VALID players bank BE SURE TO // MAKE THEM A BANK FIRST IF THEY DONT HAVE ONE fortnite forever if (config.GetFile("bank/" + GetUuidByName(server, otherParty) + ".json").size() < 1) { BankManagerFile newBankInfo = new BankManagerFile(); BankManagerMetaData newBank = new BankManagerMetaData(0, "Account Created", 0, "Server", 0); newBankInfo.bank.put(GetUuidByName(server, otherParty).toString(), newBank); try { config.WriteToJsonFile("bank/" + newBankInfo.uuid + ".json", newBankInfo); } catch (FILE_WRITE_EXCEPTION e) { System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED)); return; } } //since the other player has a account now and we have access to it we can start fucking around BankManagerFile newBankInfo = new BankManagerFile(); newBankInfo = config.GetJsonObjectFromFile("bank/" + GetUuidByName(server, otherParty) + ".json", BankManagerFile.class); // for now we will only use adding valance and not subtracting since that dosent make any sense for (Entry entry : newBankInfo.bank.entrySet()) { entry.getValue().balance += payment; entry.getValue().reason = reason; entry.getValue().payment = payment; entry.getValue().otherParty = otherParty; entry.getValue().time = 0; } //cannot use regular flash config since the hard coded values need to add agurment for the uuid //needs to be inside the player check FlashConfig(newBankInfo.uuid); try { config.WriteToJsonFile("bank/" + newBankInfo.uuid + ".json", bankInfo); } catch (FILE_WRITE_EXCEPTION e) { System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED)); } } else { System.out.println(ChatUtil.ColoredString("Player Not Found: " + otherParty, CONSOLE_COLOR.RED)); return; } } } else { System.out.println(ChatUtil.ColoredString("You need to finance better", CONSOLE_COLOR.RED)); return; } } String GetUuidByName(MinecraftServer server, String playerName) { PlayerManager playerManager = server.getPlayerManager(); ServerPlayerEntity player = playerManager.getPlayer(playerName); if (player.getUuid() != null) { return player.getUuidAsString(); } else { return ""; } } String PlayerListNameChecker(String otherParty) { MinecraftServer server = CommonServerUtils.GetServerInstance(); String[] playerList = server.getPlayerNames(); for (int i = 0; i < playerList.length; i++) { System.out.println(ChatUtil.ColoredString("PLAYERS: " + playerList, CONSOLE_COLOR.YELLOW)); if (playerList[i] == otherParty) { System.out.println(ChatUtil.ColoredString("Found Player: " + otherParty, CONSOLE_COLOR.GREEN)); // we will use getuuidbyname then set the other VALID players bank BE SURE TO // MAKE THEM A BANK FIRST IF THEY DONT HAVE ONE fortnite forever if (config.GetFile("bank/" + GetUuidByName(server, otherParty) + ".json").size() < 1) { BankManagerFile newBankInfo = new BankManagerFile(); BankManagerMetaData newBank = new BankManagerMetaData(0, "Account Created", 0, "Server", 0); newBankInfo.bank.put(GetUuidByName(server, otherParty).toString(), newBank); try { config.WriteToJsonFile("bank/" + newBankInfo.uuid + ".json", newBankInfo); } catch (FILE_WRITE_EXCEPTION e) { System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED)); return ""; } } else { System.out.println(ChatUtil.ColoredString("Bank Account Found", CONSOLE_COLOR.GREEN)); } return GetUuidByName(server, otherParty); } } System.out.println(ChatUtil.ColoredString("For Loop condition bypassed Null Playerlist or Null Server instance", CONSOLE_COLOR.RED)); return ""; } public void FlashConfig(String uuid) { try { config.WriteToJsonFile("bank/" + uuid + ".json", bankInfo); } catch (FILE_WRITE_EXCEPTION e) { System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED)); } } }