From 5898e4b912b30d536a3824fc2384999b978473df Mon Sep 17 00:00:00 2001 From: Jesse Date: Thu, 22 Aug 2024 22:54:53 +0000 Subject: [PATCH] All changes made with a wire command only thing to note is potentially the... --- src/main/java/jesse/CommonServerUtils.java | 17 ++ .../keeblarcraft/BankMgr/BankManager.java | 256 ++++++++++++++++++ .../keeblarcraft/Commands/BankCommands.java | 155 +++++++++++ .../Commands/CustomCommandManager.java | 3 + .../keeblarcraft/ConfigMgr/ConfigManager.java | 8 + .../keeblarcraft/ServerTickListener.java | 20 ++ .../java/jesse/keeblarcraft/Utils/Setup.java | 16 ++ 7 files changed, 475 insertions(+) create mode 100644 src/main/java/jesse/CommonServerUtils.java create mode 100644 src/main/java/jesse/keeblarcraft/BankMgr/BankManager.java create mode 100644 src/main/java/jesse/keeblarcraft/Commands/BankCommands.java create mode 100644 src/main/java/jesse/keeblarcraft/ServerTickListener.java diff --git a/src/main/java/jesse/CommonServerUtils.java b/src/main/java/jesse/CommonServerUtils.java new file mode 100644 index 0000000..b241354 --- /dev/null +++ b/src/main/java/jesse/CommonServerUtils.java @@ -0,0 +1,17 @@ +package jesse; + +import net.minecraft.server.MinecraftServer; + +public class CommonServerUtils { +//the config is gonna hold the server instance change it if you want but dont reject my code cause its here +private static MinecraftServer server; + +public void SetServerInstance(MinecraftServer inputServer) { + server = inputServer; +} + +public static MinecraftServer GetServerInstance() { + return server; +} + +} diff --git a/src/main/java/jesse/keeblarcraft/BankMgr/BankManager.java b/src/main/java/jesse/keeblarcraft/BankMgr/BankManager.java new file mode 100644 index 0000000..01e6e06 --- /dev/null +++ b/src/main/java/jesse/keeblarcraft/BankMgr/BankManager.java @@ -0,0 +1,256 @@ +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)); + } + } +} diff --git a/src/main/java/jesse/keeblarcraft/Commands/BankCommands.java b/src/main/java/jesse/keeblarcraft/Commands/BankCommands.java new file mode 100644 index 0000000..9030c5f --- /dev/null +++ b/src/main/java/jesse/keeblarcraft/Commands/BankCommands.java @@ -0,0 +1,155 @@ +package jesse.keeblarcraft.Commands; + +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.LongArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; + +import jesse.keeblarcraft.BankMgr.BankManager; +import jesse.keeblarcraft.ConfigMgr.ConfigManager; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; + +public class BankCommands { + ConfigManager config = new ConfigManager(); + + public void RegisterCommands(){ + // Command: "/getbalance" + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + dispatcher.register(CommandManager.literal("getbalance") + .executes(context -> GetBalance(context))); + }); + + // Command: "/setbalance || rootSetBalance" + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + final var rootSetBalance = dispatcher.register(CommandManager.literal("setbalance") + .then(CommandManager.argument("newBalance", IntegerArgumentType.integer()) + .then(CommandManager.argument("reason", StringArgumentType.string()) + .then(CommandManager.argument("otherParty", StringArgumentType.string()) + .executes(context -> SetBalance( + IntegerArgumentType.getInteger(context, "newBalance"), + StringArgumentType.getString(context, "reason"), + StringArgumentType.getString(context, "otherParty"), + context)))))); + + dispatcher.register(CommandManager.literal("setbalance").redirect(rootSetBalance)); + }); + + // Command: "/AddMoney" + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + dispatcher.register(CommandManager.literal("AddMoney") + .then(CommandManager.argument("reason", StringArgumentType.string()) + .then(CommandManager.argument("payment", LongArgumentType.longArg()) + .then(CommandManager.argument("otherParty", StringArgumentType.string()) + .executes(context -> AddMoney( + StringArgumentType.getString(context, "reason"), + LongArgumentType.getLong(context, "payment"), + StringArgumentType.getString(context, "otherParty"), + context)))))); + }); + + // Command: "/subtractbalance || /SubtractBalance" + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + final var SubtractBalance = dispatcher.register(CommandManager.literal("subtractbalance") + .then(CommandManager.argument("reason", StringArgumentType.string()) + .then(CommandManager.argument("payment", LongArgumentType.longArg()) + .then(CommandManager.argument("otherParty", StringArgumentType.string()) + .executes(context -> SubtractBalance( + StringArgumentType.getString(context, "reason"), + LongArgumentType.getLong(context, "payment"), + StringArgumentType.getString(context, "otherParty"), + context)))))); + + dispatcher.register(CommandManager.literal("subtractbalance").redirect(SubtractBalance)); + }); + + // Command: "/wire || /sendmoney" + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + final var sendmoney = dispatcher.register(CommandManager.literal("wire") + .then(CommandManager.argument("reason", StringArgumentType.string()) + .then(CommandManager.argument("payment", LongArgumentType.longArg()) + .then(CommandManager.argument("otherParty", StringArgumentType.string()) + .executes(context -> Wire( + StringArgumentType.getString(context, "reason"), + LongArgumentType.getLong(context, "payment"), + StringArgumentType.getString(context, "otherParty"), + context)))))); + + dispatcher.register(CommandManager.literal("wire").redirect(sendmoney)); + }); + } + + public Integer GetBalance(CommandContext context) { + Integer ret = -1; + + if (context.getSource().isExecutedByPlayer()) { + ServerPlayerEntity player = context.getSource().getPlayer(); + BankManager playerBank = new BankManager(player.getUuidAsString()); + player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance()))); + return 0; + } + + return ret; + } + + //SetBalance will be a ServerCommand only Perm level Op + public Integer SetBalance(Integer newBalance, String reason, String otherParty, CommandContext context) { + Integer ret = -1; + + if (context.getSource().hasPermissionLevel(1)) { + ServerPlayerEntity player = context.getSource().getPlayer(); + BankManager playerBank = new BankManager(player.getUuidAsString()); + playerBank.SetBalance(newBalance, reason, otherParty); + player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance()))); + } + + return ret; + } + + //AddMoney will be a ServerCommand Only Perm level Op + public Integer AddMoney(String reason, long payment, String otherParty, CommandContext context) { + Integer ret = -1; + + if (context.getSource().hasPermissionLevel(1)) { + ret = 0; + ServerPlayerEntity player = context.getSource().getPlayer(); + BankManager playerBank = new BankManager(player.getUuidAsString()); + playerBank.AddMoney(reason, payment, otherParty); + player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance()))); + } + + return ret; + } + + //SubtractBalance will be a ServerCommand Perm leve Op + public Integer SubtractBalance(String reason, long payment, String otherParty, CommandContext context) { + Integer ret = -1; + + if (context.getSource().hasPermissionLevel(1)) { + ret = 0; + ServerPlayerEntity player = context.getSource().getPlayer(); + BankManager playerBank = new BankManager(player.getUuidAsString()); + playerBank.SubtractBalance(reason, payment, otherParty); + player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance()))); + } + + return ret; + } + + public Integer Wire(String reason, long payment, String otherParty, CommandContext context) { + Integer ret = -1; + + if (context.getSource().isExecutedByPlayer()) { + ret = 0; + ServerPlayerEntity player = context.getSource().getPlayer(); + BankManager playerBank = new BankManager(player.getUuidAsString()); + playerBank.Wire(reason, payment, otherParty); + player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance()))); + } + + return ret; + } +} diff --git a/src/main/java/jesse/keeblarcraft/Commands/CustomCommandManager.java b/src/main/java/jesse/keeblarcraft/Commands/CustomCommandManager.java index 77de42a..39736c4 100644 --- a/src/main/java/jesse/keeblarcraft/Commands/CustomCommandManager.java +++ b/src/main/java/jesse/keeblarcraft/Commands/CustomCommandManager.java @@ -20,10 +20,13 @@ public class CustomCommandManager { // CUSTOM COMMAND CLASS OBJECTS BELOW ShortcutCommands shortcuts = new ShortcutCommands(); NoteCommands noteCommands = new NoteCommands(); + BankCommands bankCommands = new BankCommands(); // REGISTER COMMANDS BELOW System.out.println(ChatUtil.ColoredString("REGISTERING CUSTOM COMMAND EXTENSIONS BELOW", CONSOLE_COLOR.BLUE)); shortcuts.RegisterShortcutCommands(); noteCommands.RegisterNoteCommands(); + bankCommands.RegisterCommands(); + } } diff --git a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java index ba83634..dd8110a 100644 --- a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java +++ b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java @@ -12,6 +12,7 @@ package jesse.keeblarcraft.ConfigMgr; import java.io.FileWriter; +import java.io.FileReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -20,12 +21,16 @@ import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonIOException; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import java.util.List; import org.apache.commons.io.FileUtils; +import org.spongepowered.asm.mixin.injection.struct.InjectorGroupInfo.Map; import java.util.ArrayList; @@ -34,6 +39,9 @@ import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR; // Import all custom exceptions import jesse.keeblarcraft.Utils.CustomExceptions.*; +//minecraft server instance +import net.minecraft.server.MinecraftServer; + public class ConfigManager { // Pedantic empty constructor diff --git a/src/main/java/jesse/keeblarcraft/ServerTickListener.java b/src/main/java/jesse/keeblarcraft/ServerTickListener.java new file mode 100644 index 0000000..b31258a --- /dev/null +++ b/src/main/java/jesse/keeblarcraft/ServerTickListener.java @@ -0,0 +1,20 @@ +package jesse.keeblarcraft; + +import jesse.CommonServerUtils; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.server.MinecraftServer; + +//this lets get the MinecraftServer instance and also do stuff on the end of the tick if we wanted to +public class ServerTickListener implements ServerTickEvents.EndTick { + CommonServerUtils config = new CommonServerUtils(); + @Override + public void onEndTick(MinecraftServer server) { + //Code that runs on end of each tick yes this actually works and tested + config.SetServerInstance(server); + } + + // Static method to register the server tick listener + public static void initialize() { + ServerTickEvents.END_SERVER_TICK.register(new ServerTickListener()); + } +} diff --git a/src/main/java/jesse/keeblarcraft/Utils/Setup.java b/src/main/java/jesse/keeblarcraft/Utils/Setup.java index f6c9ca2..a824252 100644 --- a/src/main/java/jesse/keeblarcraft/Utils/Setup.java +++ b/src/main/java/jesse/keeblarcraft/Utils/Setup.java @@ -17,6 +17,8 @@ import org.spongepowered.include.com.google.common.io.Files; import java.util.List; import java.util.ArrayList; +import jesse.keeblarcraft.Keeblarcraft; +import jesse.keeblarcraft.ServerTickListener; import jesse.keeblarcraft.ConfigMgr.ConfigManager; import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR; import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION; @@ -59,6 +61,7 @@ public final class Setup { add("story"); // Expect 1 file per story chapter! add("commands"); // Expect 1 file per command that's configurable! add("events"); // Expect 1 file per event that is configurable! + add("bank"); }}; // These will be top-level config files above the directories this mod creates @@ -156,8 +159,21 @@ public final class Setup { throw new SETUP_FAILED_EXCEPTION(); } + //testing for this + try { + onInitialize(); + } catch (Exception e){ + System.out.println(ChatUtil.ColoredString("servertick failed to inilize.", CONSOLE_COLOR.RED)); + throw new SETUP_FAILED_EXCEPTION(); + } + System.out.println(ChatUtil.ColoredString("DID SETUP COMPLETE SUCCESSFULLY? ", CONSOLE_COLOR.YELLOW) + (ret ? ChatUtil.ColoredString("YES", CONSOLE_COLOR.YELLOW) : ChatUtil.ColoredString("NO", CONSOLE_COLOR.YELLOW))); return ret; } + + public void onInitialize() { + // Call the setup file to register event listeners and other setup tasks + ServerTickListener.initialize(); + } }