summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandeco.java
blob: f589dcfed24094c108ec89c24e50f03759de2578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.earth2me.essentials.commands;

import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal;
import java.util.Locale;
import net.ess3.api.MaxMoneyException;
import org.bukkit.Server;


public class Commandeco extends EssentialsLoopCommand
{
	Commandeco.EcoCommands cmd;
	BigDecimal amount;

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

		BigDecimal startingBalance = ess.getSettings().getStartingBalance();

		try
		{
			cmd = Commandeco.EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH));
			amount = (cmd == Commandeco.EcoCommands.RESET) ? startingBalance : new BigDecimal(args[2].replaceAll("[^0-9\\.]", ""));
		}
		catch (Exception ex)
		{
			throw new NotEnoughArgumentsException(ex);
		}

		loopOfflinePlayers(server, sender, false, true, args[1], args);

		if (cmd == Commandeco.EcoCommands.RESET || cmd == Commandeco.EcoCommands.SET)
		{
			if (args[1].contentEquals("**"))
			{
				server.broadcastMessage(tl("resetBalAll", NumberUtil.displayCurrency(amount, ess)));
			}
			else if (args[1].contentEquals("*"))
			{
				server.broadcastMessage(tl("resetBal", NumberUtil.displayCurrency(amount, ess)));
			}
		}
	}

	@Override
	protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) throws NotEnoughArgumentsException, ChargeException, MaxMoneyException
	{
		switch (cmd)
		{
		case GIVE:
			player.giveMoney(amount, sender);
			break;

		case TAKE:
			take(amount, player, sender);
			break;

		case RESET:
		case SET:
			set(amount, player, sender);
			break;
		}
	}

	private void take(BigDecimal amount, final User player, final CommandSource sender) throws ChargeException
	{
		BigDecimal money = player.getMoney();
		BigDecimal minBalance = ess.getSettings().getMinMoney();
		if (money.subtract(amount).compareTo(minBalance) > 0)
		{
			player.takeMoney(amount, sender);
		}
		else if (sender == null)
		{
			try
			{
				player.setMoney(minBalance);
			}
			catch (MaxMoneyException ex)
			{
				// Take shouldn't be able to throw a max money exception
			}
			player.sendMessage(tl("takenFromAccount", NumberUtil.displayCurrency(player.getMoney(), ess)));
		}
		else
		{
			throw new ChargeException(tl("insufficientFunds"));
		}
	}

	private void set(BigDecimal amount, final User player, final CommandSource sender) throws MaxMoneyException
	{
		BigDecimal minBalance = ess.getSettings().getMinMoney();
		BigDecimal maxBalance = ess.getSettings().getMaxMoney();
		boolean underMinimum = (amount.compareTo(minBalance) < 0);
		boolean aboveMax = (amount.compareTo(maxBalance) > 0);
		player.setMoney(underMinimum ? minBalance : aboveMax ? maxBalance : amount);
		player.sendMessage(tl("setBal", NumberUtil.displayCurrency(player.getMoney(), ess)));
		if (sender != null)
		{
			sender.sendMessage(tl("setBalOthers", player.getDisplayName(), NumberUtil.displayCurrency(player.getMoney(), ess)));
		}
	}


	private enum EcoCommands
	{
		GIVE, TAKE, SET, RESET
	}
}