Skip to content

Commit

Permalink
Add abbreviation support to /pay command (EssentialsX#5457)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Roy <[email protected]>
  • Loading branch information
alexanderdidio and JRoy authored Aug 8, 2023
1 parent 0a4cf27 commit ad5f174
Showing 1 changed file with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
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");
}
Expand All @@ -28,17 +33,43 @@ public void run(final Server server, final User user, final String commandLabel,
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)));
}
Expand Down

0 comments on commit ad5f174

Please sign in to comment.