From e40b85a27bd4c5f6486dcbd8312fd00edb0f3abc Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sat, 16 Dec 2023 21:04:02 +0800 Subject: [PATCH] feat: Add support for granular tax percentage control in tax bracket --- pom.xml | 2 +- .../quicktax/utils/TaxManager.java | 85 ++++++++++++------- src/main/resources/config.yml | 4 + 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index 1f99869..f1fb82c 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ quicktax quicktax - 1.4.2 + 1.4.3 QuickTax diff --git a/src/main/java/tk/taverncraft/quicktax/utils/TaxManager.java b/src/main/java/tk/taverncraft/quicktax/utils/TaxManager.java index a1c78a1..295eafe 100644 --- a/src/main/java/tk/taverncraft/quicktax/utils/TaxManager.java +++ b/src/main/java/tk/taverncraft/quicktax/utils/TaxManager.java @@ -94,9 +94,9 @@ public void collectIndividual(CommandSender sender, String name, String amount) * @param sender the executor of the command */ public void collectAll(CommandSender sender) throws NullPointerException { - double balTaxAmount = this.main.getConfig().getDouble("all.bal-amount"); - double claimsTaxAmount = this.main.getConfig().getDouble("all.claims-ratio"); - boolean usePercentage = this.main.getConfig().getBoolean("all.use-percentage"); + double balTaxAmount = this.main.getConfig().getDouble("all.bal-amount", 0); + double claimsTaxAmount = this.main.getConfig().getDouble("all.claims-ratio", 0); + boolean usePercentage = this.main.getConfig().getBoolean("all.use-percentage", false); updateType func; boolean taxClaims = this.main.getConfig().getBoolean("tax-claims"); @@ -129,7 +129,7 @@ public void collectAll(CommandSender sender) throws NullPointerException { * @param sender the executor of the command */ public void collectRank(CommandSender sender) throws NullPointerException { - boolean usePercentage = this.main.getConfig().getBoolean("rank-bracket.use-percentage"); + boolean usePercentage = this.main.getConfig().getBoolean("rank-bracket.use-percentage", false); ConfigurationSection ranks = this.main.getConfig().getConfigurationSection("rank-bracket.ranks"); updateType func; @@ -148,9 +148,10 @@ public void collectRank(CommandSender sender) throws NullPointerException { for (String rank : ranks.getKeys(true)) { String[] playerGroups = Main.getPermissions().getPlayerGroups(Bukkit.getWorlds().get(0).getName(), offlinePlayer); if (Arrays.stream(playerGroups).anyMatch(rank::equalsIgnoreCase)) { - double balTaxAmount = this.main.getConfig().getDouble("rank-bracket.ranks." + rank + ".bal"); - double claimsTaxAmount = this.main.getConfig().getDouble("rank-bracket.ranks." + rank + ".claims-ratio"); - func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, usePercentage); + double balTaxAmount = this.main.getConfig().getDouble("rank-bracket.ranks." + rank + ".bal", 0); + double claimsTaxAmount = this.main.getConfig().getDouble("rank-bracket.ranks." + rank + ".claims-ratio", 0); + boolean finalUsePercentage = this.main.getConfig().getBoolean("rank-bracket.ranks." + rank + ".use-percentage", usePercentage); + func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, finalUsePercentage); break; } } @@ -172,7 +173,7 @@ public void collectRank(CommandSender sender) throws NullPointerException { * @param sender the executor of the command */ public void collectBal(CommandSender sender) throws NullPointerException { - boolean usePercentage = this.main.getConfig().getBoolean("bal-bracket.use-percentage"); + boolean usePercentage = this.main.getConfig().getBoolean("bal-bracket.use-percentage", false); ConfigurationSection bals = this.main.getConfig().getConfigurationSection("bal-bracket.amount"); if (bals == null) { this.main.getLogger().info("Cannot find balance bracket, is the config correct?"); @@ -204,9 +205,10 @@ public void collectBal(CommandSender sender) throws NullPointerException { continue; } if (BigDecimal.valueOf(Main.getEconomy().getBalance(offlinePlayer)).compareTo(BigDecimal.valueOf((double) bal)) >= 0) { - double balTaxAmount = this.main.getConfig().getDouble("bal-bracket.amount." + bal + ".bal"); - double claimsTaxAmount = this.main.getConfig().getDouble("bal-bracket.amount." + bal + ".claims-ratio"); - func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, usePercentage); + double balTaxAmount = this.main.getConfig().getDouble("bal-bracket.amount." + bal + ".bal", 0); + double claimsTaxAmount = this.main.getConfig().getDouble("bal-bracket.amount." + bal + ".claims-ratio", 0); + boolean finalUsePercentage = this.main.getConfig().getBoolean("bal-bracket.amount." + bal + ".use-percentage", usePercentage); + func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, finalUsePercentage); break; } } @@ -227,7 +229,7 @@ public void collectBal(CommandSender sender) throws NullPointerException { * @param sender the executor of the command */ public void collectActivity(CommandSender sender) throws NullPointerException { - boolean usePercentage = this.main.getConfig().getBoolean("activity-bracket.use-percentage"); + boolean usePercentage = this.main.getConfig().getBoolean("activity-bracket.use-percentage", false); ConfigurationSection lastSeenActivities = this.main.getConfig().getConfigurationSection("activity-bracket.last-seen"); if (lastSeenActivities == null) { this.main.getLogger().info("Cannot find activity bracket, is the config correct?"); @@ -262,9 +264,10 @@ public void collectActivity(CommandSender sender) throws NullPointerException { long currentTime = System.currentTimeMillis(); long elapsedTime = (currentTime - lastPlayed) / 1000; if (elapsedTime >= lastSeen) { - double balTaxAmount = this.main.getConfig().getDouble("activity-bracket.last-seen." + lastSeen + ".bal"); - double claimsTaxAmount = this.main.getConfig().getDouble("activity-bracket.last-seen." + lastSeen + ".claims-ratio"); - func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, usePercentage); + double balTaxAmount = this.main.getConfig().getDouble("activity-bracket.last-seen." + lastSeen + ".bal", 0); + double claimsTaxAmount = this.main.getConfig().getDouble("activity-bracket.last-seen." + lastSeen + ".claims-ratio", 0); + boolean finalUsePercentage = this.main.getConfig().getBoolean("activity-bracket.last-seen." + lastSeen + ".use-percentage", usePercentage); + func.updatePlayer(offlinePlayer, balTaxAmount, claimsTaxAmount, finalUsePercentage); break; } } @@ -290,18 +293,18 @@ public void collectActivity(CommandSender sender) throws NullPointerException { public double getSubtractAmountNoClaims(OfflinePlayer player, boolean usePercentage, double taxAmount, double playerBal) { double subtractAmount; if (usePercentage) { + // keep tax percentage between 0 and 1 + taxAmount = Math.min(1, Math.max(0, taxAmount)); + if (playerBal < 0) { + return 0; + } subtractAmount = playerBal * taxAmount; } else { subtractAmount = taxAmount; } - // prevent negative taxes - if (subtractAmount < 0) { - subtractAmount = 0; - } - - // handles case when player has not enough money - if (playerBal < subtractAmount) { + // handles case when player has not enough money but subtract amount is positive + if (subtractAmount > 0 && playerBal < subtractAmount) { int debtMode = main.getConfig().getInt("debt-mode", 0); if (debtMode == 0) { subtractAmount = 0; @@ -328,6 +331,11 @@ public double getSubtractAmountNoClaims(OfflinePlayer player, boolean usePercent public double getSubtractAmountWithClaims(OfflinePlayer player, boolean usePercentage, double balTaxAmount, double playerBal, double claimsTaxAmount, double playerClaims) { double subtractAmount; if (usePercentage) { + // keep tax percentage between 0 and 1 + balTaxAmount = Math.min(1, Math.max(0, balTaxAmount)); + if (playerBal < 0) { + return 0; + } subtractAmount = playerBal * balTaxAmount; } else { subtractAmount = balTaxAmount; @@ -335,13 +343,8 @@ public double getSubtractAmountWithClaims(OfflinePlayer player, boolean usePerce subtractAmount += claimsTaxAmount * playerClaims; - // prevent negative taxes - if (subtractAmount < 0) { - subtractAmount = 0; - } - - // handles case when player has not enough money - if (playerBal < subtractAmount) { + // handles case when player has not enough money but subtract amount is positive + if (subtractAmount > 0 && playerBal < subtractAmount) { int debtMode = main.getConfig().getInt("debt-mode", 0); if (debtMode == 0) { subtractAmount = 0; @@ -397,10 +400,18 @@ public boolean updatePlayerWithClaims(OfflinePlayer player, double balTaxAmount, // needed in rare situations where async access to economy plugin is disallowed try { - Main.getEconomy().withdrawPlayer(player, subtractAmount); + if (subtractAmount > 0) { + Main.getEconomy().withdrawPlayer(player, subtractAmount); + } else { + Main.getEconomy().depositPlayer(player, Math.abs(subtractAmount)); + } } catch (IllegalStateException e) { double finalSubtractAmount = subtractAmount; - Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().withdrawPlayer(player, finalSubtractAmount)); + if (subtractAmount > 0) { + Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().withdrawPlayer(player, finalSubtractAmount)); + } else { + Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().depositPlayer(player, Math.abs(finalSubtractAmount))); + } } if (validationManager.doStoreData(null)) { @@ -445,9 +456,17 @@ public boolean updatePlayerNoClaims(OfflinePlayer player, double balTaxAmount, d // needed in rare situations where async access to economy plugin is disallowed try { - Main.getEconomy().withdrawPlayer(player, subtractAmount); + if (subtractAmount > 0) { + Main.getEconomy().withdrawPlayer(player, subtractAmount); + } else { + Main.getEconomy().depositPlayer(player, Math.abs(subtractAmount)); + } } catch (IllegalStateException e) { - Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().withdrawPlayer(player, subtractAmount)); + if (subtractAmount > 0) { + Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().withdrawPlayer(player, subtractAmount)); + } else { + Bukkit.getScheduler().runTask(main, () -> Main.getEconomy().depositPlayer(player, Math.abs(subtractAmount))); + } } if (validationManager.doStoreData(null)) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 26c2852..470d8db 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -95,6 +95,7 @@ auto-remove-claims: false # the options here apply when the /quicktax collectall command is used all: # if true, the values used for bal-amount will be in percentage (0.1 = 10%) + # note that if player balance is negative, then percentage will default to 0% (no tax) use-percentage: false # amount of balance to tax @@ -120,6 +121,7 @@ rank-bracket: Knight: bal: 300 claims-ratio: 0.0 + use-percentage: false # optional, if set will apply only to this specific rank # rank apprentice Apprentice: bal: 200 @@ -147,6 +149,7 @@ bal-bracket: 100000: bal: 300 claims-ratio: 0.0 + use-percentage: false # optional, if set will apply only to this specific balance bracket # balance 10000 or more 10000: bal: 200 @@ -174,6 +177,7 @@ activity-bracket: 604800: bal: 300 claims-ratio: 0.0 + use-percentage: false # optional, if set will apply only to this specific activity bracket # last seen more than 1 day go 86400: bal: 200