feat: Add admin commands to modify server tax balance

This commit is contained in:
tjtanjin 2023-12-24 23:24:56 +08:00
parent 682eec4212
commit 111c8c8909
6 changed files with 184 additions and 2 deletions

View File

@ -7,7 +7,7 @@
<groupId>quicktax</groupId>
<artifactId>quicktax</artifactId>
<version>1.5.1</version>
<version>1.5.2</version>
<name>QuickTax</name>

View File

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

View File

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

View File

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

View File

@ -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.
*

View File

@ -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 <amount> &b- withdraw money to self from server tax balance
&a/quicktax server withdraw <amount> <name> &b- withdraw money to player from server tax balance
&a/quicktax server admin add <amount> &b- add money to the server tax balance
&a/quicktax server admin take <amount> &b- take money from the server tax balance
&a/quicktax server admin set <amount> &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