summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandeco.java
blob: 0ebf4c04fcf5cf93ee2a8d364278673b9d16174d (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
125
126
127
package net.ess3.commands;

import java.util.Locale;
import static net.ess3.I18n._;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class Commandeco extends EssentialsCommand
{
	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new NotEnoughArgumentsException();
		}
		final EcoCommands cmd;
		final double amount;
		try
		{
			cmd = EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH));
			amount = Double.parseDouble(args[2].replaceAll("[^0-9\\.]", ""));
		}
		catch (Exception ex)
		{
			throw new NotEnoughArgumentsException(ex);
		}

		if (args[1].contentEquals("**"))
		{
			boolean ecoResetAll = false;
			ISettings settings = ess.getSettings();
			for (String sUser : ess.getUserMap().getAllUniqueUsers())
			{
				final IUser player = ess.getUserMap().getUser(sUser);
				switch (cmd)
				{
				case GIVE:
					player.giveMoney(amount);
					break;

				case TAKE:
					if (player.canAfford(amount, false))
					{
						player.takeMoney(amount);
					}
					break;

				case RESET:
					player.setMoney(amount == 0 ? settings.getData().getEconomy().getStartingBalance() : amount);
					ecoResetAll = true;
					break;
				}
			}
			if (ecoResetAll == true)
			{
				ess.broadcastMessage(null, _("ecoResetAll", settings.getData().getEconomy().getCurrencySymbol() + amount));
			}


		}
		else if (args[1].contentEquals("*"))
		{
			boolean ecoResetAllOnline = false;
			final ISettings settings = ess.getSettings();
			for (Player onlinePlayer : server.getOnlinePlayers())
			{
				final IUser player = ess.getUserMap().getUser(onlinePlayer);
				switch (cmd)
				{
				case GIVE:
					player.giveMoney(amount);
					break;

				case TAKE:
					if (!player.canAfford(amount, false))
					{
						throw new Exception(_("§4You do not have sufficient funds."));
					}
					player.takeMoney(amount);
					break;

				case RESET:
					player.setMoney(amount == 0 ? settings.getData().getEconomy().getStartingBalance() : amount);
					ecoResetAllOnline = true;
					break;
				}
			}
			if (ecoResetAllOnline == true)
			{
				ess.broadcastMessage(null, _("ecoResetAllOnline", settings.getData().getEconomy().getCurrencySymbol() + amount));
			}
		}
		else
		{
			final IUser player = ess.getUserMap().matchUser(args[1], true);
			switch (cmd)
			{
			case GIVE:
				player.giveMoney(amount, sender);
				break;

			case TAKE:
				if (!player.canAfford(amount, false))
				{
					throw new Exception(_("§4You do not have sufficient funds."));
				}
				player.takeMoney(amount, sender);
				break;

			case RESET:
				ISettings settings = ess.getSettings();
				player.setMoney(amount == 0 ? settings.getData().getEconomy().getStartingBalance() : amount);
				break;
			}
		}
	}


	private enum EcoCommands
	{
		GIVE, TAKE, RESET
	}
}