/
KelpCommand.java
501 lines (454 loc) · 17.6 KB
/
KelpCommand.java
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package de.pxav.kelp.core.command;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import de.pxav.kelp.core.player.KelpPlayer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/**
* This is the base class of any command - no matter if sub- or
* main command. Every command class has to inherit from this class
* to be able to use essential methods like {@code #onCommand}.
*
* Note that your command class will also have to be annotated with
* one of the command annotations.
*
* @see CreateCommand
* @see CreateSubCommand
* @author pxav
*/
public class KelpCommand {
// saves the sub commands of this command, where the key is the command class
// of the sub command and the value is its annotation.
private Map<KelpCommand, CreateSubCommand> subCommands = Maps.newHashMap();
// saves the aliases for the command.
private Collection<String> aliases = Lists.newArrayList();
// description for the command (can be displayed later in help commands for example)
private String description;
// the message which is sent, when a player has no permission.
private String noPermissionMessage;
// the message which is sent when a a console executes a command
// which is only meant for players.
private String noPlayerMessage;
// the message which is sent when a player executes a command
// which is only meant for the console.
private String noConsoleMessage;
// the permission needed by players to execute this command.
private String permission;
// if 'true', this will allow to enter custom parameters after a command
// which are not defined as a sub command.
// Example: /xp 10 where 10 would be a custom parameter.
private boolean allowCustomParameters;
// resets the argument count for sub commands to 0 again.
private boolean argumentsStartFromZero;
// if 'true' the onCommand() method for console will be
// used although a player executes the command (if executor
// type is set to PLAYER_AND_CONSOLE)
private boolean delegatePlayerToConsole;
// whether basic properties such as error messages should be taken
// from the parent command, so that they don't have to be defined
// manually each time.
private boolean inheritFromMainCommand;
/**
* This method is executed by the command registry, when the command
* is executed by a player and {@code PLAYER_ONLY} is set as executor type.
*
* @param player The player who has executed the command.
* @param args The arguments of the command. They act the same as the
* normal bukkit arguments if you have not reset them manually.
* @see ExecutorType
* @see KelpPlayer
*/
public void onCommand(KelpPlayer player, String[] args) {}
/**
* This method is executed by the command registry, when the command
* is executed by the console and {@code CONSOLE_ONLY} is set as executor type.
*
* It is also called when {@code PLAYER_AND_CONSOLE} is enabled and either a player
* or a console executes the command.
*
* @param consoleSender The {@code KelpConsoleSender} object of the command executor.
* @param args The arguments of the command. They act the same as the
* normal bukkit arguments if you have not reset them manually.
* @see ExecutorType
* @see KelpConsoleSender
*/
public void onCommand(KelpConsoleSender consoleSender, String[] args) {}
/**
* This method is executed when the command is registered.
* Mostly, this happens once on every server startup and only once
* during the application runtime.
*
* So it is recommended to put the command properties like permission,
* alias, etc. in here
*/
public void onCommandRegister() {}
/**
* Gets a collection of all sub commands associated with this command.
*
* @return A map of all sub commands, where the key is the command class
* of the command and the value is the sub command annotation.
*/
public Map<KelpCommand, CreateSubCommand> getSubCommands() {
return subCommands;
}
/**
* Delegates the command executor from the player to a console. Normally, if executor type
* is set to {@link ExecutorType#PLAYER_AND_CONSOLE} and a player executes the command,
* the {@link #onCommand(KelpPlayer, String[])} method is called. But if you want the same
* command structure to be executed for both types of users, you might not want to copy the
* methods for player and console. That's why you can use this method to create a kind of
* redirection. If this is enabled, the player will be converted to a console sender and
* the {@link #onCommand(KelpConsoleSender, String[])} method will be executed so that
* you only have to maintain one command method.
*
* @param delegate {@code true} if the described redirection/delegation
* of commands should be done.
*/
public void delegatePlayerToConsole(boolean delegate) {
this.delegatePlayerToConsole = delegate;
}
/**
* Makes the current command inherit certain properties of the parent command.
* This includes:
* <ul>
* <li>Command description</li>
* <li>Command permission</li>
* <li>No permission message</li>
* <li>No player message</li>
* <li>No console message</li>
* </ul>
*
* You can of course inherit properties from the parent command and overwrite
* them manually later, while keeping the values for the other strings. So if
* you only want to change the description you can inherit all properties and overwrite
* the description yourself.
*
* @param inherit {@code true} if you want the listed properties to be inherited from the main command.
*/
public void inheritFromMainCommand(boolean inherit) {
this.inheritFromMainCommand = inherit;
}
/**
* Sets the description of the command, which can later be used
* to display in help commands, etc.
*
* @param description The description of your command. It should be brief
* but accurate.
*/
public void description(String description) {
this.description = description;
}
/**
* Sets the message, which is sent to a player when they do not
* have enough permissions to execute the command. If this message
* is not set, kelp will use the default one.
*
* @param noPermissionMessage The message which should be sent (may
* include color codes with '§')
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand noPermissionMessage(String noPermissionMessage) {
this.noPermissionMessage = noPermissionMessage;
return this;
}
/**
* Sets the message, which is sent to the console when it
* executes a command, which is only meant for players.
*
* @param noPlayerMessage The message which should be sent (may
* include color codes with '§')
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand noPlayerMessage(String noPlayerMessage) {
this.noPlayerMessage = noPlayerMessage;
return this;
}
/**
* Sets the message, which is sent to a player when they try
* to execute a command, which is meant for the console only.
* If this message is not set, kelp will use a default one.
*
* @param noConsoleMessage The message which should be sent (may
* include color codes with '§')
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand noConsoleMessage(String noConsoleMessage) {
this.noConsoleMessage = noConsoleMessage;
return this;
}
/**
* Sets the permission of the command. This permission has to be assigned
* to a player by a separate permission system or similar. Consoles won't
* be checked for permissions as they already have all permissions by default.
*
* @param permission The permission, which is needed by the player.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand permission(String permission) {
this.permission = permission;
return this;
}
/**
* Sets a collection of aliases for the command. All
* other aliases, which have existed before are overwritten with
* this method.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The collection of aliases you want to set.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand setAliases(Collection<String> aliases) {
this.aliases = aliases;
return this;
}
/**
* Sets an array of aliases for the command. All
* other aliases, which have existed before are overwritten with
* this method.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The array of aliases you want to set.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand setAliases(String... aliases) {
this.aliases = Arrays.asList(aliases);
return this;
}
/**
* Adds an alias for a command. This method simply adds a single
* alias, other aliases are not overwritten by that.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param alias The array of aliases you want to set.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand addAlias(String alias) {
this.aliases.add(alias);
return this;
}
/**
* Adds an array of aliases for the command. Other aliases are
* not overwritten by this method.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The array of aliases you want to set.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand addAliases(String... aliases) {
this.aliases.addAll(Arrays.asList(aliases));
return this;
}
/**
* Adds a collection of aliases for the command. Other aliases are
* not overwritten by this method.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The collection of aliases you want to set.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand addAliases(Collection<String> aliases) {
this.aliases.addAll(aliases);
return this;
}
/**
* Removes a single alias from your command.
*
* Note that removing aliases is not possible during the application
* runtime. The remove methods only exist for processing reasons.
* If you are reading aliases from a config file for example, you
* can remove certain aliases again if needed.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param alias The alias you want to remove.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand removeAlias(String alias) {
this.aliases.remove(alias);
return this;
}
/**
* Removes an array of aliases from your command.
*
* Note that removing aliases is not possible during the application
* runtime. The remove methods only exist for processing reasons.
* If you are reading aliases from a config file for example, you
* can remove certain aliases again if needed.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The array of aliases you want to remove.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand removeAliases(String... aliases) {
this.aliases.removeAll(Arrays.asList(aliases));
return this;
}
/**
* Removes a collection of aliases from your command.
*
* Note that removing aliases is not possible during the application
* runtime. The remove methods only exist for processing reasons.
* If you are reading aliases from a config file for example, you
* can remove certain aliases again if needed.
*
* Aliases are alternative names for a command, which can
* be typed by the executor to execute the same command.
*
* @param aliases The collection of aliases you want to remove.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand removeAliases(Collection<String> aliases) {
this.aliases.removeAll(aliases);
return this;
}
/**
* Sets the sub command map of this command. It is recommended not to
* touch this method, as sub commands are administrated by the kelp-internal
* command registry, but you can manipulate it if you want of course.
*
* @param subCommands The map of sub commands.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand subCommands(Map<KelpCommand, CreateSubCommand> subCommands) {
this.subCommands = subCommands;
return this;
}
/**
* Allow/Disallow custom parameters in your command. Custom parameters
* are arguments, which are no sub commands defined by any plugin.
*
* Examples of custom parameters would be:
* /giveXP 20 <- the amount of XP points.
* /setSpawn 0 120 0 <- the axes of the location
*
* @param allow {@code true} if you want to accept custom parameters,
* {@code false} if not.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand allowCustomParameters(boolean allow) {
this.allowCustomParameters = allow;
return this;
}
/**
* You can enable this property, if you want to reset the argument
* count within a sub command.
*
* If you have nested commands with a complicated structure, you probably
* lose the overview of your commands and do not know at which argument
* you have to start counting in your sub command. Or if you want to add
* a new sub command in between two commands, you would have to change
* all argument ids in the existing commands. To avoid that, you can
* reset the argument count to 0 every time. Then you have constant
* argument ids and it becomes more readable for other developers.
*
* @param argumentsStartFromZero {@code true} if you want to reset the argument count,
* if not, choose {@code false}.
* @return The current command object, which can be used to append further properties.
*/
public KelpCommand argumentsStartFromZero(boolean argumentsStartFromZero) {
this.argumentsStartFromZero = argumentsStartFromZero;
return this;
}
/**
* Gets the command's description used in the Kelp command
* overview for example.
*
* @return The description of the command.
*/
public String getDescription() {
return description;
}
/**
* Gets the custom message that is sent to a player when they do not
* have sufficient permissions to execute the command.
*
* @return The message which is sent to players, if they do not have enough
* permissions.
*/
public String getNoPermissionMessage() {
return noPermissionMessage;
}
/**
* Gets the custom message set by this command which is sent when
* a console tries to execute the command, although it is made
* for players only. If this is null, the default message of
* Kelp will be used.
*
* @return The message which is sent to the console, when it
* executes a command, which is meant for players only.
*/
public String getNoPlayerMessage() {
return noPlayerMessage;
}
/**
* Gets the custom message set by this command which is sent when
* a player executes the command although it is meant for consoles
* only. If this is null, the default message will be used.
*
* @return The message which is sent to players when they execute a console command.
*/
public String getNoConsoleMessage() {
return noConsoleMessage;
}
/**
* Gets the permission that is needed by a player to execute the command.
* A console does not need this permission.
*
* @return Returns the permission string, which is needed for this command.
*/
public String getPermission() {
return permission;
}
/**
* Checks whether custom parameters other than sub commands are
* accepted by the command. If this is false, you won't be able to
* handle such custom user inputs.
*
* @return {@code true} if custom parameters are allowed in this command.
*/
public boolean customParametersAllowed() {
return this.allowCustomParameters;
}
/**
* Checks whether the arguments should be reset when queried in a sub command.
* For more information see {@link #argumentsStartFromZero(boolean)}
*
* @return {@code true} if the argument count should be reset to 0.
*/
public boolean shouldArgumentsStartFromZero() {
return this.argumentsStartFromZero;
}
/**
* Gets a collection of all aliases defined for this command.
*
* @return A collection of all aliases the command has.
*/
public Collection<String> getAliases() {
return aliases;
}
/**
* Checks if the command
*
* @return
*/
public boolean shouldDelegateToConsole() {
return this.delegatePlayerToConsole;
}
public boolean shouldInheritFromMainCommand() {
return this.inheritFromMainCommand;
}
}