Class PluginCommand<T extends org.cloudburstmc.api.plugin.PluginContainer>

java.lang.Object
org.cloudburstmc.server.command.Command
org.cloudburstmc.server.command.PluginCommand<T>
All Implemented Interfaces:
PluginIdentifiableCommand

public class PluginCommand<T extends org.cloudburstmc.api.plugin.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.

Wherever you have the CommandExecutor implemented, you will want to have the following:

      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);
 
Author:
MagicDroidX
See Also:
CommandExecutor.onCommand(org.cloudburstmc.api.command.CommandSender, org.cloudburstmc.server.command.Command, java.lang.String, java.lang.String[])
  • Constructor Details

  • 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 the 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.

      Specified by:
      execute in class Command
      Parameters:
      sender - Origin of the command
      commandLabel - Alias that was used to call the command
      args - Command line arguments
      Returns:
      true on successful processing, false otherwise (which will show Usage message to the sender)
    • getExecutor

      public CommandExecutor getExecutor()
    • setExecutor

      public void setExecutor(@Nonnull CommandExecutor executor)
    • getPlugin

      public T getPlugin()
      Description copied from interface: PluginIdentifiableCommand
      Returns the owner of the PluginIdentifiableCommand.
      Specified by:
      getPlugin in interface PluginIdentifiableCommand
      Returns:
      The plugin that owns this PluginIdentifiableCommand