From c82b04970b979cc5584ffb2cc349ca7a138e4ea5 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Sun, 27 Aug 2023 18:01:16 +0200 Subject: [PATCH] fix: handle franctional digits --- .../lighteco/api/model/currency/Currency.java | 7 +++---- .../lighteco/bukkit/commands/BalanceCommand.java | 2 -- .../lighteco/bukkit/commands/GiveCommand.java | 2 +- .../lighteco/bukkit/commands/PayCommand.java | 2 +- .../lighteco/bukkit/commands/SetCommand.java | 2 +- .../lighteco/bukkit/commands/TakeCommand.java | 2 +- .../lighteco/bukkittest/TestCurrency2.java | 2 +- .../xhyrom/lighteco/bukkittest/TestPlugin.java | 2 +- .../manager/command/AbstractCommandManager.java | 16 ++++++++++++++-- .../currency/money/common/MoneyCurrency.java | 2 +- 10 files changed, 24 insertions(+), 15 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 24c97aa..7d18fd3 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 @@ -17,12 +17,11 @@ public abstract class Currency { public abstract boolean isPayable(); /** - * Get the number of decimal places this currency has - * If zero, the currency is considered to be a whole number (integer) + * Get the number of fractional digits this currency has * - * @return The number of decimal places + * @return The number of fractional digits */ - public int getDecimalPlaces() { + public int fractionalDigits() { return 0; }; diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java index 1fb2ac7..da8bf25 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java @@ -10,8 +10,6 @@ import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; -import java.util.List; - @RequiredArgsConstructor public class BalanceCommand implements Command { private final BukkitCommandManager manager; diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java index 1f921cb..e9b677a 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java @@ -26,7 +26,7 @@ public class GiveCommand implements Command { .withPermission(permissionBase + "give") .withArguments( new OfflinePlayerArgument("target"), - currency.getProxy().getDecimalPlaces() > 0 + currency.getProxy().fractionalDigits() > 0 ? new DoubleArgument("amount", 1) : new IntegerArgument("amount", 1) ) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java index cffda49..7cb02a0 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java @@ -26,7 +26,7 @@ public class PayCommand implements Command { .withPermission(permissionBase + "pay") .withArguments( new OfflinePlayerArgument("target"), - currency.getProxy().getDecimalPlaces() > 0 + currency.getProxy().fractionalDigits() > 0 ? new DoubleArgument("amount", 1) : new IntegerArgument("amount", 1) ) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java index 819f44b..c3fc918 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java @@ -26,7 +26,7 @@ public class SetCommand implements Command { .withPermission(permissionBase + "set") .withArguments( new OfflinePlayerArgument("target"), - currency.getProxy().getDecimalPlaces() > 0 + currency.getProxy().fractionalDigits() > 0 ? new DoubleArgument("amount", 0) : new IntegerArgument("amount", 0) ) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java index 08e3127..41cd7b5 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java @@ -26,7 +26,7 @@ public class TakeCommand implements Command { .withPermission(permissionBase + "take") .withArguments( new OfflinePlayerArgument("target"), - currency.getProxy().getDecimalPlaces() > 0 + currency.getProxy().fractionalDigits() > 0 ? new DoubleArgument("amount", 1) : new IntegerArgument("amount", 1) ) 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 d9e6bf4..ed03b04 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java @@ -22,7 +22,7 @@ public class TestCurrency2 extends Currency { } @Override - public int getDecimalPlaces() { + public int fractionalDigits() { return 2; } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java index b669671..212bea6 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java @@ -19,7 +19,7 @@ public class TestPlugin extends JavaPlugin { getLogger().info("TestCurrency registered!"); currencyManager.getRegisteredCurrencies().forEach(currency -> { - getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getDecimalPlaces() + ", " + currency.isPayable() + ")"); + getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.fractionalDigits() + ", " + currency.isPayable() + ")"); }); provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test")); 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 c3e2672..18141d3 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 @@ -7,6 +7,7 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import net.kyori.adventure.text.minimessage.MiniMessage; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.HashMap; import java.util.UUID; @@ -73,6 +74,7 @@ public abstract class AbstractCommandManager implements CommandManager { @Override public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) { addToMustWait(sender.getUniqueId(), target.getUniqueId()); + amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); target.setBalance(currency, amount); @@ -87,6 +89,7 @@ public abstract class AbstractCommandManager implements CommandManager { @Override public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) { addToMustWait(sender.getUniqueId(), target.getUniqueId()); + amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); target.setBalance(currency, target.getBalance(currency).add(amount)); @@ -101,6 +104,7 @@ public abstract class AbstractCommandManager implements CommandManager { @Override public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) { addToMustWait(sender.getUniqueId(), target.getUniqueId()); + amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); target.setBalance(currency, target.getBalance(currency).subtract(amount)); @@ -122,11 +126,19 @@ public abstract class AbstractCommandManager implements CommandManager { return; } - addToMustWait(sender.getUniqueId(), target.getUniqueId()); + amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); - // TODO: ADD CHECKS FOR IF USER HAS ENOUGH MONEY + if (user.getBalance(currency).compareTo(amount) < 0) { + sender.sendMessage( + miniMessage.deserialize("You do not have enough money!") + ); + + return; + } + + addToMustWait(sender.getUniqueId(), target.getUniqueId()); // calculate tax using Currency#calculateTax BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount); diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/MoneyCurrency.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/MoneyCurrency.java index 061ecbf..4c3db5a 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/MoneyCurrency.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/MoneyCurrency.java @@ -26,7 +26,7 @@ public class MoneyCurrency extends Currency { } @Override - public int getDecimalPlaces() { + public int fractionalDigits() { return 2; } }