public class PluginCommand<T extends PluginContainer> extends Command implements PluginIdentifiableCommand
This class is used as a base for all Commands for Plugins. You will want to extend this class to implement
your custom commands, and then register them with the CommandRegistry
in your Plugin's onLoad() method to ensure you are registering
them before the registration period closes.
The CommandData created during the constructor cannot be modified at runtime. For simplicity, a
Builder class has been provided to make construction of custom Commands easier.
Here is an example of implementation of a PluginCommand for a Plugin with base class MyPlugin:
public class MyPluginCommand extends PluginCommand<MyPlugin> {
public MyPluginCommand(MyPlugin plugin) {
super(plugin, CommandData.builder("mycommand")
.setDescription("This is my awesome Command!")
.setUsage("/mycommand <arg> [optionalArg]")
.setPermission("com.example.mycommand")
.build());
// Here you can optionally set a CommandExecutor that will be called on the command run
// by default, the command Executor will be the owning Plugin (MyPlugin)
}
}
By default, the PluginBase (MyPlugin in example above) is the CommandExecutor.
You may implement the CommandExecutor interface on the PluginCommand itself if you wish, and then add
this.setExecutor(this) after the super() call in the Constructor.
public boolean onCommand(CommandSender sender, Command command, commandLabel, args) {
// If you do not define a CommandExecutor for the Command either in the
// constructor or at time of registration, the owning Plugin will be used as the executor
// Do your command stuff here
// Note that a base permissions check will already have been done at this point,
// and if you return false, the server will send the Usage message to the sender
}
To register the command (recommended to be done from your PluginBase class onLoad()), you can do the following:
getServer().getCommandRegistry().register(this, new MyCommand(this);
commandData, timing| Constructor and Description |
|---|
PluginCommand(T owner,
CommandExecutor executor,
CommandData data) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
execute(CommandSender sender,
String commandLabel,
String[] args)
It is not recommended to override this method, but instead to implement the
CommandExecutor
(by default the owning Plugin of this command). |
CommandExecutor |
getExecutor() |
T |
getPlugin()
Returns the owner of the PluginIdentifiableCommand.
|
void |
setExecutor(CommandExecutor executor) |
getAliases, getCommandParameters, getDescription, getLabel, getName, getPermissionMessage, getPermissions, getRegisteredName, getUsage, removeAlias, setRegisteredName, testPermission, testPermissionSilent, toNetwork, toStringpublic PluginCommand(T owner, CommandExecutor executor, CommandData data)
public boolean execute(CommandSender sender, String commandLabel, String[] args)
CommandExecutor
(by default the owning Plugin of this command). This way you will have a built-in pre-check of if
your plugin is enabled, the sender has permission, and will show the Usage message to the sender
if your onCommand() returns false;
Alternatively, you may override this method in your Command class, but will need to check the sender's
permissions and ensure your plugin is enabled prior to running your command code. Returning false
will still show the Usage (if not empty) to the sender. If you already sent feedback to the sender,
you may return true.
public CommandExecutor getExecutor()
public void setExecutor(@Nonnull CommandExecutor executor)
public T getPlugin()
PluginIdentifiableCommandgetPlugin in interface PluginIdentifiableCommandCopyright © 2020. All rights reserved.