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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
package org.bukkit.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.StringUtil;
import com.google.common.collect.ImmutableList;
/**
* Represents a Command, which executes various tasks upon user input
*/
public abstract class Command {
private final String name;
private String nextLabel;
private String label;
private List<String> aliases;
private List<String> activeAliases;
private CommandMap commandMap = null;
protected String description = "";
protected String usageMessage;
private String permission;
private String permissionMessage;
protected Command(String name) {
this(name, "", "/" + name, new ArrayList<String>());
}
protected Command(String name, String description, String usageMessage, List<String> aliases) {
this.name = name;
this.nextLabel = name;
this.label = name;
this.description = description;
this.usageMessage = usageMessage;
this.aliases = aliases;
this.activeAliases = new ArrayList<String>(aliases);
}
/**
* Executes the command, returning its success
*
* @param sender Source object which is executing this command
* @param commandLabel The alias of the command used
* @param args All arguments passed to the command, split via ' '
* @return true if the command was successful, otherwise false
*/
public abstract boolean execute(CommandSender sender, String commandLabel, String[] args);
/**
* @deprecated This method is not supported and returns null
*/
@Deprecated
public List<String> tabComplete(CommandSender sender, String[] args) {
return null;
}
/**
* Executed on tab completion for this command, returning a list of
* options the player can tab through.
*
* @param sender Source object which is executing this command
* @param alias the alias being used
* @param args All arguments passed to the command, split via ' '
* @return a list of tab-completions for the specified arguments. This
* will never be null. List may be immutable.
* @throws IllegalArgumentException if sender, alias, or args is null
*/
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(args, "Arguments cannot be null");
Validate.notNull(alias, "Alias cannot be null");
if (args.length == 0) {
return ImmutableList.of();
}
String lastWord = args[args.length - 1];
Player senderPlayer = sender instanceof Player ? (Player) sender : null;
ArrayList<String> matchedPlayers = new ArrayList<String>();
for (Player player : sender.getServer().getOnlinePlayers()) {
String name = player.getName();
if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
matchedPlayers.add(name);
}
}
Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER);
return matchedPlayers;
}
/**
* Returns the name of this command
*
* @return Name of this command
*/
public String getName() {
return name;
}
/**
* Gets the permission required by users to be able to perform this
* command
*
* @return Permission name, or null if none
*/
public String getPermission() {
return permission;
}
/**
* Sets the permission required by users to be able to perform this
* command
*
* @param permission Permission name or null
*/
public void setPermission(String permission) {
this.permission = permission;
}
/**
* Tests the given {@link CommandSender} to see if they can perform this
* command.
* <p>
* If they do not have permission, they will be informed that they cannot
* do this.
*
* @param target User to test
* @return true if they can use it, otherwise false
*/
public boolean testPermission(CommandSender target) {
if (testPermissionSilent(target)) {
return true;
}
if (permissionMessage == null) {
target.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is in error.");
} else if (permissionMessage.length() != 0) {
for (String line : permissionMessage.replace("<permission>", permission).split("\n")) {
target.sendMessage(line);
}
}
return false;
}
/**
* Tests the given {@link CommandSender} to see if they can perform this
* command.
* <p>
* No error is sent to the sender.
*
* @param target User to test
* @return true if they can use it, otherwise false
*/
public boolean testPermissionSilent(CommandSender target) {
if ((permission == null) || (permission.length() == 0)) {
return true;
}
for (String p : permission.split(";")) {
if (target.hasPermission(p)) {
return true;
}
}
return false;
}
/**
* Returns the current label for this command
*
* @return Label of this command or null if not registered
*/
public String getLabel() {
return label;
}
/**
* Sets the label of this command.
* <p>
* If the command is currently registered the label change will only take
* effect after its been re-registered e.g. after a /reload
*
* @param name The command's name
* @return returns true if the name change happened instantly or false if
* it was scheduled for re-registration
*/
public boolean setLabel(String name) {
this.nextLabel = name;
if (!isRegistered()) {
this.label = name;
return true;
}
return false;
}
/**
* Registers this command to a CommandMap.
* Once called it only allows changes the registered CommandMap
*
* @param commandMap the CommandMap to register this command to
* @return true if the registration was successful (the current registered
* CommandMap was the passed CommandMap or null) false otherwise
*/
public boolean register(CommandMap commandMap) {
if (allowChangesFrom(commandMap)) {
this.commandMap = commandMap;
return true;
}
return false;
}
/**
* Unregisters this command from the passed CommandMap applying any
* outstanding changes
*
* @param commandMap the CommandMap to unregister
* @return true if the unregistration was successfull (the current
* registered CommandMap was the passed CommandMap or null) false
* otherwise
*/
public boolean unregister(CommandMap commandMap) {
if (allowChangesFrom(commandMap)) {
this.commandMap = null;
this.activeAliases = new ArrayList<String>(this.aliases);
this.label = this.nextLabel;
return true;
}
return false;
}
private boolean allowChangesFrom(CommandMap commandMap) {
return (null == this.commandMap || this.commandMap == commandMap);
}
/**
* Returns the current registered state of this command
*
* @return true if this command is currently registered false otherwise
*/
public boolean isRegistered() {
return (null != this.commandMap);
}
/**
* Returns a list of active aliases of this command
*
* @return List of aliases
*/
public List<String> getAliases() {
return activeAliases;
}
/**
* Returns a message to be displayed on a failed permission check for this
* command
*
* @return Permission check failed message
*/
public String getPermissionMessage() {
return permissionMessage;
}
/**
* Gets a brief description of this command
*
* @return Description of this command
*/
public String getDescription() {
return description;
}
/**
* Gets an example usage of this command
*
* @return One or more example usages
*/
public String getUsage() {
return usageMessage;
}
/**
* Sets the list of aliases to request on registration for this command.
* This is not effective outside of defining aliases in the {@link
* PluginDescriptionFile#getCommands()} (under the
* `<code>aliases</code>' node) is equivalent to this method.
*
* @param aliases aliases to register to this command
* @return this command object, for chaining
*/
public Command setAliases(List<String> aliases) {
this.aliases = aliases;
if (!isRegistered()) {
this.activeAliases = new ArrayList<String>(aliases);
}
return this;
}
/**
* Sets a brief description of this command. Defining a description in the
* {@link PluginDescriptionFile#getCommands()} (under the
* `<code>description</code>' node) is equivalent to this method.
*
* @param description new command description
* @return this command object, for chaining
*/
public Command setDescription(String description) {
this.description = description;
return this;
}
/**
* Sets the message sent when a permission check fails
*
* @param permissionMessage new permission message, null to indicate
* default message, or an empty string to indicate no message
* @return this command object, for chaining
*/
public Command setPermissionMessage(String permissionMessage) {
this.permissionMessage = permissionMessage;
return this;
}
/**
* Sets the example usage of this command
*
* @param usage new example usage
* @return this command object, for chaining
*/
public Command setUsage(String usage) {
this.usageMessage = usage;
return this;
}
public static void broadcastCommandMessage(CommandSender source, String message) {
broadcastCommandMessage(source, message, true);
}
public static void broadcastCommandMessage(CommandSender source, String message, boolean sendToSource) {
String result = source.getName() + ": " + message;
if (source instanceof BlockCommandSender && ((BlockCommandSender) source).getBlock().getWorld().getGameRuleValue("commandBlockOutput").equalsIgnoreCase("false")) {
Bukkit.getConsoleSender().sendMessage(result);
return;
}
Set<Permissible> users = Bukkit.getPluginManager().getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
String colored = ChatColor.GRAY + "" + ChatColor.ITALIC + "[" + result + ChatColor.GRAY + ChatColor.ITALIC + "]";
if (sendToSource && !(source instanceof ConsoleCommandSender)) {
source.sendMessage(message);
}
for (Permissible user : users) {
if (user instanceof CommandSender) {
CommandSender target = (CommandSender) user;
if (target instanceof ConsoleCommandSender) {
target.sendMessage(result);
} else if (target != source) {
target.sendMessage(colored);
}
}
}
}
@Override
public String toString() {
return getClass().getName() + '(' + name + ')';
}
}
|