Skip to content

Commit

Permalink
feat: Add support for granular tax percentage control in tax bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Dec 16, 2023
1 parent c5fdd6c commit e40b85a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>quicktax</groupId>
<artifactId>quicktax</artifactId>
<version>1.4.2</version>
<version>1.4.3</version>

<name>QuickTax</name>

Expand Down
85 changes: 52 additions & 33 deletions src/main/java/tk/taverncraft/quicktax/utils/TaxManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<OfflinePlayer, Double, Double, Boolean> func;
boolean taxClaims = this.main.getConfig().getBoolean("tax-claims");
Expand Down Expand Up @@ -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<OfflinePlayer, Double, Double, Boolean> func;
Expand All @@ -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;
}
}
Expand All @@ -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?");
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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?");
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -328,20 +331,20 @@ 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;
}

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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e40b85a

Please sign in to comment.