summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandpweather.java
blob: 9aec1ff6b32bbefb57ffec639f71a50cb0ea677a (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import java.util.*;
import org.bukkit.Server;
import org.bukkit.WeatherType;
import org.bukkit.entity.Player;


public class Commandpweather extends EssentialsCommand
{
	public static final Set<String> getAliases = new HashSet<String>();
	public static final Map<String, WeatherType> weatherAliases = new HashMap<String, WeatherType>();

	static
	{
		getAliases.add("get");
		getAliases.add("list");
		getAliases.add("show");
		getAliases.add("display");
		weatherAliases.put("sun", WeatherType.CLEAR);
		weatherAliases.put("clear", WeatherType.CLEAR);
		weatherAliases.put("storm", WeatherType.DOWNFALL);
		weatherAliases.put("thunder", WeatherType.DOWNFALL);
	}

	public Commandpweather()
	{
		super("pweather");
	}

	@Override
	public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		// Which Players(s) / Users(s) are we interested in?
		String userSelector = null;
		if (args.length == 2)
		{
			userSelector = args[1];
		}
		Set<User> users = getUsers(server, sender, userSelector);

		if (args.length == 0)
		{
			getUsersWeather(sender, users);
			return;
		}

		if (getAliases.contains(args[0]))
		{
			getUsersWeather(sender, users);
			return;
		}

		if (sender.isPlayer())
		{
			User user = ess.getUser(sender.getPlayer());
			if (user != null && (!users.contains(user) || users.size() > 1) && !user.isAuthorized("essentials.pweather.others"))
			{
				user.sendMessage(tl("pWeatherOthersPermission"));
				return;
			}
		}

		setUsersWeather(sender, users, args[0].toLowerCase());
	}

	/**
	 * Used to get the time and inform
	 */
	private void getUsersWeather(final CommandSource sender, final Collection<User> users)
	{
		if (users.size() > 1)
		{
			sender.sendMessage(tl("pWeatherPlayers"));
		}

		for (User user : users)
		{
			if (user.getBase().getPlayerWeather() == null)
			{
				sender.sendMessage(tl("pWeatherNormal", user.getName()));
			}
			else
			{
				sender.sendMessage(tl("pWeatherCurrent", user.getName(), user.getBase().getPlayerWeather().toString().toLowerCase(Locale.ENGLISH)));
			}
		}
	}

	/**
	 * Used to set the time and inform of the change
	 */
	private void setUsersWeather(final CommandSource sender, final Collection<User> users, final String weatherType) throws Exception
	{

		final StringBuilder msg = new StringBuilder();
		for (User user : users)
		{
			if (msg.length() > 0)
			{
				msg.append(", ");
			}

			msg.append(user.getName());
		}

		if (weatherType.equalsIgnoreCase("reset"))
		{
			for (User user : users)
			{
				user.getBase().resetPlayerWeather();
			}

			sender.sendMessage(tl("pWeatherReset", msg));
		}
		else
		{
			if (!weatherAliases.containsKey(weatherType))
			{
				throw new NotEnoughArgumentsException(tl("pWeatherInvalidAlias"));
			}

			for (User user : users)
			{
				user.getBase().setPlayerWeather(weatherAliases.get(weatherType));
			}
			sender.sendMessage(tl("pWeatherSet", weatherType, msg.toString()));
		}
	}

	/**
	 * Used to parse an argument of the type "users(s) selector"
	 */
	private Set<User> getUsers(final Server server, final CommandSource sender, final String selector) throws Exception
	{
		final Set<User> users = new TreeSet<User>(new UserNameComparator());
		// If there is no selector we want the sender itself. Or all users if sender isn't a user.
		if (selector == null)
		{
			if (sender.isPlayer())
			{
				final User user = ess.getUser(sender.getPlayer());
				users.add(user);
			}
			else
			{
				for (User user : ess.getOnlineUsers())
				{
					users.add(user);
				}
			}
			return users;
		}

		// Try to find the user with name = selector
		User user = null;
		final List<Player> matchedPlayers = server.matchPlayer(selector);
		if (!matchedPlayers.isEmpty())
		{
			user = ess.getUser(matchedPlayers.get(0));
		}

		if (user != null)
		{
			users.add(user);
		}
		// If that fails, Is the argument something like "*" or "all"?
		else if (selector.equalsIgnoreCase("*") || selector.equalsIgnoreCase("all"))
		{
			for (Player player : server.getOnlinePlayers())
			{
				users.add(ess.getUser(player));
			}
		}
		// We failed to understand the world target...
		else
		{
			throw new PlayerNotFoundException();
		}

		return users;
	}
}