From 95de939289d41258affbad9bcc900fec3ffb3459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ko=C5=82ecki?= <61193738+arthurr0@users.noreply.github.com> Date: Thu, 10 Mar 2022 19:23:49 +0100 Subject: [PATCH] Rounded scale (#13) * #11 Fixed rounded money. * Removed old formatting type. --- .../minecodes/mineeconomy/EconomyPlugin.java | 8 +++--- .../admin/sub/EconomyClearCommand.java | 2 +- .../admin/sub/EconomyDepositCommand.java | 4 +-- .../command/admin/sub/EconomySetCommand.java | 4 +-- .../admin/sub/EconomyWithdrawCommand.java | 4 +-- .../command/argument/DoubleArgument.java | 6 ++++- .../command/player/TransferCommand.java | 6 ++--- .../data/configuration/Configuration.java | 10 ++++++++ .../mineeconomy/hook/vault/VaultManager.java | 25 +++++++++++-------- .../mineeconomy/profile/Profile.java | 17 ++++++++----- 10 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java b/src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java index c6b22b3..2f1e573 100644 --- a/src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java +++ b/src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java @@ -33,12 +33,8 @@ import pl.minecodes.mineeconomy.profile.ProfileService; import pl.minecodes.mineeconomy.runnable.ProfileSaveTask; -import java.text.DecimalFormat; - public class EconomyPlugin extends JavaPlugin { - public static final DecimalFormat FORMAT = new DecimalFormat("#.##"); - private Injector injector; private Messages messages; private Configuration configuration; @@ -91,6 +87,9 @@ public void onEnable() { @Override public void onDisable() { + if (this.liteCommands != null) { + this.liteCommands.getPlatformManager().unregisterCommands(); + } vaultHook.unregisterHook(); } @@ -117,4 +116,5 @@ private void registerCommands() { .message(ValidationInfo.INVALID_USE, messageInfoContext -> this.messages.getCommandUsage().replace("{usage}", messageInfoContext.getUseScheme())) .register(); } + } diff --git a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyClearCommand.java b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyClearCommand.java index c3e762e..5e53381 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyClearCommand.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyClearCommand.java @@ -40,6 +40,6 @@ public void done() { public void cancel(CancelReason reason) { MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative()); } - }); + }, this.configuration.getRoundedScale()); } } diff --git a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyDepositCommand.java b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyDepositCommand.java index adf3ae8..74a85ce 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyDepositCommand.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyDepositCommand.java @@ -30,7 +30,7 @@ public class EconomyDepositCommand { @Execute(route = "deposit", required = 2) public void executeDeposit(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double value) { - AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value))); + AtomicDouble atomicValue = new AtomicDouble(value); Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId()); profile.deposit(atomicValue.get(), new BalanceOperationCallback() { @@ -47,6 +47,6 @@ public void done() { public void cancel(CancelReason reason) { MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative()); } - }); + }, this.configuration.getRoundedScale()); } } diff --git a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomySetCommand.java b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomySetCommand.java index 501f6df..fedf2d6 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomySetCommand.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomySetCommand.java @@ -30,7 +30,7 @@ public class EconomySetCommand { @Execute(route = "set", required = 2) public void executeSet(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double balance) { - AtomicDouble atomicBalance = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(balance))); + AtomicDouble atomicBalance = new AtomicDouble(balance); Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId()); profile.setupBalance(atomicBalance.get(), new BalanceOperationCallback() { @@ -47,6 +47,6 @@ public void done() { public void cancel(CancelReason reason) { MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative()); } - }); + }, this.configuration.getRoundedScale()); } } diff --git a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyWithdrawCommand.java b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyWithdrawCommand.java index eeb817f..be71afd 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyWithdrawCommand.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/admin/sub/EconomyWithdrawCommand.java @@ -30,7 +30,7 @@ public class EconomyWithdrawCommand { @Execute(route = "withdraw", required = 2) public void executeWithdraw(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double value) { - AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value))); + AtomicDouble atomicValue = new AtomicDouble(value); Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId()); profile.withdraw(atomicValue.get(), new BalanceOperationCallback() { @@ -57,6 +57,6 @@ public void cancel(CancelReason reason) { break; } } - }); + }, this.configuration.getRoundedScale()); } } diff --git a/src/main/java/pl/minecodes/mineeconomy/command/argument/DoubleArgument.java b/src/main/java/pl/minecodes/mineeconomy/command/argument/DoubleArgument.java index 000d4bb..4e2fa68 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/argument/DoubleArgument.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/argument/DoubleArgument.java @@ -5,8 +5,11 @@ import dev.rollczi.litecommands.argument.SingleArgumentHandler; import dev.rollczi.litecommands.valid.ValidationCommandException; import eu.okaeri.injector.annotation.Inject; +import pl.minecodes.mineeconomy.data.configuration.Configuration; import pl.minecodes.mineeconomy.data.configuration.Messages; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Arrays; import java.util.List; @@ -14,6 +17,7 @@ public class DoubleArgument implements SingleArgumentHandler { @Inject private Messages messages; + @Inject private Configuration configuration; @Override public Double parse(LiteInvocation invocation, String argument) throws ValidationCommandException { @@ -24,7 +28,7 @@ public Double parse(LiteInvocation invocation, String argument) throws Validatio throw new ValidationCommandException(this.messages.getBalanceOperationParameterIsNotCorrect()); } - return parseDouble; + return new BigDecimal(parseDouble).setScale(this.configuration.getRoundedScale(), RoundingMode.HALF_UP).doubleValue(); } @Override diff --git a/src/main/java/pl/minecodes/mineeconomy/command/player/TransferCommand.java b/src/main/java/pl/minecodes/mineeconomy/command/player/TransferCommand.java index 883fadd..d6b595c 100644 --- a/src/main/java/pl/minecodes/mineeconomy/command/player/TransferCommand.java +++ b/src/main/java/pl/minecodes/mineeconomy/command/player/TransferCommand.java @@ -27,7 +27,7 @@ public class TransferCommand { @Execute(required = 2) public void executeTransfer(Player sender, @Arg(0) Player target, @Arg(1) Double value) { - AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value))); + AtomicDouble atomicValue = new AtomicDouble(value); Profile senderProfile = this.profileService.getProfile(sender.getUniqueId()); if (!(senderProfile.has(atomicValue.get()))) { @@ -46,7 +46,7 @@ public void done() { public void cancel(CancelReason reason) { //ignore } - }); + }, this.configuration.getRoundedScale()); targetProfile.deposit(atomicValue.get(), new BalanceOperationCallback() { @Override @@ -70,7 +70,7 @@ public void done() { public void cancel(CancelReason reason) { MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative()); } - }); + }, this.configuration.getRoundedScale()); } } diff --git a/src/main/java/pl/minecodes/mineeconomy/data/configuration/Configuration.java b/src/main/java/pl/minecodes/mineeconomy/data/configuration/Configuration.java index 3ce41c5..993e2f1 100644 --- a/src/main/java/pl/minecodes/mineeconomy/data/configuration/Configuration.java +++ b/src/main/java/pl/minecodes/mineeconomy/data/configuration/Configuration.java @@ -14,6 +14,8 @@ public class Configuration extends OkaeriConfig { private CurrencyPositionVault currencyPositionVault = CurrencyPositionVault.BEHIND; @Comment("Początkowy stan konta gracza.") private double startBalance = 10; + @Comment("Do ilu liczb po przecinku powinna być zaokrąglana kwota.") + private int roundedScale = 2; private DatabaseData databaseData = new DatabaseData(); @@ -62,4 +64,12 @@ public void setDatabaseData(DatabaseData databaseData) { } public enum CurrencyPositionVault {AHEAD, BEHIND} + + public int getRoundedScale() { + return roundedScale; + } + + public void setRoundedScale(int roundedScale) { + this.roundedScale = roundedScale; + } } diff --git a/src/main/java/pl/minecodes/mineeconomy/hook/vault/VaultManager.java b/src/main/java/pl/minecodes/mineeconomy/hook/vault/VaultManager.java index 2c551ed..89eba59 100644 --- a/src/main/java/pl/minecodes/mineeconomy/hook/vault/VaultManager.java +++ b/src/main/java/pl/minecodes/mineeconomy/hook/vault/VaultManager.java @@ -11,6 +11,8 @@ import pl.minecodes.mineeconomy.profile.ProfileService; import pl.minecodes.mineeconomy.profile.helper.BalanceOperationCallback; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Collections; import java.util.List; @@ -43,13 +45,14 @@ public int fractionalDigits() { @Override public String format(double value) { + double doubleValue = new BigDecimal(value).setScale(this.configuration.getRoundedScale(), RoundingMode.HALF_UP).doubleValue(); switch (this.configuration.getCurrencyPositionVault()) { case AHEAD: - return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value); + return this.configuration.getCurrency(doubleValue) + doubleValue; case BEHIND: - return EconomyPlugin.FORMAT.format(value) + this.configuration.getCurrency(value); + return doubleValue + this.configuration.getCurrency(doubleValue); } - return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value); + return this.configuration.getCurrency(doubleValue) + doubleValue; } @Override @@ -162,7 +165,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -181,7 +184,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -205,7 +208,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -224,7 +227,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -248,7 +251,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -267,7 +270,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -291,7 +294,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } @@ -310,7 +313,7 @@ public void done() { public void cancel(CancelReason reason) { economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString())); } - }); + }, this.configuration.getRoundedScale()); return economyResponse.get(); } diff --git a/src/main/java/pl/minecodes/mineeconomy/profile/Profile.java b/src/main/java/pl/minecodes/mineeconomy/profile/Profile.java index 344f241..9f5bd01 100644 --- a/src/main/java/pl/minecodes/mineeconomy/profile/Profile.java +++ b/src/main/java/pl/minecodes/mineeconomy/profile/Profile.java @@ -5,6 +5,8 @@ import pl.minecodes.mineeconomy.data.configuration.Configuration; import pl.minecodes.mineeconomy.profile.helper.BalanceOperationCallback; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.UUID; public class Profile { @@ -37,18 +39,19 @@ public double getBalance() { return balance; } - public void deposit(double money, BalanceOperationCallback callback) { + public void deposit(double money, BalanceOperationCallback callback, int roundedScale) { if (money < 0) { callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_PARAMETER); return; } callback.done(); - this.balance += money; + BigDecimal decimal = new BigDecimal(money).setScale(roundedScale, RoundingMode.HALF_UP); + this.balance += decimal.doubleValue(); this.needUpdate = true; } - public void withdraw(double money, BalanceOperationCallback callback) { + public void withdraw(double money, BalanceOperationCallback callback, int roundedScale) { if (this.balance < 0) { callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_BALANCE); return; @@ -63,18 +66,20 @@ public void withdraw(double money, BalanceOperationCallback callback) { } callback.done(); - this.balance -= money; + BigDecimal decimal = new BigDecimal(money).setScale(roundedScale, RoundingMode.HALF_UP); + this.balance -= decimal.doubleValue(); this.needUpdate = true; } - public void setupBalance(double balance, BalanceOperationCallback callback) { + public void setupBalance(double balance, BalanceOperationCallback callback, int roundedScale) { if (balance < 0) { callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_PARAMETER); return; } callback.done(); - this.balance = balance; + BigDecimal decimal = new BigDecimal(balance).setScale(roundedScale, RoundingMode.HALF_UP); + this.balance = decimal.doubleValue(); this.needUpdate = true; }