From ad5f17481c37ffc7ed51d2ea0c5c2942acca31ea Mon Sep 17 00:00:00 2001 From: Alex <63587318+alexanderdidio@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:05:30 -0700 Subject: [PATCH] Add abbreviation support to /pay command (#5457) Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com> --- .../essentials/commands/Commandpay.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpay.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpay.java index ecc6a4207..444f80e77 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpay.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpay.java @@ -18,6 +18,11 @@ import java.util.concurrent.atomic.AtomicBoolean; import static com.earth2me.essentials.I18n.tl; public class Commandpay extends EssentialsLoopCommand { + private static final BigDecimal THOUSAND = new BigDecimal(1000); + private static final BigDecimal MILLION = new BigDecimal(1_000_000); + private static final BigDecimal BILLION = new BigDecimal(1_000_000_000); + private static final BigDecimal TRILLION = new BigDecimal(1_000_000_000_000L); + public Commandpay() { super("pay"); } @@ -28,17 +33,43 @@ public class Commandpay extends EssentialsLoopCommand { throw new NotEnoughArgumentsException(); } - if (args[1].contains("-")) { + final String ogStr = args[1]; + + if (ogStr.contains("-")) { throw new Exception(tl("payMustBePositive")); } - final String stringAmount = args[1].replaceAll("[^0-9\\.]", ""); + final String sanitizedString = ogStr.replaceAll("[^0-9.]", ""); - if (stringAmount.length() < 1) { + if (sanitizedString.isEmpty()) { throw new NotEnoughArgumentsException(); } - final BigDecimal amount = new BigDecimal(stringAmount); + BigDecimal tempAmount = new BigDecimal(sanitizedString); + switch (Character.toLowerCase(ogStr.charAt(ogStr.length() - 1))) { + case 'k': { + tempAmount = tempAmount.multiply(THOUSAND); + break; + } + case 'm': { + tempAmount = tempAmount.multiply(MILLION); + break; + } + case 'b': { + tempAmount = tempAmount.multiply(BILLION); + break; + } + case 't': { + tempAmount = tempAmount.multiply(TRILLION); + break; + } + default: { + break; + } + } + + final BigDecimal amount = tempAmount; + if (amount.compareTo(ess.getSettings().getMinimumPayAmount()) < 0) { // Check if amount is less than minimum-pay-amount throw new Exception(tl("minimumPayAmount", NumberUtil.displayCurrencyExactly(ess.getSettings().getMinimumPayAmount(), ess))); }