Class Command

java.lang.Object
org.bukkit.command.Command
Direct Known Subclasses:
BukkitCommand, FormattedCommandAlias, MultipleCommandAlias, PluginCommand, VanillaCommand

public abstract class Command
extends java.lang.Object
Represents a Command, which executes various tasks upon user input
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.lang.String description  
    CustomTimingsHandler timings  
    protected java.lang.String usageMessage  
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected Command​(java.lang.String name)  
    protected Command​(java.lang.String name, java.lang.String description, java.lang.String usageMessage, java.util.List<java.lang.String> aliases)  
  • Method Summary

    Modifier and Type Method Description
    static void broadcastCommandMessage​(CommandSender source, java.lang.String message)  
    static void broadcastCommandMessage​(CommandSender source, java.lang.String message, boolean sendToSource)  
    abstract boolean execute​(CommandSender sender, java.lang.String commandLabel, java.lang.String[] args)
    Executes the command, returning its success
    java.util.List<java.lang.String> getAliases()
    Returns a list of active aliases of this command
    java.lang.String getDescription()
    Gets a brief description of this command
    java.lang.String getLabel()
    Returns the label for this command
    java.lang.String getName()
    Returns the name of this command
    java.lang.String getPermission()
    Gets the permission required by users to be able to perform this command
    java.lang.String getPermissionMessage()
    Returns a message to be displayed on a failed permission check for this command
    java.lang.String getUsage()
    Gets an example usage of this command
    boolean isRegistered()
    Returns the current registered state of this command
    boolean register​(CommandMap commandMap)
    Registers this command to a CommandMap.
    Command setAliases​(java.util.List<java.lang.String> aliases)
    Sets the list of aliases to request on registration for this command.
    Command setDescription​(java.lang.String description)
    Sets a brief description of this command.
    boolean setLabel​(java.lang.String name)
    Sets the label of this command.
    boolean setName​(java.lang.String name)
    Sets the name of this command.
    void setPermission​(java.lang.String permission)
    Sets the permission required by users to be able to perform this command
    Command setPermissionMessage​(java.lang.String permissionMessage)
    Sets the message sent when a permission check fails
    Command setUsage​(java.lang.String usage)
    Sets the example usage of this command
    java.util.List<java.lang.String> tabComplete​(CommandSender sender, java.lang.String[] args)
    Deprecated.
    This method is not supported and returns null
    java.util.List<java.lang.String> tabComplete​(CommandSender sender, java.lang.String alias, java.lang.String[] args)
    Executed on tab completion for this command, returning a list of options the player can tab through.
    boolean testPermission​(CommandSender target)
    Tests the given CommandSender to see if they can perform this command.
    boolean testPermissionSilent​(CommandSender target)
    Tests the given CommandSender to see if they can perform this command.
    java.lang.String toString()  
    boolean unregister​(CommandMap commandMap)
    Unregisters this command from the passed CommandMap applying any outstanding changes

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Command

      protected Command​(java.lang.String name)
    • Command

      protected Command​(java.lang.String name, java.lang.String description, java.lang.String usageMessage, java.util.List<java.lang.String> aliases)
  • Method Details

    • execute

      public abstract boolean execute​(CommandSender sender, java.lang.String commandLabel, java.lang.String[] args)
      Executes the command, returning its success
      Parameters:
      sender - Source object which is executing this command
      commandLabel - The alias of the command used
      args - All arguments passed to the command, split via ' '
      Returns:
      true if the command was successful, otherwise false
    • tabComplete

      @Deprecated public java.util.List<java.lang.String> tabComplete​(CommandSender sender, java.lang.String[] args)
      Deprecated.
      This method is not supported and returns null
      Executed on tab completion for this command, returning a list of options the player can tab through.
      Parameters:
      sender - Source object which is executing this command
      args - All arguments passed to the command, split via ' '
      Returns:
      a list of tab-completions for the specified arguments. This will never be null. List may be immutable.
    • tabComplete

      public java.util.List<java.lang.String> tabComplete​(CommandSender sender, java.lang.String alias, java.lang.String[] args) throws java.lang.IllegalArgumentException
      Executed on tab completion for this command, returning a list of options the player can tab through.
      Parameters:
      sender - Source object which is executing this command
      alias - the alias being used
      args - All arguments passed to the command, split via ' '
      Returns:
      a list of tab-completions for the specified arguments. This will never be null. List may be immutable.
      Throws:
      java.lang.IllegalArgumentException - if sender, alias, or args is null
    • getName

      public java.lang.String getName()
      Returns the name of this command
      Returns:
      Name of this command
    • setName

      public boolean setName​(java.lang.String name)
      Sets the name of this command.

      May only be used before registering the command. Will return true if the new name is set, and false if the command has already been registered.

      Parameters:
      name - New command name
      Returns:
      returns true if the name change happened instantly or false if the command was already registered
    • getPermission

      public java.lang.String getPermission()
      Gets the permission required by users to be able to perform this command
      Returns:
      Permission name, or null if none
    • setPermission

      public void setPermission​(java.lang.String permission)
      Sets the permission required by users to be able to perform this command
      Parameters:
      permission - Permission name or null
    • testPermission

      public boolean testPermission​(CommandSender target)
      Tests the given CommandSender to see if they can perform this command.

      If they do not have permission, they will be informed that they cannot do this.

      Parameters:
      target - User to test
      Returns:
      true if they can use it, otherwise false
    • testPermissionSilent

      public boolean testPermissionSilent​(CommandSender target)
      Tests the given CommandSender to see if they can perform this command.

      No error is sent to the sender.

      Parameters:
      target - User to test
      Returns:
      true if they can use it, otherwise false
    • getLabel

      public java.lang.String getLabel()
      Returns the label for this command
      Returns:
      Label of this command
    • setLabel

      public boolean setLabel​(java.lang.String name)
      Sets the label of this command.

      May only be used before registering the command. Will return true if the new name is set, and false if the command has already been registered.

      Parameters:
      name - The command's name
      Returns:
      returns true if the name change happened instantly or false if the command was already registered
    • register

      public boolean register​(CommandMap commandMap)
      Registers this command to a CommandMap. Once called it only allows changes the registered CommandMap
      Parameters:
      commandMap - the CommandMap to register this command to
      Returns:
      true if the registration was successful (the current registered CommandMap was the passed CommandMap or null) false otherwise
    • unregister

      public boolean unregister​(CommandMap commandMap)
      Unregisters this command from the passed CommandMap applying any outstanding changes
      Parameters:
      commandMap - the CommandMap to unregister
      Returns:
      true if the unregistration was successfull (the current registered CommandMap was the passed CommandMap or null) false otherwise
    • isRegistered

      public boolean isRegistered()
      Returns the current registered state of this command
      Returns:
      true if this command is currently registered false otherwise
    • getAliases

      public java.util.List<java.lang.String> getAliases()
      Returns a list of active aliases of this command
      Returns:
      List of aliases
    • getPermissionMessage

      public java.lang.String getPermissionMessage()
      Returns a message to be displayed on a failed permission check for this command
      Returns:
      Permission check failed message
    • getDescription

      public java.lang.String getDescription()
      Gets a brief description of this command
      Returns:
      Description of this command
    • getUsage

      public java.lang.String getUsage()
      Gets an example usage of this command
      Returns:
      One or more example usages
    • setAliases

      public Command setAliases​(java.util.List<java.lang.String> aliases)
      Sets the list of aliases to request on registration for this command. This is not effective outside of defining aliases in the PluginDescriptionFile.getCommands() (under the `aliases' node) is equivalent to this method.
      Parameters:
      aliases - aliases to register to this command
      Returns:
      this command object, for chaining
    • setDescription

      public Command setDescription​(java.lang.String description)
      Sets a brief description of this command. Defining a description in the PluginDescriptionFile.getCommands() (under the `description' node) is equivalent to this method.
      Parameters:
      description - new command description
      Returns:
      this command object, for chaining
    • setPermissionMessage

      public Command setPermissionMessage​(java.lang.String permissionMessage)
      Sets the message sent when a permission check fails
      Parameters:
      permissionMessage - new permission message, null to indicate default message, or an empty string to indicate no message
      Returns:
      this command object, for chaining
    • setUsage

      public Command setUsage​(java.lang.String usage)
      Sets the example usage of this command
      Parameters:
      usage - new example usage
      Returns:
      this command object, for chaining
    • broadcastCommandMessage

      public static void broadcastCommandMessage​(CommandSender source, java.lang.String message)
    • broadcastCommandMessage

      public static void broadcastCommandMessage​(CommandSender source, java.lang.String message, boolean sendToSource)
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object