2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-10-16 19:59:39 +00:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2011-12-08 02:21:10 +00:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-03-19 22:39:51 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2015-11-07 17:23:40 +00:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2016-12-18 15:03:03 +00:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2014-02-02 16:07:32 +00:00
|
|
|
import net.ess3.api.MaxMoneyException;
|
2019-12-23 13:16:34 +00:00
|
|
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.math.BigDecimal;
|
2017-06-11 00:17:43 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-08-11 18:09:22 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
|
|
|
|
public class Commandpay extends EssentialsLoopCommand {
|
|
|
|
public Commandpay() {
|
|
|
|
super("pay");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
if (args.length < 2) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
2016-11-21 18:05:17 +00:00
|
|
|
|
|
|
|
if (args[1].contains("-")) {
|
|
|
|
throw new Exception(tl("payMustBePositive"));
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
String stringAmount = args[1].replaceAll("[^0-9\\.]", "");
|
|
|
|
|
|
|
|
if (stringAmount.length() < 1) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:09:22 +00:00
|
|
|
final BigDecimal amount = new BigDecimal(stringAmount);
|
2015-11-07 17:23:40 +00:00
|
|
|
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)));
|
|
|
|
}
|
2020-08-11 18:09:22 +00:00
|
|
|
final AtomicBoolean informToConfirm = new AtomicBoolean(false);
|
|
|
|
loopOnlinePlayersConsumer(server, user.getSource(), false, user.isAuthorized("essentials.pay.multiple"), args[0], player -> {
|
|
|
|
try {
|
|
|
|
if (!player.isAcceptingPay() || (ess.getSettings().isPayExcludesIgnoreList() && player.isIgnoredPlayer(user))) {
|
|
|
|
user.sendMessage(tl("notAcceptingPay", player.getDisplayName()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (user.isPromptingPayConfirm() && !amount.equals(user.getConfirmingPayments().get(player))) { // checks if exists and if command needs to be repeated.
|
|
|
|
// Used to reset confirmations and inform to confirm when a new pay command has been inserted.
|
|
|
|
if (!informToConfirm.get()) {
|
|
|
|
// User hasnt been asked to confirm payment to this player, reset all confirmed payments and ask to confirm again.
|
|
|
|
// Clear previous confirmations to ensure that a new confirmation message is brought up.
|
|
|
|
user.getConfirmingPayments().clear();
|
|
|
|
informToConfirm.set(true);
|
|
|
|
}
|
|
|
|
user.getConfirmingPayments().put(player, amount);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user.payUser(player, amount, UserBalanceUpdateEvent.Cause.COMMAND_PAY);
|
|
|
|
user.getConfirmingPayments().remove(player);
|
|
|
|
Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
|
|
|
|
} catch (MaxMoneyException ex) {
|
|
|
|
user.sendMessage(tl("maxMoney"));
|
|
|
|
try {
|
|
|
|
user.setMoney(user.getMoney().add(amount));
|
|
|
|
} catch (MaxMoneyException ignored) {
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
user.sendMessage(e.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (informToConfirm.get()) {
|
2020-04-25 12:08:57 +00:00
|
|
|
String cmd = "/" + commandLabel + " " + StringUtil.joinList(" ", args);
|
2016-12-18 15:03:03 +00:00
|
|
|
user.sendMessage(tl("confirmPayment", NumberUtil.displayCurrency(amount, ess), cmd));
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
@Override
|
2020-08-11 18:09:22 +00:00
|
|
|
protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) {
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
return getPlayers(server, sender);
|
|
|
|
} else if (args.length == 2) {
|
|
|
|
return Lists.newArrayList(ess.getSettings().getMinimumPayAmount().toString());
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|