Class CommandsManager<T>

java.lang.Object
com.sk89q.minecraft.util.commands.CommandsManager<T>
Type Parameters:
T - command sender class

@Deprecated public abstract class CommandsManager<T> extends Object
Deprecated.
Manager for handling commands. This allows you to easily process commands, including nested commands, by correctly annotating methods of a class.

To use this, it is merely a matter of registering classes containing the commands (as methods with the proper annotations) with the manager. When you want to process a command, use one of the execute methods. If something is wrong, such as incorrect usage, insufficient permissions, or a missing command altogether, an exception will be raised for upstream handling.

Methods of a class to be registered can be static, but if an injector is registered with the class, the instances of the command classes will be created automatically and methods will be called non-statically.

To mark a method as a command, use Command. For nested commands, see NestedCommand. To handle permissions, use CommandPermissions.

This uses Java reflection extensively, but to reduce the overhead of reflection, command lookups are completely cached on registration. This allows for fast command handling. Method invocation still has to be done with reflection, but this is quite fast in that of itself.

  • Field Details

    • commands

      protected Map<Method,Map<String,Method>> commands
      Deprecated.
      Mapping of commands (including aliases) with a description. Root commands are stored under a key of null, whereas child commands are cached under their respective Method. The child map has the key of the command name (one for each alias) with the method.
    • instances

      protected Map<Method,Object> instances
      Deprecated.
      Used to store the instances associated with a method.
    • descs

      protected Map<String,String> descs
      Deprecated.
      Mapping of commands (not including aliases) with a description. This is only for top level commands.
    • injector

      protected Injector injector
      Deprecated.
      Stores the injector used to getInstance.
    • helpMessages

      protected Map<String,String> helpMessages
      Deprecated.
      Mapping of commands (not including aliases) with a description. This is only for top level commands.
  • Constructor Details

    • CommandsManager

      public CommandsManager()
      Deprecated.
  • Method Details

    • register

      public void register(Class<?> cls)
      Deprecated.
      Register an class that contains commands (denoted by Command. If no dependency injector is specified, then the methods of the class will be registered to be called statically. Otherwise, new instances will be created of the command classes and methods will not be called statically.
      Parameters:
      cls - the class to register
    • registerAndReturn

      public List<Command> registerAndReturn(Class<?> cls)
      Deprecated.
      Register an class that contains commands (denoted by Command. If no dependency injector is specified, then the methods of the class will be registered to be called statically. Otherwise, new instances will be created of the command classes and methods will not be called statically. A List of Command annotations from registered commands is returned.
      Parameters:
      cls - the class to register
      Returns:
      A List of Command annotations from registered commands, for use in eg. a dynamic command registration system.
    • registerMethods

      public List<Command> registerMethods(Class<?> cls, Method parent)
      Deprecated.
      Register the methods of a class. This will automatically construct instances as necessary.
      Parameters:
      cls - the class to register
      parent - the parent method
      Returns:
      Commands Registered
    • hasCommand

      public boolean hasCommand(String command)
      Deprecated.
      Checks to see whether there is a command named such at the root level. This will check aliases as well.
      Parameters:
      command - the command
      Returns:
      true if the command exists
    • getCommands

      public Map<String,String> getCommands()
      Deprecated.
      Get a list of command descriptions. This is only for root commands.
      Returns:
      a map of commands
    • getMethods

      public Map<Method,Map<String,Method>> getMethods()
      Deprecated.
      Get the mapping of methods under a parent command.
      Returns:
      the mapping
    • getHelpMessages

      public Map<String,String> getHelpMessages()
      Deprecated.
      Get a map from command name to help message. This is only for root commands.
      Returns:
      a map of help messages for each command
    • getUsage

      protected String getUsage(String[] args, int level, Command cmd)
      Deprecated.
      Get the usage string for a command.
      Parameters:
      args - the arguments
      level - the depth of the command
      cmd - the command annotation
      Returns:
      the usage string
    • getArguments

      protected CharSequence getArguments(Command cmd)
      Deprecated.
    • getNestedUsage

      protected String getNestedUsage(String[] args, int level, Method method, T player) throws CommandException
      Deprecated.
      Get the usage string for a nested command.
      Parameters:
      args - the arguments
      level - the depth of the command
      method - the parent method
      player - the player
      Returns:
      the usage string
      Throws:
      CommandException - on some error
    • execute

      public void execute(String cmd, String[] args, T player, Object... methodArgs) throws CommandException
      Deprecated.
      Attempt to execute a command. This version takes a separate command name (for the root command) and then a list of following arguments.
      Parameters:
      cmd - command to run
      args - arguments
      player - command source
      methodArgs - method arguments
      Throws:
      CommandException - thrown when the command throws an error
    • execute

      public void execute(String[] args, T player, Object... methodArgs) throws CommandException
      Deprecated.
      Attempt to execute a command.
      Parameters:
      args - the arguments
      player - the player
      methodArgs - the arguments for the method
      Throws:
      CommandException - thrown on command error
    • executeMethod

      public void executeMethod(Method parent, String[] args, T player, Object[] methodArgs, int level) throws CommandException
      Deprecated.
      Attempt to execute a command.
      Parameters:
      parent - the parent method
      args - an array of arguments
      player - the player
      methodArgs - the array of method arguments
      level - the depth of the command
      Throws:
      CommandException - thrown on a command error
    • checkPermission

      protected void checkPermission(T player, Method method) throws CommandException
      Deprecated.
      Throws:
      CommandException
    • invokeMethod

      public void invokeMethod(Method parent, String[] args, T player, Method method, Object instance, Object[] methodArgs, int level) throws CommandException
      Deprecated.
      Throws:
      CommandException
    • hasPermission

      protected boolean hasPermission(Method method, T player)
      Deprecated.
      Returns whether a player has access to a command.
      Parameters:
      method - the method
      player - the player
      Returns:
      true if permission is granted
    • hasPermission

      public abstract boolean hasPermission(T player, String permission)
      Deprecated.
      Returns whether a player permission..
      Parameters:
      player - the player
      permission - the permission
      Returns:
      true if permission is granted
    • getInjector

      public Injector getInjector()
      Deprecated.
      Get the injector used to create new instances. This can be null, in which case only classes will be registered statically.
      Returns:
      an injector instance
    • setInjector

      public void setInjector(Injector injector)
      Deprecated.
      Set the injector for creating new instances.
      Parameters:
      injector - injector or null