From 0cd8e50f9c344fe12bbbe6c177fe3e47de6165c2 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Sat, 26 Aug 2023 22:52:56 +0200 Subject: [PATCH] feat: handle pay + improve tax system --- .../lighteco/api/model/currency/Currency.java | 8 +++-- .../bukkit/manager/BukkitCommandManager.java | 30 ++++++++++++++++++- .../lighteco/bukkittest/TestCurrency.java | 5 ---- .../lighteco/bukkittest/TestCurrency2.java | 5 ++-- .../command/AbstractCommandManager.java | 27 +++++++++++++++++ .../manager/command/CommandManager.java | 1 + 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java index 864af32..24c97aa 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java @@ -1,5 +1,7 @@ package dev.xhyrom.lighteco.api.model.currency; +import dev.xhyrom.lighteco.api.model.user.User; + import java.math.BigDecimal; public abstract class Currency { @@ -36,10 +38,10 @@ public abstract class Currency { * Used for payables * * @param amount The amount to calculate the tax for - * @return The tax + * @return Amount that should be taxed */ - public BigDecimal calculateTax(BigDecimal amount) { - return getDefaultBalance(); + public BigDecimal calculateTax(User user, BigDecimal amount) { + return BigDecimal.ZERO; }; /** diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java index 45c6513..2b5a549 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java @@ -91,6 +91,18 @@ public class BukkitCommandManager extends AbstractCommandManager { this.handleTake(sender, args, currency); }) ) + .withSubcommand(new CommandAPICommand("pay") + .withPermission(permissionBase + "pay") + .withArguments( + new OfflinePlayerArgument("target"), + currency.getProxy().getDecimalPlaces() > 0 + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) + ) + .executesPlayer((sender, args) -> { + this.handlePay(sender, args, currency); + }) + ) .withSubcommand(balanceCommands.get(0)) .withSubcommands(balanceCommands.get(1)) .register(); @@ -147,7 +159,7 @@ public class BukkitCommandManager extends AbstractCommandManager { }); } - public void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) { + private void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) { BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); OfflinePlayer target = (OfflinePlayer) args.get("target"); BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); @@ -162,4 +174,20 @@ public class BukkitCommandManager extends AbstractCommandManager { this.onTake(sender, currency, result, amount); }); } + + private void handlePay(CommandSender originalSender, CommandArguments args, Currency currency) { + BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); + OfflinePlayer target = (OfflinePlayer) args.get("target"); + BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); + + if (!this.canUse(sender)) return; + + this.plugin.getUserManager().loadUser(target.getUniqueId()) + .thenAcceptAsync(result -> { + String name = target.getName() != null ? target.getName() : args.getRaw("target"); + result.setUsername(name); + + this.onPay(sender, currency, result, amount); + }); + } } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java index 6548afc..96fceee 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java @@ -24,9 +24,4 @@ public class TestCurrency extends Currency { public BigDecimal getDefaultBalance() { return BigDecimal.ZERO; } - - @Override - public BigDecimal calculateTax(BigDecimal amount) { - return BigDecimal.ZERO; - } } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java index c7f6a22..d9e6bf4 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.bukkittest; import dev.xhyrom.lighteco.api.model.currency.Currency; +import dev.xhyrom.lighteco.api.model.user.User; import java.math.BigDecimal; @@ -31,7 +32,7 @@ public class TestCurrency2 extends Currency { } @Override - public BigDecimal calculateTax(BigDecimal amount) { - return BigDecimal.ZERO; + public BigDecimal calculateTax(User user, BigDecimal amount) { + return amount.multiply(BigDecimal.valueOf(0.2)); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java index 8cfc483..5c66ca5 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java @@ -10,6 +10,7 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.UUID; +import java.util.concurrent.CompletableFuture; public abstract class AbstractCommandManager implements CommandManager { protected final LightEcoPlugin plugin; @@ -110,4 +111,30 @@ public abstract class AbstractCommandManager implements CommandManager { this.plugin.getUserManager().saveUser(target) .thenAccept(v -> removeFromMustWait(target.getUniqueId(), sender.getUniqueId())); } + + @Override + public void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount) { + addToMustWait(sender.getUniqueId(), target.getUniqueId()); + + User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + + // calculate tax using Currency#calculateTax + BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount); + + // subtract tax from amount + amount = amount.subtract(tax); + + target.setBalance(currency, target.getBalance(currency).add(amount)); + user.setBalance(currency, user.getBalance(currency).subtract(amount)); + + // send message and also include tax rate (percentage) with tax amount + sender.sendMessage( + miniMessage.deserialize("Paid " + amount.toPlainString() + " " + currency.getIdentifier() + " to " + target.getUsername() + " with a tax rate of " + tax + "% (" + tax.toPlainString() + " " + currency.getIdentifier() + ")") + ); + + CompletableFuture.allOf( + this.plugin.getUserManager().saveUser(user), + this.plugin.getUserManager().saveUser(target) + ).thenAccept(v -> removeFromMustWait(sender.getUniqueId(), target.getUniqueId())); + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java index efa48e3..6b92aa2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java @@ -18,4 +18,5 @@ public interface CommandManager { void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount); void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount); void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount); + void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount); }