summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandtime.java
blob: c8f3911c408a09c36e3dbd51ec83b3844ba050ab (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
package net.ess3.commands;

import java.util.*;
import static net.ess3.I18n._;
import net.ess3.permissions.Permissions;
import net.ess3.utils.DescParseTickFormat;
import net.ess3.utils.Util;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class Commandtime extends EssentialsCommand
{
	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		boolean add = false;
		final List<String> argList = new ArrayList<String>(Arrays.asList(args));
		if (argList.remove("set") && !argList.isEmpty() && Util.isInt(argList.get(0)))
		{
			argList.set(0, argList.get(0) + "t");
		}
		if (argList.remove("add") && !argList.isEmpty() && Util.isInt(argList.get(0)))
		{
			add = true;
			argList.set(0, argList.get(0) + "t");
		}
		final String[] validArgs = argList.toArray(new String[0]);

		// Which World(s) are we interested in?
		String worldSelector = null;
		if (validArgs.length == 2)
		{
			worldSelector = validArgs[1];
		}
		final Set<World> worlds = getWorlds(sender, worldSelector);

		// If no arguments we are reading the time
		if (validArgs.length == 0)
		{
			getWorldsTime(sender, worlds);
			return;
		}

		if (!Permissions.TIME_SET.isAuthorized(sender))
		{
			sender.sendMessage(_("You are not authorized to set the time."));
			return;
		}

		// Parse the target time int ticks from args[0]
		final long ticks;
		try
		{
			ticks = DescParseTickFormat.parse(validArgs[0]);
		}
		catch (NumberFormatException e)
		{
			throw new NotEnoughArgumentsException(e);
		}

		setWorldsTime(sender, worlds, ticks, add);
	}

	/**
	 * Used to get the time and inform
	 */
	private void getWorldsTime(final CommandSender sender, final Collection<World> worlds)
	{
		if (worlds.size() == 1)
		{
			final Iterator<World> iter = worlds.iterator();
			sender.sendMessage(DescParseTickFormat.format(iter.next().getTime()));
			return;
		}

		for (World world : worlds)
		{
			sender.sendMessage(_("The current time in {0} is {1}.", world.getName(), DescParseTickFormat.format(world.getTime())));
		}
	}

	/**
	 * Used to set the time and inform of the change
	 */
	private void setWorldsTime(final CommandSender sender, final Collection<World> worlds, final long ticks, final boolean add)
	{
		// Update the time
		Iterator<World> iterator = worlds.iterator();
		while (iterator.hasNext())
		{
			World world = iterator.next();
			if (!Permissions.TIME_WORLDS.isAuthorized(sender, world.getName()))
			{
				iterator.remove();
				sender.sendMessage(_("timeWorldFailed", world.getName()));
				continue;
			}
			long time = world.getTime();
			if (!add)
			{
				time -= time % 24000;
			}
			world.setTime(time + (add ? 0 : 24000) + ticks);
		}

		if (worlds.isEmpty())
		{
			return;
		}
		final StringBuilder output = new StringBuilder();
		for (World world : worlds)
		{
			if (output.length() > 0)
			{
				output.append(", ");
			}

			output.append(world.getName());
		}

		sender.sendMessage(_("The time was set to {0} in: {1}.", DescParseTickFormat.format(ticks), output.toString()));
	}

	/**
	 * Used to parse an argument of the type "world(s) selector"
	 */
	private Set<World> getWorlds(final CommandSender sender, final String selector) throws Exception
	{
		final Set<World> worlds = new TreeSet<World>(new WorldNameComparator());

		// If there is no selector we want the world the user is currently in. Or all worlds if it isn't a user.
		if (selector == null)
		{
			final Player user = getPlayerOrNull(sender);
			if (user == null)
			{
				worlds.addAll(server.getWorlds());
			}
			else
			{
				worlds.add(user.getWorld());
			}
			return worlds;
		}

		// Try to find the world with name = selector
		final World world = server.getWorld(selector);
		if (world != null)
		{
			worlds.add(world);
		}
		// If that fails, Is the argument something like "*" or "all"?
		else if (selector.equalsIgnoreCase("*") || selector.equalsIgnoreCase("all"))
		{
			worlds.addAll(server.getWorlds());
		}
		// We failed to understand the world target...
		else
		{
			throw new Exception(_("Invalid world."));
		}

		return worlds;
	}
}


class WorldNameComparator implements Comparator<World>
{
	@Override
	public int compare(final World a, final World b)
	{
		return a.getName().compareTo(b.getName());
	}
}