T - command sender classpublic abstract class CommandsManager<T> extends Object
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.
| Modifier and Type | Field and Description |
|---|---|
protected Map<Method,Map<String,Method>> |
commands
Mapping of commands (including aliases) with a description.
|
protected Map<String,String> |
descs
Mapping of commands (not including aliases) with a description.
|
protected Map<String,String> |
helpMessages
Mapping of commands (not including aliases) with a description.
|
protected Injector |
injector
Stores the injector used to getInstance.
|
protected Map<Method,Object> |
instances
Used to store the instances associated with a method.
|
protected static Logger |
logger
Logger for general errors.
|
| Constructor and Description |
|---|
CommandsManager() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkPermission(T player,
Method method) |
void |
execute(String[] args,
T player,
Object... methodArgs)
Attempt to execute a command.
|
void |
execute(String cmd,
String[] args,
T player,
Object... methodArgs)
Attempt to execute a command.
|
void |
executeMethod(Method parent,
String[] args,
T player,
Object[] methodArgs,
int level)
Attempt to execute a command.
|
protected CharSequence |
getArguments(Command cmd) |
Map<String,String> |
getCommands()
Get a list of command descriptions.
|
Map<String,String> |
getHelpMessages()
Get a map from command name to help message.
|
Injector |
getInjector()
Get the injector used to create new instances.
|
Map<Method,Map<String,Method>> |
getMethods() |
protected String |
getNestedUsage(String[] args,
int level,
Method method,
T player)
Get the usage string for a nested command.
|
protected String |
getUsage(String[] args,
int level,
Command cmd)
Get the usage string for a command.
|
boolean |
hasCommand(String command)
Checks to see whether there is a command named such at the root level.
|
protected boolean |
hasPermission(Method method,
T player)
Returns whether a player has access to a command.
|
abstract boolean |
hasPermission(T player,
String perm)
Returns whether a player permission..
|
void |
invokeMethod(Method parent,
String[] args,
T player,
Method method,
Object instance,
Object[] methodArgs,
int level) |
void |
register(Class<?> cls)
Register an class that contains commands (denoted by
Command. |
List<Command> |
registerAndReturn(Class<?> cls)
Register an class that contains commands (denoted by
Command. |
void |
setInjector(Injector injector)
Set the injector for creating new instances.
|
protected static final Logger logger
protected Map<Method,Map<String,Method>> commands
Method. The child map has
the key of the command name (one for each alias) with the
method.protected Map<Method,Object> instances
protected Map<String,String> descs
protected Injector injector
public void register(Class<?> cls)
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.cls - public List<Command> registerAndReturn(Class<?> cls)
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.cls - Command annotations from registered commands,
for use in eg. a dynamic command registration system.public boolean hasCommand(String command)
command - public Map<String,String> getCommands()
public Map<String,String> getHelpMessages()
protected String getUsage(String[] args, int level, Command cmd)
args - level - cmd - protected CharSequence getArguments(Command cmd)
protected String getNestedUsage(String[] args, int level, Method method, T player) throws CommandException
args - level - method - player - CommandExceptionpublic void execute(String cmd, String[] args, T player, Object... methodArgs) throws CommandException
cmd - command to runargs - argumentsplayer - command sourcemethodArgs - method argumentsCommandExceptionpublic void execute(String[] args, T player, Object... methodArgs) throws CommandException
args - player - methodArgs - CommandExceptionpublic void executeMethod(Method parent, String[] args, T player, Object[] methodArgs, int level) throws CommandException
parent - args - player - methodArgs - level - CommandExceptionprotected void checkPermission(T player, Method method) throws CommandException
CommandExceptionpublic void invokeMethod(Method parent, String[] args, T player, Method method, Object instance, Object[] methodArgs, int level) throws CommandException
CommandExceptionprotected boolean hasPermission(Method method, T player)
method - player - public abstract boolean hasPermission(T player, String perm)
player - perm - public Injector getInjector()
public void setInjector(Injector injector)
injector - injector or nullCopyright © 2010-2014. All Rights Reserved.