Class ConfigurableCommand

java.lang.Object
org.bukkit.command.Command
studio.magemonkey.codex.mccore.commands.ConfigurableCommand

public class ConfigurableCommand extends org.bukkit.command.Command

A command that is able to be modified via configuration

To set up hierarchies of commands, simply start with your root command, then create your sub commands, adding each to the root.

Example:

// Root commands
ConfigurableCommand root = new ConfigurableCommand(this, "root", SenderType.ANYONE);
ConfigurableCommand group;

// Add sub commands
root.addSubCommands(
    new ConfigurableCommand(this, "list", SenderType.ANYONE, new ListFunction(), "Lists available things", "", "perm.list"),
    group = new ConfigurableCommand(this, "group", SenderType.ANYONE, "Handles group functions")
);
group.addSubCommands(
    new ConfigurableCommand(this, "add", SenderType.ANYONE, new AddFunction(), "Adds a member to a group", "<player>", "perm.add"),
    new ConfigurableCommand(this, "remove", SenderType.ANYONE, new RemoveFunction(), "Removes a member from a group", "<player>", "perm.remove")
);

// Register everything
CommandManager.registerCommand(root);
  • Field Summary

    Fields inherited from class org.bukkit.command.Command

    timings, usageMessage
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType)
    Creates a new command that can only hold other commands and displays a command usage for sub commands when executed.
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, String description)
    Creates a new command that can only hold other commands and displays a command usage for sub commands when executed.
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function)
    Creates a new command that performs its own action when run but cannot have sub commands.
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description)
    Creates a new command that performs its own action when run but cannot have sub commands.
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description, String args)
    Creates a new command that performs its own action when run but cannot have sub commands.
    ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description, String args, String permission)
    Creates a new command that performs its own action when run but cannot have sub commands.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a sub command to this command
    void
    Adds multiple sub commands to this command
    boolean
    canUseCommand(org.bukkit.command.CommandSender sender)
    Checks whether or not the sender can use this command
    void
    displayHelp(org.bukkit.command.CommandSender sender)
    Displays the help for this command
    void
    displayHelp(org.bukkit.command.CommandSender sender, int page)
    Displays the help for this command using the given page
    void
    displayHelp(org.bukkit.command.CommandSender sender, String[] args)
    Displays the help for this command according to the arguments
    boolean
    execute(org.bukkit.command.CommandSender sender, String[] args)
    Executes the command using the provided arguments
    boolean
    execute(org.bukkit.command.CommandSender sender, String label, String[] args)
    Bukkit executiton of the command.
    Retrieves the described arguments for this command
    Retrieves the description for this command
    getMessage(String key, String defaultMessage, CustomFilter... filters)
    Retrieves a message for the command, using the default and adding it to the configuration if not already set.
    Retrieves the name of the command
    Retrieves the parent of the command.
    Retrieves the permission required to use this command
    static List<String>
    getPlayerTabCompletions(org.bukkit.command.CommandSender sender, String arg)
     
    org.bukkit.plugin.java.JavaPlugin
    Retrieves the plugin that owns this command
    Retrieves the type of sender required to use this command
    Retrieves a sub command by name
    static List<String>
     
    getUsableCommands(org.bukkit.command.CommandSender sender)
    Retrieves the list of commands that are usable by the sender
    boolean
    Checks whether or not this command has described arguments
    boolean
    Checks whether or not this command has a description
    boolean
    Checks whether or not the command has a parent command.
    boolean
    Checks whether or not this command has the given sub command
    boolean
    Checks whether or not this command is a container command.
    boolean
    Checks whether or not this command is a functional command.
    boolean
    Checks whether or not this is a root command
    void
    Marks the command as registered so that it cannot be added to other commands.
    boolean
    Checks whether or not this command requires a permission
    void
    sendMessage(org.bukkit.command.CommandSender sender, String key, String defaultMessage, boolean silent, CustomFilter... filters)
    Sends a command message to the sender if the message is not an empty string.
    void
    sendMessage(org.bukkit.command.CommandSender sender, String key, String defaultMessage, CustomFilter... filters)
     
    tabComplete(org.bukkit.command.CommandSender sender, String alias, String[] args)
     
    Returns a string of the command name

    Methods inherited from class org.bukkit.command.Command

    broadcastCommandMessage, broadcastCommandMessage, getAliases, getLabel, getPermissionMessage, getUsage, isRegistered, register, setAliases, setDescription, setLabel, setName, setPermission, setPermissionMessage, setUsage, tabComplete, testPermission, testPermissionSilent, unregister

    Methods inherited from class java.lang.Object

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

    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType)

      Creates a new command that can only hold other commands and displays a command usage for sub commands when executed.

      The key is used to tell commands apart and is also the default name of the command.

      The command created by this has no default description. It must be set via the configuration.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, String description)

      Creates a new command that can only hold other commands and displays a command usage for sub commands when executed.

      The key is used to tell commands apart and is also the default name of the command.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
      description - default description
    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function)

      Creates a new command that performs its own action when run but cannot have sub commands.

      The key is used to tell commands apart and is also the default name of the command.

      The command created by this has no default description, arguments, or required permission. They must be set via the configuration.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
      function - command executor
    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description)

      Creates a new command that performs its own action when run but cannot have sub commands.

      The key is used to tell commands apart and is also the default name of the command.

      The command created by this has no default arguments or required permission. They must be set via the configuration.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
      function - command executor
      description - default description
    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description, String args)

      Creates a new command that performs its own action when run but cannot have sub commands.

      The key is used to tell commands apart and is also the default name of the command.

      The command created by this has no default required permission. It must be set via the configuration.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
      function - command executor
      description - default description
      args - default arguments
    • ConfigurableCommand

      public ConfigurableCommand(org.bukkit.plugin.java.JavaPlugin plugin, String key, SenderType senderType, IFunction function, String description, String args, String permission)

      Creates a new command that performs its own action when run but cannot have sub commands.

      The key is used to tell commands apart and is also the default name of the command.

      Parameters:
      plugin - plugin reference
      key - command key
      senderType - type of sender needed for the command
      function - command executor
      description - default description
      args - default arguments
      permission - default required permission
  • Method Details

    • getTabCompletions

      @Contract("null, _ -> null") public static List<String> getTabCompletions(@Nullable Collection<String> options, @NotNull String[] args)
    • getPlayerTabCompletions

      @NotNull public static List<String> getPlayerTabCompletions(@NotNull org.bukkit.command.CommandSender sender, @NotNull String arg)
    • hasDescription

      public boolean hasDescription()

      Checks whether or not this command has a description

      Returns:
      true if the command has a description, false otherwise
    • hasArguments

      public boolean hasArguments()

      Checks whether or not this command has described arguments

      Returns:
      true if it has described arguments, false otherwise
    • requiresPermission

      public boolean requiresPermission()

      Checks whether or not this command requires a permission

      Returns:
      true if requires a permission, false otherwise
    • hasSubCommand

      public boolean hasSubCommand(String name)

      Checks whether or not this command has the given sub command

      This is not case-sensitive

      Parameters:
      name - sub command name
      Returns:
      true if the command has the sub command, false otherwise
    • canUseCommand

      public boolean canUseCommand(org.bukkit.command.CommandSender sender)

      Checks whether or not the sender can use this command

      Parameters:
      sender - sender of the command
      Returns:
      true if can use, false otherwise
    • isRootCommand

      public boolean isRootCommand()

      Checks whether or not this is a root command

      A "root" command would be a command with no parent command

      Returns:
      true if root command, false otherwise
    • getName

      public String getName()

      Retrieves the name of the command

      Overrides:
      getName in class org.bukkit.command.Command
      Returns:
      command name
    • getPlugin

      public org.bukkit.plugin.java.JavaPlugin getPlugin()

      Retrieves the plugin that owns this command

      Returns:
      owning plugin
    • getDescription

      public String getDescription()

      Retrieves the description for this command

      If this doesn't have a description, this returns null

      Overrides:
      getDescription in class org.bukkit.command.Command
      Returns:
      required permission or null if none
    • getArgs

      public String getArgs()

      Retrieves the described arguments for this command

      If this doesn't have any arguments described, this returns null

      Returns:
      required permission or null if none
    • getPermission

      public String getPermission()

      Retrieves the permission required to use this command

      If this doesn't require a permission, this returns null

      Overrides:
      getPermission in class org.bukkit.command.Command
      Returns:
      required permission or null if none
    • getSenderType

      public SenderType getSenderType()

      Retrieves the type of sender required to use this command

      Returns:
      required sender type
    • getUsableCommands

      public List<String> getUsableCommands(org.bukkit.command.CommandSender sender)

      Retrieves the list of commands that are usable by the sender

      Parameters:
      sender - sender of the command
      Returns:
      list of usable commands
    • getSubCommand

      public ConfigurableCommand getSubCommand(String name)

      Retrieves a sub command by name

      If there are no sub commands with the given name, this will return null instead

      Parameters:
      name - sub command name
      Returns:
      sub command or null if not found
    • hasParent

      public boolean hasParent()

      Checks whether or not the command has a parent command.

      This command will have a parent if it was added as a sub command to another command

      Returns:
      true if has a parent, false otherwise
    • getParent

      public ConfigurableCommand getParent()

      Retrieves the parent of the command.

      If this does not have a parent, this will return null.

      Returns:
      parent of the command
    • isContainer

      public boolean isContainer()

      Checks whether or not this command is a container command.

      A container command is one that doesn't have a function of its own but contains other commands that have functions.

      Returns:
      true if a container command, false otherwise
    • isFunction

      public boolean isFunction()

      Checks whether or not this command is a functional command.

      A functional command performs a task when executed and cannot hold other commands in it.

      Returns:
      true if functional command, false otherwise
    • markAsRegistered

      public void markAsRegistered()

      Marks the command as registered so that it cannot be added to other commands.

      This is called on commands as they are registered through the CommandManager class. You do not need to use this method.

    • addSubCommand

      public void addSubCommand(ConfigurableCommand command)

      Adds a sub command to this command

      The sub command cannot be a registered command

      You cannot register a command if this command is attached to a function

      Parameters:
      command - sub command to add
      Throws:
      IllegalStateException - when unable to add sub commands
      IllegalArgumentException - when the command is registered
    • addSubCommands

      public void addSubCommands(ConfigurableCommand... commands)

      Adds multiple sub commands to this command

      The sub commands cannot be a registered command

      You cannot register a command if this command is attached to a function

      Parameters:
      commands - sub commands to add
      Throws:
      IllegalStateException - when unable to add sub commands
      IllegalArgumentException - when the command is registered
    • execute

      public boolean execute(@NotNull org.bukkit.command.CommandSender sender, @NotNull String label, String[] args)

      Bukkit executiton of the command.

      Use execute(CommandSender, String[]) instead.

      Specified by:
      execute in class org.bukkit.command.Command
      Parameters:
      sender - sender of the command
      label - label of the command
      args - arguments provided by the sender
      Returns:
      true
    • execute

      public boolean execute(org.bukkit.command.CommandSender sender, String[] args)

      Executes the command using the provided arguments

      Root commands will pass the arguments onto sub commands or display the command usage if the args don't match any sub commands.

      Parameters:
      sender - sender of the command
      args - arguments provided by the sender
    • tabComplete

      @NotNull public List<String> tabComplete(@NotNull org.bukkit.command.CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
      Overrides:
      tabComplete in class org.bukkit.command.Command
      Throws:
      IllegalArgumentException
    • displayHelp

      public void displayHelp(org.bukkit.command.CommandSender sender)

      Displays the help for this command

      This displays the first page of the usage

      If this is a function command, this will display the usage for this command including the arguments.

      If this is a command that contains others, this will display the list of sub commands and their descriptions.

      Parameters:
      sender - sender of the command
    • displayHelp

      public void displayHelp(org.bukkit.command.CommandSender sender, String[] args)

      Displays the help for this command according to the arguments

      The displayed page is determined by the provided arguments

      If this is a function command, this will display the usage for this command including the arguments.

      If this is a command that contains others, this will display the list of sub commands and their descriptions.

      Parameters:
      sender - sender of the command
      args - arguments provided by the sender
    • displayHelp

      public void displayHelp(org.bukkit.command.CommandSender sender, int page)

      Displays the help for this command using the given page

      If the page is less than one, the first page will be displayed

      If the page is greater than the number of pages, the last page will be displayed

      If this is a function command, this will display the usage for this command including the arguments.

      If this is a command that contains others, this will display the list of sub commands and their descriptions.

      Parameters:
      sender - sender of the command
      page - page number
    • getMessage

      public String getMessage(String key, String defaultMessage, CustomFilter... filters)
      Retrieves a message for the command, using the default and adding it to the configuration if not already set.
      Parameters:
      key - the message key
      defaultMessage - the message to use if not set
      filters - filters to use on the message
      Returns:
      the message from the config or default message if not set
    • sendMessage

      public void sendMessage(org.bukkit.command.CommandSender sender, String key, String defaultMessage, boolean silent, CustomFilter... filters)
      Sends a command message to the sender if the message is not an empty string.
      Parameters:
      sender - sender of the command
      key - the message key
      defaultMessage - the message to use if not set
      silent - indicates whether the message should be sent or not
      filters - filters to use on the message
    • sendMessage

      public void sendMessage(org.bukkit.command.CommandSender sender, String key, String defaultMessage, CustomFilter... filters)
    • toString

      public String toString()

      Returns a string of the command name

      If this is a sub command, this returns the parent's name as well as the name of this command.

      For example, if this is the command "add" and is the sub command of "group", this will return "group add"

      Overrides:
      toString in class org.bukkit.command.Command
      Returns:
      command path