-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAdminCommands.java
More file actions
346 lines (282 loc) · 11.3 KB
/
AdminCommands.java
File metadata and controls
346 lines (282 loc) · 11.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package org.mctourney.autoreferee.commands;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
import org.apache.commons.lang.StringUtils;
import org.mctourney.autoreferee.AutoRefMap;
import org.mctourney.autoreferee.AutoRefMatch;
import org.mctourney.autoreferee.AutoRefTeam;
import org.mctourney.autoreferee.AutoReferee;
import org.mctourney.autoreferee.event.match.MatchUnloadEvent;
import org.mctourney.autoreferee.event.player.PlayerTeamJoinEvent.Reason;
import org.mctourney.autoreferee.util.commands.AutoRefCommand;
import org.mctourney.autoreferee.util.commands.AutoRefPermission;
import org.mctourney.autoreferee.util.commands.CommandHandler;
import org.apache.commons.cli.CommandLine;
import com.google.common.collect.Lists;
public class AdminCommands implements CommandHandler
{
AutoReferee plugin;
public AdminCommands(Plugin plugin)
{
this.plugin = (AutoReferee) plugin;
}
@AutoRefCommand(name={"autoref", "world"}, argmin=1, argmax=1,
description="Specifies the world for console commands to modify.",
usage="<command> <world or player name>")
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean setConsoleWorld(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
plugin.setConsoleWorld(args[0]);
World world = plugin.getConsoleWorld();
sender.sendMessage("Selected world: " + (world == null ? "<none>" : world.getName()));
return world != null;
}
@AutoRefCommand(name={"autoref", "setlobby"}, argmax=0,
description="Sets the current world to be the AutoReferee lobby world.")
@AutoRefPermission(console=false, nodes={"autoreferee.admin"})
public boolean setLobbyWorld(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (match != null) return false;
World lobby = ((Player) sender).getWorld();
plugin.setLobbyWorld(lobby);
sender.sendMessage(ChatColor.GREEN + lobby.getName() +
" is the new AutoReferee lobby world.");
return true;
}
@AutoRefCommand(name={"autoref", "setname"}, argmin=1, argmax=1,
description="Set the name of the current match used in the match report.",
usage="<command> <match title>")
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean setMatchName(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (match == null) return false;
match.setMatchName(args[0]);
return true;
}
@AutoRefCommand(name={"autoref", "load"}, argmin=1, options="c+x",
description="Loads a map by name, case insensitive.",
usage="<command> <map name>",
opthelp=
{
"c", "specify a custom world name",
"x", "load world without AutoReferee",
})
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean loadMap(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
// generate a map name from the args
String mapName = StringUtils.join(args, " ");
// may specify a custom world name
String customName = options.getOptionValue('c');
// get world setup for match
sender.sendMessage(ChatColor.GREEN + "Please wait...");
AutoRefMap.loadMap(sender, mapName, customName);
return true;
}
@AutoRefCommand(name={"autoref", "loadurl"}, argmin=1, argmax=1, options="c+x",
description="Loads a map from a remote zip file, taking the URL as a parameter.",
usage="<command> <url>",
opthelp=
{
"c", "specify a custom world name",
"x", "load world without AutoReferee",
})
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean loadMapFromURL(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
// may specify a custom world name
String customName = options.getOptionValue('c');
// get world setup for match
sender.sendMessage(ChatColor.GREEN + "Please wait...");
AutoRefMap.loadMapFromURL(sender, args[0], customName);
return true;
}
@AutoRefCommand(name={"autoref", "unload"}, argmin=0, argmax=0,
description="Unloads the current map. Connected players are either moved to the lobby or kicked.")
@AutoRefPermission(console=true)
public boolean unloadMap(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (match == null)
{
sender.sendMessage(ChatColor.GRAY + "No world to unload.");
return false;
}
if ((!match.isPracticeMode() || match.getCurrentState().isBeforeMatch())
&& !sender.hasPermission("autoreferee.admin")) return false;
if (match != null)
match.destroy(MatchUnloadEvent.Reason.COMMAND);
return true;
}
@AutoRefCommand(name={"autoref", "reload"}, options="x",
description="Reloads the current map (or specified map) to its original, unmodified state. Players are migrated to the new copy.",
usage="<command> [<map name>]",
opthelp=
{
"x", "transfer players to same teams and location",
})
@AutoRefPermission(console=true)
public boolean reloadMap(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
throws IOException
{
if (match == null) return false;
// in practice mode, this command is available to anyone
if ((!match.isPracticeMode() || match.getCurrentState().isBeforeMatch())
&& !sender.hasPermission("autoreferee.admin")) return false;
String name = match.getMapName();
if (args.length > 0) name = StringUtils.join(args, ' ');
AutoRefMap map = AutoRefMap.getMap(name);
if (map == null)
{
sender.sendMessage(ChatColor.DARK_GRAY +
"No archive of this map exists " + name);
return true;
}
sender.sendMessage(ChatColor.DARK_GRAY +
"Preparing a new copy of " + map.getVersionString());
AutoRefMatch newmatch = AutoRefMap.createMatch(map, null);
if (map.equals(match.getMap()) && options.hasOption('x'))
{
newmatch.setStartTime(match.getStartTime());
newmatch.setCurrentState(match.getCurrentState());
for (Player p : match.getWorld().getPlayers())
{
AutoRefTeam team = match.getPlayerTeam(p);
if (team != null)
{
AutoRefTeam newteam = newmatch.getTeam(team.getDefaultName());
newteam.join(p, Reason.AUTOMATIC, true);
}
// teleport the player to location in new map
Location loc = p.getLocation();
loc.setWorld(newmatch.getWorld());
p.teleport(loc);
}
}
else for (Player p : match.getWorld().getPlayers()) newmatch.joinMatch(p);
match.destroy(MatchUnloadEvent.Reason.COMMAND);
return true;
}
@AutoRefCommand(name={"autoref", "maplist"}, argmin=0, argmax=0,
description="List all maps available, both on this server and in the repository.")
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean getMapList(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
List<AutoRefMap> maps = Lists.newArrayList(AutoRefMap.getAvailableMaps());
Collections.sort(maps);
sender.sendMessage(ChatColor.GOLD + String.format("Available Maps (%d):", maps.size()));
for (AutoRefMap mapInfo : maps)
{
ChatColor color = mapInfo.isInstalled() ? ChatColor.WHITE : ChatColor.DARK_GRAY;
sender.sendMessage("* " + color + mapInfo.getVersionString());
}
return true;
}
@AutoRefCommand(name={"autoref", "update"}, argmin=0, argmax=0, options="f",
description="Updates maps installed on server. Use -f to force an update.",
opthelp=
{
"f", "force an update of all maps",
})
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean updateMaps(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
AutoRefMap.getUpdates(sender, options.hasOption('f'));
return true;
}
@AutoRefCommand(name={"autoref", "autoinvite"}, argmin=1,
description="Invite player(s) to participate in match. Works for offline players as well.",
usage="<command> <player names...>")
@AutoRefPermission(console=true, nodes={"autoreferee.admin"})
public boolean autoInvite(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (match != null) for (int i = 0; i < args.length; ++i)
{
// first, remove this player from all expected player lists
OfflinePlayer opl = plugin.getServer().getOfflinePlayer(args[i]);
for (AutoRefMatch m : plugin.getMatches()) m.removeExpectedPlayer(opl);
// add them to the expected players list
match.addExpectedPlayer(opl);
// if this player cannot be found, skip
Player invited = plugin.getServer().getPlayer(args[i]);
if (invited == null) continue;
// if this player is already busy competing in a match, skip
AutoRefMatch m = plugin.getMatch(invited.getWorld());
if (m != null && m.isPlayer(invited) && m.getCurrentState().inProgress()) continue;
// otherwise, let's drag them in (no asking)
match.joinMatch(invited);
}
return true;
}
@AutoRefCommand(name={"autoref", "pmsend"}, argmin=1,
description="Send plugin message (for debugging client mods).",
usage="<command> <plugin message>")
@AutoRefPermission(console=false, nodes={"autoreferee.admin"})
public boolean sendPluginMessage(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (!(sender instanceof Player) || !sender.isOp()) return false;
AutoRefMatch.messageReferee((Player) sender, StringUtils.join(args, " "));
return true;
}
/*
* Replaces the vanilla /time command so time is set only in the sender's world.
*/
@AutoRefCommand(name={"time"}, argmin=2,
description="Set the world time.",
usage="§cUsage: /time <set|add> <value>")
@AutoRefPermission(console=false, nodes={"bukkit.command.time"})
public boolean setTime(CommandSender sender, AutoRefMatch match, String[] args, CommandLine options)
{
if (args[0].equalsIgnoreCase("set")) {
int time = 0;
if (args[1].equalsIgnoreCase("day")) {
time = 1000;
} else if (args[1].equalsIgnoreCase("night")) {
time = 13000;
} else if (args[1].matches("\\d+")) {
time = Integer.parseInt(args[1]);
} else {
sender.sendMessage(ChatColor.RED + "'" + args[1] +"' is not a valid number");
return true;
}
if (sender instanceof Player) {
((Player) sender).getWorld().setTime(time);
sender.sendMessage("Set the time to " + time);
} else if (sender instanceof BlockCommandSender) {
((BlockCommandSender) sender).getBlock().getWorld().setTime(time);
} else if (sender instanceof CommandMinecart) {
((CommandMinecart) sender).getWorld().setTime(time);
}
}
if (args[0].equalsIgnoreCase("add")) {
if (args[1].matches("\\d+")) {
int time = Integer.parseInt(args[1]);
if (sender instanceof Player) {
World world = ((Player) sender).getWorld();
world.setTime(time + world.getTime());
sender.sendMessage("Added " + time + " to the time");
} else if (sender instanceof BlockCommandSender) {
World world = ((BlockCommandSender) sender).getBlock().getWorld();
world.setTime(time + world.getTime());
} else if (sender instanceof CommandMinecart) {
World world = ((CommandMinecart) sender).getWorld();
world.setTime(time + world.getTime());
}
} else {
sender.sendMessage(ChatColor.RED + "'" + args[1] +"' is not a valid number");
return true;
}
}
return true;
}
}