Add abbreviation support to /pay command (#5457)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
Alex 2023-08-08 14:05:30 -07:00 committed by GitHub
parent 0a4cf27342
commit ad5f17481c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 4 deletions

View File

@ -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)));
}