All changes made with a wire command only thing to note is potentially the getPlayerNames() on line 155 needs to be changed but that is still uet to be tested on a real server with real accounts

This commit is contained in:
Walrus 2024-08-15 18:24:19 -04:00
parent 4bcbc88a42
commit 5b28fa4fc4
7 changed files with 502 additions and 0 deletions

View File

@ -0,0 +1,233 @@
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));
// }
// }
}
}

View File

@ -0,0 +1,177 @@
package jesse.keeblarcraft.Commands;
import java.util.HashMap;
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();
HashMap<String, Integer> monopolyMan;
public void RegisterCommands(){
// Command: "/getbalance"
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("getbalance")
.executes(context -> GetBalance(context)));
});
// Command: "/setbalance"
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("payment", LongArgumentType.longArg())
.then(CommandManager.argument("otherParty", StringArgumentType.string())
//probably will remove time and otherParty from this agurment list not sure yet
.then(CommandManager.argument("time", IntegerArgumentType.integer())
.executes(context -> SetBalance(
IntegerArgumentType.getInteger(context, "newBalance"),
StringArgumentType.getString(context, "reason"),
LongArgumentType.getLong(context, "payment"),
StringArgumentType.getString(context, "otherParty"),
IntegerArgumentType.getInteger(context, "time"),
context))))))));
dispatcher.register(CommandManager.literal("resetbalance").redirect(rootSetBalance));
});
// Command: "/addbalance"
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("addbalance")
.then(CommandManager.argument("newBalance", IntegerArgumentType.integer())
.then(CommandManager.argument("reason", StringArgumentType.string())
.then(CommandManager.argument("payment", LongArgumentType.longArg())
.then(CommandManager.argument("otherParty", StringArgumentType.string())
//probably will remove time and otherParty from this agurment list not sure yet
.then(CommandManager.argument("time", IntegerArgumentType.integer())
.executes(context -> AddBalance(
IntegerArgumentType.getInteger(context, "newBalance"),
StringArgumentType.getString(context, "reason"),
LongArgumentType.getLong(context, "payment"),
StringArgumentType.getString(context, "otherParty"),
IntegerArgumentType.getInteger(context, "time"),
context))))))));
});
// Command: "/removebalance"
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
final var rootSubtractBalance = dispatcher.register(CommandManager.literal("subtractbalance")
.then(CommandManager.argument("newBalance", IntegerArgumentType.integer())
.then(CommandManager.argument("reason", StringArgumentType.string())
.then(CommandManager.argument("payment", LongArgumentType.longArg())
.then(CommandManager.argument("otherParty", StringArgumentType.string())
//probably will remove time and otherParty from this agurment list not sure yet
.then(CommandManager.argument("time", IntegerArgumentType.integer())
.executes(context -> SubtractBalance(
IntegerArgumentType.getInteger(context, "newBalance"),
StringArgumentType.getString(context, "reason"),
LongArgumentType.getLong(context, "payment"),
StringArgumentType.getString(context, "otherParty"),
IntegerArgumentType.getInteger(context, "time"),
context))))))));
dispatcher.register(CommandManager.literal("-balance").redirect(rootSubtractBalance));
});
// Command: "/wire"
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
final var sendmoney = dispatcher.register(CommandManager.literal("wire")
.then(CommandManager.argument("newBalance", IntegerArgumentType.integer())
.then(CommandManager.argument("reason", StringArgumentType.string())
.then(CommandManager.argument("payment", LongArgumentType.longArg())
.then(CommandManager.argument("otherParty", StringArgumentType.string())
//probably will remove time and otherParty from this agurment list not sure yet
.then(CommandManager.argument("time", IntegerArgumentType.integer())
.executes(context -> Wire(
IntegerArgumentType.getInteger(context, "newBalance"),
StringArgumentType.getString(context, "reason"),
LongArgumentType.getLong(context, "payment"),
StringArgumentType.getString(context, "otherParty"),
IntegerArgumentType.getInteger(context, "time"),
context))))))));
dispatcher.register(CommandManager.literal("resetbalance").redirect(sendmoney));
});
}
public Integer GetBalance(CommandContext<ServerCommandSource> 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 ret;
}
//SetBalance will be a ServerCommand only
public Integer SetBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time, CommandContext<ServerCommandSource> context) {
Integer ret = -1;
if (context.getSource().isExecutedByPlayer()) {
ServerPlayerEntity player = context.getSource().getPlayer();
BankManager playerBank = new BankManager(player.getUuidAsString());
playerBank.SetBalance(newBalance, reason, payment, otherParty, time);
player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance())));
}
return ret;
}
//AddBalance will be a ServerCommand Only
public Integer AddBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time, CommandContext<ServerCommandSource> context) {
Integer ret = -1;
if (context.getSource().isExecutedByPlayer()) {
ret = 0;
ServerPlayerEntity player = context.getSource().getPlayer();
BankManager playerBank = new BankManager(player.getUuidAsString());
playerBank.AddBalance(newBalance, reason, payment, otherParty, time);
player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance())));
}
return ret;
}
//SubtractBalance will be a ServerCommand only
public Integer SubtractBalance(Integer newBalance, String reason, long payment, String otherParty, Integer time, CommandContext<ServerCommandSource> context) {
Integer ret = -1;
if (context.getSource().isExecutedByPlayer()) {
ret = 0;
ServerPlayerEntity player = context.getSource().getPlayer();
BankManager playerBank = new BankManager(player.getUuidAsString());
playerBank.SubtractBalance(newBalance, reason, payment, otherParty, time);
player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance())));
}
return ret;
}
public Integer Wire(Integer newBalance, String reason, long payment, String otherParty, Integer time, CommandContext<ServerCommandSource> context) {
Integer ret = -1;
if (context.getSource().isExecutedByPlayer()) {
ret = 0;
ServerPlayerEntity player = context.getSource().getPlayer();
BankManager playerBank = new BankManager(player.getUuidAsString());
playerBank.Wire(newBalance, reason, payment, otherParty, time);
player.sendMessage(Text.literal(String.valueOf(playerBank.GetBalance())));
}
return ret;
}
}

