From 111c8c8909251db0cff524d280e39d00f30ddff4 Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sun, 24 Dec 2023 23:24:56 +0800 Subject: [PATCH] feat: Add admin commands to modify server tax balance --- pom.xml | 2 +- .../quicktax/commands/AdminCommand.java | 119 ++++++++++++++++++ .../quicktax/commands/CommandParser.java | 4 +- .../commands/CommandTabCompleter.java | 5 + .../quicktax/utils/StatsManager.java | 48 +++++++ src/main/resources/en.yml | 8 ++ 6 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/main/java/tk/taverncraft/quicktax/commands/AdminCommand.java diff --git a/pom.xml b/pom.xml index e46114c..01da469 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ quicktax quicktax - 1.5.1 + 1.5.2 QuickTax diff --git a/src/main/java/tk/taverncraft/quicktax/commands/AdminCommand.java b/src/main/java/tk/taverncraft/quicktax/commands/AdminCommand.java new file mode 100644 index 0000000..73f3aa3 --- /dev/null +++ b/src/main/java/tk/taverncraft/quicktax/commands/AdminCommand.java @@ -0,0 +1,119 @@ +package tk.taverncraft.quicktax.commands; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; + +import tk.taverncraft.quicktax.Main; +import tk.taverncraft.quicktax.utils.MessageManager; +import tk.taverncraft.quicktax.utils.TaxManager; +import tk.taverncraft.quicktax.utils.ValidationManager; + +/** + * AdminCommand contains the execute method for when an admin modifies the server balance. + */ +public class AdminCommand { + + private final String adminAddPerm = "quicktax.server.admin.add"; + private final String adminTakePerm = "quicktax.server.admin.take"; + private final String adminSetPerm = "quicktax.server.admin.set"; + Main main; + TaxManager taxManager; + ValidationManager validationManager; + + /** + * Constructor for AdminCommand. + */ + public AdminCommand(Main main) { + this.main = main; + this.taxManager = new TaxManager(main); + this.validationManager = new ValidationManager(main); + } + + /** + * Pay tax to the server. + * + * @param sender user who sent the command + * @param args command arguments + * + * @return true at end of execution + */ + public boolean execute(CommandSender sender, String[] args) { + if (!validationManager.doStoreData(sender)) { + return true; + } + + String modifyType = args[2]; + + if (modifyType.equalsIgnoreCase("ADD") && validationManager.hasPermission(adminAddPerm, sender)) { + addToServerBalance(sender, args[3]); + } else if (modifyType.equalsIgnoreCase("TAKE") && validationManager.hasPermission(adminTakePerm, sender)) { + removeFromServerBalance(sender, args[3]); + } else if (modifyType.equalsIgnoreCase("SET") && validationManager.hasPermission(adminSetPerm, sender)) { + setServerBalance(sender, args[3]); + } + return true; + } + + /** + * Adds specified amount to the server balance. + * + * @param sender user who sent the command + * @param strAmount amount to withdraw + */ + private void addToServerBalance(CommandSender sender, String strAmount) { + if (!this.validationManager.isDouble(strAmount, sender)) { + return; + } + + double amount = Double.parseDouble(strAmount); + this.main.getStatsManager().addServerBalanceAsAdmin(amount); + MessageManager.sendMessage(sender, "admin-add-server-balance", + new String[]{"%amount%"}, + new String[]{strAmount}); + } + + /** + * Removes specified amount from the server balance. + * + * @param sender user who sent the command + * @param strAmount amount to withdraw + */ + private void removeFromServerBalance(CommandSender sender, String strAmount) { + if (!this.validationManager.isDouble(strAmount, sender)) { + return; + } + + double amount = Double.parseDouble(strAmount); + + double totalTaxBalance = Double.parseDouble(this.main.getStatsManager().getServerStats()[1]); + if (!validationManager.serverHasTaxBalance(amount, totalTaxBalance)) { + MessageManager.sendMessage(sender, "tax-withdraw-fail"); + return; + } + + this.main.getStatsManager().takeServerBalanceAsAdmin(amount); + MessageManager.sendMessage(sender, "admin-take-server-balance", + new String[]{"%amount%"}, + new String[]{strAmount}); + } + + /** + * Sets specified amount to the server balance. + * + * @param sender user who sent the command + * @param strAmount amount to withdraw + */ + private void setServerBalance(CommandSender sender, String strAmount) { + if (!this.validationManager.isDouble(strAmount, sender)) { + return; + } + + double amount = Double.parseDouble(strAmount); + this.main.getStatsManager().setServerBalanceAsAdmin(amount); + MessageManager.sendMessage(sender, "admin-set-server-balance", + new String[]{"%amount%"}, + new String[]{strAmount}); + } +} + diff --git a/src/main/java/tk/taverncraft/quicktax/commands/CommandParser.java b/src/main/java/tk/taverncraft/quicktax/commands/CommandParser.java index 4b9602c..88c655e 100644 --- a/src/main/java/tk/taverncraft/quicktax/commands/CommandParser.java +++ b/src/main/java/tk/taverncraft/quicktax/commands/CommandParser.java @@ -66,12 +66,14 @@ public class CommandParser implements CommandExecutor { } } - // command to view server stats or withdraw server balance + // command to view server stats, withdraw server balance or admin modify server balance if (chatCmd.equals("server")) { if (args.length == 2 && args[1].equalsIgnoreCase("stats")) { return new StatsCommand(this.main).execute(sender, true); } else if (args.length > 2 && args[1].equalsIgnoreCase("withdraw")) { return new WithdrawCommand(this.main).execute(sender, args); + } else if (args.length > 2 && args[1].equalsIgnoreCase("admin")) { + return new AdminCommand(this.main).execute(sender, args); } } diff --git a/src/main/java/tk/taverncraft/quicktax/commands/CommandTabCompleter.java b/src/main/java/tk/taverncraft/quicktax/commands/CommandTabCompleter.java index c090f6a..f3c0188 100644 --- a/src/main/java/tk/taverncraft/quicktax/commands/CommandTabCompleter.java +++ b/src/main/java/tk/taverncraft/quicktax/commands/CommandTabCompleter.java @@ -42,6 +42,11 @@ public class CommandTabCompleter implements TabCompleter { } else if (args.length == 2 && args[0].equalsIgnoreCase("server")) { completions.add("stats"); completions.add("withdraw"); + completions.add("admin"); + } else if (args.length == 3 && args[0].equalsIgnoreCase("server") && args[1].equalsIgnoreCase("admin")) { + completions.add("add"); + completions.add("take"); + completions.add("set"); } else if (args.length == 4 && args[1].equalsIgnoreCase("withdraw")) { for (Player p : Bukkit.getOnlinePlayers()) { completions.add(p.getName()); diff --git a/src/main/java/tk/taverncraft/quicktax/utils/StatsManager.java b/src/main/java/tk/taverncraft/quicktax/utils/StatsManager.java index 682a440..51ceb08 100644 --- a/src/main/java/tk/taverncraft/quicktax/utils/StatsManager.java +++ b/src/main/java/tk/taverncraft/quicktax/utils/StatsManager.java @@ -189,6 +189,54 @@ public class StatsManager { } } + /** + * Adds to the server balance as an admin. + * + * @param amount the amount of balance to add + */ + public void addServerBalanceAsAdmin(double amount) { + FileConfiguration serverStatsConfig = this.main.getServerStatsConfig(); + File serverStatsFile = new File(this.main.getDataFolder(), "serverstats.yml"); + serverStatsConfig.set("total-tax-balance", serverStatsConfig.getInt("total-tax-balance") + amount); + try { + serverStatsConfig.save(serverStatsFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Takes from the server balance as an admin. + * + * @param amount the amount of balance to take + */ + public void takeServerBalanceAsAdmin(double amount) { + FileConfiguration serverStatsConfig = this.main.getServerStatsConfig(); + File serverStatsFile = new File(this.main.getDataFolder(), "serverstats.yml"); + serverStatsConfig.set("total-tax-balance", serverStatsConfig.getInt("total-tax-balance") - amount); + try { + serverStatsConfig.save(serverStatsFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Sets the server balance as an admin. + * + * @param amount the amount of balance to set + */ + public void setServerBalanceAsAdmin(double amount) { + FileConfiguration serverStatsConfig = this.main.getServerStatsConfig(); + File serverStatsFile = new File(this.main.getDataFolder(), "serverstats.yml"); + serverStatsConfig.set("total-tax-balance", amount); + try { + serverStatsConfig.save(serverStatsFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + /** * Gets the stats of a player. * diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 887da7a..2252fee 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -34,6 +34,11 @@ player-pay-tax-fail: '&cYou have insufficient balance to pay tax!' # %player% player-pay-tax-success: '&aTax was collected from you and you have paid $%amount%!' # %player%, %amount% player-receive-tax-money: '&aYou received $%amount% from the server tax balance!' # %amount% +# admin modify server balance messages +admin-add-server-balance: '&aYou have added $%amount% to the server balance!' # %amount% +admin-take-server-balance: '&aYou have taken $%amount% from the server balance!' # %amount% +admin-set-server-balance: '&aYou have set $%amount% as the server balance!' # %amount% + # scheduling messages schedule-disabled: '&cThis command requires schedule to be enabled in its config!' schedule-not-found: '&cThere are no tax collection schedules to run!' @@ -69,6 +74,9 @@ help-body: | &a/quicktax server stats &b- view the server total tax stats &a/quicktax server withdraw &b- withdraw money to self from server tax balance &a/quicktax server withdraw &b- withdraw money to player from server tax balance + &a/quicktax server admin add &b- add money to the server tax balance + &a/quicktax server admin take &b- take money from the server tax balance + &a/quicktax server admin set &b- set the server tax balance &a/quicktax schedule start &b- start tax collection schedule &a/quicktax schedule stop &b- stop tax collection schedule &a/quicktax schedule view &b- view all schedules