Class PluginCommand<T extends org.cloudburstmc.api.plugin.PluginContainer>
- All Implemented Interfaces:
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);
-
Field Summary
Fields inherited from class org.cloudburstmc.server.command.Command
commandData, timing -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanIt is not recommended to override this method, but instead to implement theCommandExecutor(by default the owning Plugin of this command).Returns the owner of the PluginIdentifiableCommand.voidsetExecutor(CommandExecutor executor)Methods inherited from class org.cloudburstmc.server.command.Command
getAliases, getCommandParameters, getDescription, getLabel, getName, getPermissionMessage, getPermissions, getRegisteredName, getUsage, removeAlias, setRegisteredName, testPermission, testPermissionSilent, toNetwork, toString
-
Constructor Details
-
PluginCommand
-
-
Method Details
-
execute
public boolean execute(org.cloudburstmc.api.command.CommandSender sender, String commandLabel, String[] args)It is not recommended to override this method, but instead to implement theCommandExecutor(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 youronCommand()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
falsewill still show the Usage (if not empty) to the sender. If you already sent feedback to the sender, you may returntrue. -
getExecutor
-
setExecutor
-
getPlugin
Description copied from interface:PluginIdentifiableCommandReturns the owner of the PluginIdentifiableCommand.- Specified by:
getPluginin interfacePluginIdentifiableCommand- Returns:
- The plugin that owns this PluginIdentifiableCommand
-