Class CommandsManager<T>
- Type Parameters:
T
- command sender 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 Summary
Modifier and TypeFieldDescriptionDeprecated.Mapping of commands (including aliases) with a description.Deprecated.Mapping of commands (not including aliases) with a description.Deprecated.Mapping of commands (not including aliases) with a description.protected Injector
Deprecated.Stores the injector used to getInstance.Deprecated.Used to store the instances associated with a method. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkPermission
(T player, Method method) Deprecated.void
Deprecated.Attempt to execute a command.void
Deprecated.Attempt to execute a command.void
executeMethod
(Method parent, String[] args, T player, Object[] methodArgs, int level) Deprecated.Attempt to execute a command.protected CharSequence
getArguments
(Command cmd) Deprecated.Deprecated.Get a list of command descriptions.Deprecated.Get a map from command name to help message.Deprecated.Get the injector used to create new instances.Deprecated.Get the mapping of methods under a parent command.protected String
getNestedUsage
(String[] args, int level, Method method, T player) Deprecated.Get the usage string for a nested command.protected String
Deprecated.Get the usage string for a command.boolean
hasCommand
(String command) Deprecated.Checks to see whether there is a command named such at the root level.protected boolean
hasPermission
(Method method, T player) Deprecated.Returns whether a player has access to a command.abstract boolean
hasPermission
(T player, String permission) Deprecated.Returns whether a player permission..void
invokeMethod
(Method parent, String[] args, T player, Method method, Object instance, Object[] methodArgs, int level) Deprecated.void
Deprecated.Register an class that contains commands (denoted byCommand
.registerAndReturn
(Class<?> cls) Deprecated.Register an class that contains commands (denoted byCommand
.registerMethods
(Class<?> cls, Method parent) Deprecated.Register the methods of a class.void
setInjector
(Injector injector) Deprecated.Set the injector for creating new instances.
-
Field Details
-
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 respectiveMethod
. The child map has the key of the command name (one for each alias) with the method. -
instances
Deprecated.Used to store the instances associated with a method. -
descs
Deprecated.Mapping of commands (not including aliases) with a description. This is only for top level commands. -
injector
Deprecated.Stores the injector used to getInstance. -
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
Deprecated.Register an class that contains commands (denoted byCommand
. 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
Deprecated.Register an class that contains commands (denoted byCommand
. 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 ofCommand
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
Deprecated.Register the methods of a class. This will automatically construct instances as necessary.- Parameters:
cls
- the class to registerparent
- the parent method- Returns:
- Commands Registered
-
hasCommand
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
Deprecated.Get a list of command descriptions. This is only for root commands.- Returns:
- a map of commands
-
getMethods
Deprecated.Get the mapping of methods under a parent command.- Returns:
- the mapping
-
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
Deprecated.Get the usage string for a command.- Parameters:
args
- the argumentslevel
- the depth of the commandcmd
- the command annotation- Returns:
- the usage string
-
getArguments
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 argumentslevel
- the depth of the commandmethod
- the parent methodplayer
- 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 runargs
- argumentsplayer
- command sourcemethodArgs
- method arguments- Throws:
CommandException
- thrown when the command throws an error
-
execute
Deprecated.Attempt to execute a command.- Parameters:
args
- the argumentsplayer
- the playermethodArgs
- 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 methodargs
- an array of argumentsplayer
- the playermethodArgs
- the array of method argumentslevel
- the depth of the command- Throws:
CommandException
- thrown on a command error
-
checkPermission
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
Deprecated.Returns whether a player has access to a command.- Parameters:
method
- the methodplayer
- the player- Returns:
- true if permission is granted
-
hasPermission
Deprecated.Returns whether a player permission..- Parameters:
player
- the playerpermission
- the permission- Returns:
- true if permission is granted
-
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
Deprecated.Set the injector for creating new instances.- Parameters:
injector
- injector or null
-