View File

@ -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();
}
}

View File

@ -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
@ -142,6 +150,38 @@ public class ConfigManager {
}
}
//NOTE: THIS APPENDS TO EXISITING JSON FILE
// public void AppendToJsonFile(String fileName, Object data) throws FILE_WRITE_EXCEPTION {
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
// File file = new File(fileName);
// if (!file.exists()) {
// WriteToJsonFile(fileName, data);
// }
// try {
// //read the json file and get the objects and store them in a array
// FileReader jsonParser = new FileReader(fileName);
// JsonArray jsonArray = new JsonArray();
// jsonArray = JsonParser.parseReader(jsonParser).getAsJsonArray();
// FileWriter writer = new FileWriter(fileName);
// jsonParser.close();
// //make the new json object here convert the map with gson and put in the array
// JsonObject createJsonObject = new JsonObject();
// Gson mapper = new Gson();
// createJsonObject = mapper.toJsonTree(data).getAsJsonObject();
// jsonArray.add(createJsonObject);
// //write to file
// gson.toJson(jsonArray, writer);
// writer.flush();
// writer.close();
// }
// catch (JsonIOException | IOException e) {
// System.out.println(ChatUtil.ColoredString("Could not successfully write to json file", CONSOLE_COLOR.RED));
// throw new FILE_WRITE_EXCEPTION();
// }
// }
// GetJsonStringFromFile
//
// Retrieves json file and converts to desired object. Returns JsonSyntaxException if file could not be fitted to class input
@ -241,4 +281,15 @@ public class ConfigManager {
return ret;
}
//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;
}
}

View File

@ -11,6 +11,7 @@ package jesse.keeblarcraft;
import net.fabricmc.api.ModInitializer;
// import net.minecraft.server.command.ServerCommandSource;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -0,0 +1,19 @@
package jesse.keeblarcraft;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
public class ServerTickListener implements ServerTickEvents.EndTick {
ConfigManager config = new ConfigManager();
@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());
}
}

View File

@ -17,11 +17,15 @@ 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;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_DELETE_EXCEPTION;
import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
// Singleton class is designed to help the mod set itself up and create all the important things. It does two things:
//
@ -59,6 +63,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 +161,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();
}
}