the_big_one/src/main/java/jesse/keeblarcraft/BankMgr/BankManager.java

234 lines
9.9 KiB
Java

package jesse.keeblarcraft.BankMgr;
import java.util.HashMap;
import java.util.Map.Entry;
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 {
InnerBankManagerInfo bankInfo = new InnerBankManagerInfo(); // not sure why we make another one but i guess so we
ConfigManager config = new ConfigManager(); // for read and write privs
public BankManager(String uuid) {
Boolean existingFile = false;
try {
bankInfo = config.GetJsonObjectFromFile("bank/" + uuid + ".json", InnerBankManagerInfo.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 {
bankInfo.uuid = uuid;
FlashConfig();
} 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;
}
}
private class InnerBankManager {
public InnerBankManager(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;
long payment;
String otherParty;
Integer time;
}
public class InnerBankManagerInfo {
// 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<String, InnerBankManager> bank = new HashMap<String, InnerBankManager>();
}
public long GetBalance() {
long ret = 0;
for (Entry<String, InnerBankManager> entry : bankInfo.bank.entrySet()) {
ret = entry.getValue().balance;
}
return ret;
}
public void SetBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time) {
Integer transactionNumber = bankInfo.bank.size();
bankInfo.bank.put(transactionNumber.toString(),
new InnerBankManager(newBalance, reason, payment, otherParty, time));
FlashConfig();
}
public void AddBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time) {
if (bankInfo.bank.size() > 0) {
for (Entry<String, InnerBankManager> entry : bankInfo.bank.entrySet()) {
entry.getValue().balance += newBalance;
entry.getValue().reason = reason;
entry.getValue().payment = payment;
entry.getValue().otherParty = otherParty;
entry.getValue().time = time;
}
} else {
bankInfo.bank.put(bankInfo.uuid, new InnerBankManager(newBalance, reason, payment, otherParty, time));
}
FlashConfig();
}
public void SubtractBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time) {
if (bankInfo.bank.size() > 0) {
for (Entry<String, InnerBankManager> entry : bankInfo.bank.entrySet()) {
entry.getValue().balance -= newBalance;
entry.getValue().reason = reason;
entry.getValue().payment = payment;
entry.getValue().otherParty = otherParty;
entry.getValue().time = time;
}
} else {
bankInfo.bank.put(bankInfo.uuid, new InnerBankManager(newBalance, reason, payment, otherParty, time));
}
FlashConfig();
}
//WE NEED TO ADD A CHECK IF THE PLAYER HAS A ACCOUNT AS WELL TECHINCALLY even though they should never try to wire first
public void Wire(Integer newBalance, String reason, long payment, String otherParty, Integer time) {
if (bankInfo.bank.size() > 0) {
for (Entry<String, InnerBankManager> entry : bankInfo.bank.entrySet()) {
entry.getValue().balance -= newBalance;
entry.getValue().reason = reason;
entry.getValue().payment = payment;
entry.getValue().otherParty = otherParty;
entry.getValue().time = time;
if (payment <= 0) {
// add a error for the PLAYER not the server
return;
}
}
// make a server instance
MinecraftServer server = ConfigManager.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") != null) {
InnerBankManagerInfo newBankInfo = new InnerBankManagerInfo();
InnerBankManager newBank = new InnerBankManager(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
InnerBankManagerInfo newBankInfo = new InnerBankManagerInfo();
newBankInfo = config.GetJsonObjectFromFile("bank/" + GetUuidByName(server, otherParty) + ".json", InnerBankManagerInfo.class);
// for now we will only use adding valance and not subtracting since that dosent make any sense
for (Entry<String, InnerBankManager> entry : newBankInfo.bank.entrySet()) {
entry.getValue().balance += newBalance;
entry.getValue().reason = reason;
entry.getValue().payment = payment;
entry.getValue().otherParty = otherParty;
entry.getValue().time = time;
}
//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();
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 "";
}
}
public void FlashConfig() {
try {
config.WriteToJsonFile("bank/" + bankInfo.uuid.toString() + ".json", bankInfo);
} catch (FILE_WRITE_EXCEPTION e) {
System.out.println(ChatUtil.ColoredString("Could not flash notes configuration file", CONSOLE_COLOR.RED));
}
// public void FlashConfigAppend() {
// try {
// config.WriteToJsonFile("bank/" + bankInfo.uuid.toString() + ".json",
// bankInfo);
// } catch (FILE_WRITE_EXCEPTION e) {
// System.out.println(ChatUtil.ColoredString("Could not flash notes
// configuration file", CONSOLE_COLOR.RED));
// }
// }
}
}