Class CommandBuilder

java.lang.Object
me.tayebyassine.homestead.commands.CommandBuilder
All Implemented Interfaces:
org.bukkit.command.CommandExecutor, org.bukkit.command.TabCompleter
Direct Known Subclasses:
ClaimCommand, ForceUnclaimCommand, HomesteadAdminCommand, RegionCommand, UnclaimCommand

public abstract class CommandBuilder extends Object implements org.bukkit.command.CommandExecutor, org.bukkit.command.TabCompleter
Base class for every top-level Bukkit command.

A command may hold any number of sub-commands. When no sub-command is matched, execution falls back to onDefaultExecution(CommandSender, String[]).

  • Field Details

  • Constructor Details

    • CommandBuilder

      public CommandBuilder(String name)
      Creates a command without aliases.
      Parameters:
      name - the command name, as declared in plugin.yml
    • CommandBuilder

      public CommandBuilder(String name, String... aliases)
      Creates a command with the given aliases.
      Parameters:
      name - the command name, as declared in plugin.yml
      aliases - the command aliases, each also declared in plugin.yml
  • Method Details

    • registerSubCommand

      protected void registerSubCommand(SubCommandBuilder subCommand)
      Registers a sub-command, together with all of its aliases, on this command.
      Parameters:
      subCommand - the sub-command to register
    • getSubCommand

      protected SubCommandBuilder getSubCommand(String name)
      Resolves a sub-command by name or alias.
      Parameters:
      name - the sub-command name or alias
      Returns:
      the matching sub-command, or null if none match
    • getAllSubCommands

      protected Collection<SubCommandBuilder> getAllSubCommands()
      Returns:
      a de-duplicated view of all registered sub-commands
    • getSubCommandNames

      protected List<String> getSubCommandNames()
      Returns:
      a sorted list of the names of all registered sub-commands
    • setPermission

      public CommandBuilder setPermission(String permission)
      Replaces the permission list with a single permission.
      Parameters:
      permission - the required permission
      Returns:
      this command, for chaining
    • setPermission

      public CommandBuilder setPermission(List<String> permissions)
      Replaces the permission list. All permissions must be held by the sender.
      Parameters:
      permissions - the required permissions
      Returns:
      this command, for chaining
    • setAllowedCommandSenders

      public CommandBuilder setAllowedCommandSenders(CommandSenderType... types)
      Sets the allowed executor types for this command.
      Parameters:
      types - the allowed executor types
      Returns:
      this command, for chaining
    • setPlayerOnly

      public CommandBuilder setPlayerOnly()
      Restricts this command to players only.
      Returns:
      this command, for chaining
    • setConsoleOnly

      public CommandBuilder setConsoleOnly()
      Restricts this command to the console only.
      Returns:
      this command, for chaining
    • hasPermission

      public boolean hasPermission(org.bukkit.command.CommandSender sender)
      Checks whether the sender holds every required permission.
      Parameters:
      sender - the command sender
      Returns:
      true if no permissions are required or the sender holds all of them
    • onDefaultExecution

      public abstract boolean onDefaultExecution(org.bukkit.command.CommandSender sender, String[] args)
      Executes the command when no (matching) sub-command is provided.
      Parameters:
      sender - the command sender
      args - the full command arguments
      Returns:
      true to signal that the command was handled
    • onDefaultTabComplete

      public List<String> onDefaultTabComplete(org.bukkit.command.CommandSender sender, String[] args)
      Provides tab-completion suggestions for the default execution of this command.
      Parameters:
      sender - the command sender
      args - the full command arguments
      Returns:
      the list of suggestions
    • isAllowedCommandSender

      public boolean isAllowedCommandSender(org.bukkit.command.CommandSender sender)
      Checks if the given sender is allowed to execute this command.
      Parameters:
      sender - the command sender
      Returns:
      true if the sender type is allowed
    • getAllowedCommandSenderDescription

      public String getAllowedCommandSenderDescription()
      Gets a human-readable description of allowed executors.
      Returns:
      description string
    • onCommand

      public final boolean onCommand(org.bukkit.command.CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args)
      Specified by:
      onCommand in interface org.bukkit.command.CommandExecutor
    • onTabComplete

      public final List<String> onTabComplete(org.bukkit.command.CommandSender sender, org.bukkit.command.Command cmd, String alias, String[] args)
      Specified by:
      onTabComplete in interface org.bukkit.command.TabCompleter
    • getName

      public String getName()
      Returns:
      the command name
    • getAliases

      public String[] getAliases()
      Returns:
      the command aliases
    • getUsage

      public String getUsage()
      Returns:
      the command usage string
    • setUsage

      public CommandBuilder setUsage(String usage)
      Sets the usage string shown to the player.
      Parameters:
      usage - the usage string
      Returns:
      this command, for chaining
    • getPermissions

      public List<String> getPermissions()
      Returns:
      an unmodifiable view of the required permissions
    • isPlayerOnly

      public boolean isPlayerOnly()
      Returns:
      true if this command is restricted to players only
    • isConsoleOnly

      public boolean isConsoleOnly()
      Returns:
      true if this command is restricted to the console only
    • getallowedCommandSenders

      public Set<CommandSenderType> getallowedCommandSenders()
      Returns:
      an unmodifiable view of the allowed executor types
    • asPlayer

      protected org.bukkit.entity.Player asPlayer(org.bukkit.command.CommandSender sender)
      Casts the sender to a Player when possible.
      Parameters:
      sender - the command sender
      Returns:
      the player, or null if the sender is not a player