2011-03-19 23:39:51 +01:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
import com.earth2me.essentials.ChargeException;
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2011-11-18 18:42:26 +01:00
|
|
|
import com.earth2me.essentials.User;
|
2013-06-08 23:31:19 +02:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2017-06-11 02:17:43 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2014-02-02 17:07:32 +01:00
|
|
|
import net.ess3.api.MaxMoneyException;
|
2019-12-23 14:16:34 +01:00
|
|
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
2011-03-19 23:39:51 +01:00
|
|
|
import org.bukkit.Server;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.math.BigDecimal;
|
2017-06-11 02:17:43 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
2011-03-19 23:39:51 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public class Commandeco extends EssentialsLoopCommand {
|
|
|
|
|
|
|
|
public Commandeco() {
|
|
|
|
super("eco");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
if (args.length < 2) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
EcoCommands cmd;
|
|
|
|
boolean isPercent;
|
|
|
|
BigDecimal amount;
|
2015-04-15 06:06:16 +02:00
|
|
|
try {
|
2020-08-11 20:09:22 +02:00
|
|
|
cmd = EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH));
|
2020-05-28 20:44:46 +02:00
|
|
|
isPercent = cmd != EcoCommands.RESET && args[2].endsWith("%");
|
2020-08-11 20:09:22 +02:00
|
|
|
amount = (cmd == EcoCommands.RESET) ? ess.getSettings().getStartingBalance() : new BigDecimal(args[2].replaceAll("[^0-9\\.]", ""));
|
2015-04-15 06:06:16 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new NotEnoughArgumentsException(ex);
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
loopOfflinePlayersConsumer(server, sender, false, true, args[1], player -> {
|
|
|
|
BigDecimal userAmount = amount;
|
|
|
|
if (isPercent) {
|
|
|
|
userAmount = player.getMoney().multiply(userAmount).scaleByPowerOfTen(-2);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
switch (cmd) {
|
|
|
|
case GIVE: {
|
|
|
|
player.giveMoney(userAmount, sender, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TAKE: {
|
|
|
|
if (player.getMoney().subtract(userAmount).compareTo(ess.getSettings().getMinMoney()) >= 0) {
|
|
|
|
player.takeMoney(userAmount, sender, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
|
|
|
} else {
|
|
|
|
ess.showError(sender, new Exception(tl("minimumBalanceError", NumberUtil.displayCurrency(ess.getSettings().getMinMoney(), ess))), commandLabel);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RESET:
|
|
|
|
case SET: {
|
|
|
|
BigDecimal minBal = ess.getSettings().getMinMoney();
|
|
|
|
BigDecimal maxBal = ess.getSettings().getMaxMoney();
|
|
|
|
boolean underMin = (userAmount.compareTo(minBal) < 0);
|
|
|
|
boolean aboveMax = (userAmount.compareTo(maxBal) > 0);
|
|
|
|
player.setMoney(underMin ? minBal : aboveMax ? maxBal : userAmount, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
|
|
|
player.sendMessage(tl("setBal", NumberUtil.displayCurrency(player.getMoney(), ess)));
|
|
|
|
sender.sendMessage(tl("setBalOthers", player.getDisplayName(), NumberUtil.displayCurrency(player.getMoney(), ess)));
|
|
|
|
break;
|
|
|
|
}
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2020-08-11 20:09:22 +02:00
|
|
|
});
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private enum EcoCommands {
|
|
|
|
GIVE, TAKE, SET, RESET
|
|
|
|
}
|
2017-06-11 02:17:43 +02:00
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
@Override
|
|
|
|
protected void updatePlayer(Server server, CommandSource sender, User user, String[] args) throws NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-11 02:17:43 +02:00
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(Server server, final CommandSource sender, String commandLabel, String[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
List<String> options = Lists.newArrayList();
|
|
|
|
for (EcoCommands command : EcoCommands.values()) {
|
|
|
|
options.add(command.name().toLowerCase(Locale.ENGLISH));
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
} else if (args.length == 2) {
|
|
|
|
return getPlayers(server, sender);
|
|
|
|
} else if (args.length == 3 && !args[0].equalsIgnoreCase(EcoCommands.RESET.name())) {
|
|
|
|
if (args[0].equalsIgnoreCase(EcoCommands.SET.name())) {
|
|
|
|
return Lists.newArrayList("0", ess.getSettings().getStartingBalance().toString());
|
|
|
|
} else {
|
|
|
|
return Lists.newArrayList("1", "10", "100", "1000");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|