T
- command sender class@Deprecated
public abstract class CommandsManager<T>
extends java.lang.Object
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 java.util.Map<java.lang.reflect.Method,java.util.Map<java.lang.String,java.lang.reflect.Method>> |
commands
Deprecated.
Mapping of commands (including aliases) with a description.
|
protected java.util.Map<java.lang.String,java.lang.String> |
descs
Deprecated.
Mapping of commands (not including aliases) with a description.
|
protected java.util.Map<java.lang.String,java.lang.String> |
helpMessages
Deprecated.
Mapping of commands (not including aliases) with a description.
|
protected Injector |
injector
Deprecated.
Stores the injector used to getInstance.
|
protected java.util.Map<java.lang.reflect.Method,java.lang.Object> |
instances
Deprecated.
Used to store the instances associated with a method.
|
protected static org.slf4j.Logger |
logger
Deprecated.
|
Constructor and Description |
---|
CommandsManager()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkPermission(T player,
java.lang.reflect.Method method)
Deprecated.
|
void |
execute(java.lang.String[] args,
T player,
java.lang.Object... methodArgs)
Deprecated.
Attempt to execute a command.
|
void |
execute(java.lang.String cmd,
java.lang.String[] args,
T player,
java.lang.Object... methodArgs)
Deprecated.
Attempt to execute a command.
|
void |
executeMethod(java.lang.reflect.Method parent,
java.lang.String[] args,
T player,
java.lang.Object[] methodArgs,
int level)
Deprecated.
Attempt to execute a command.
|
protected java.lang.CharSequence |
getArguments(Command cmd)
Deprecated.
|
java.util.Map<java.lang.String,java.lang.String> |
getCommands()
Deprecated.
Get a list of command descriptions.
|
java.util.Map<java.lang.String,java.lang.String> |
getHelpMessages()
Deprecated.
Get a map from command name to help message.
|
Injector |
getInjector()
Deprecated.
Get the injector used to create new instances.
|
java.util.Map<java.lang.reflect.Method,java.util.Map<java.lang.String,java.lang.reflect.Method>> |
getMethods()
Deprecated.
Get the mapping of methods under a parent command.
|
protected java.lang.String |
getNestedUsage(java.lang.String[] args,
int level,
java.lang.reflect.Method method,
T player)
Deprecated.
Get the usage string for a nested command.
|
protected java.lang.String |
getUsage(java.lang.String[] args,
int level,
Command cmd)
Deprecated.
Get the usage string for a command.
|
boolean |
hasCommand(java.lang.String command)
Deprecated.
Checks to see whether there is a command named such at the root level.
|
protected boolean |
hasPermission(java.lang.reflect.Method method,
T player)
Deprecated.
Returns whether a player has access to a command.
|
abstract boolean |
hasPermission(T player,
java.lang.String permission)
Deprecated.
Returns whether a player permission..
|
void |
invokeMethod(java.lang.reflect.Method parent,
java.lang.String[] args,
T player,
java.lang.reflect.Method method,
java.lang.Object instance,
java.lang.Object[] methodArgs,
int level)
Deprecated.
|
void |
register(java.lang.Class<?> cls)
Deprecated.
Register an class that contains commands (denoted by
Command . |
java.util.List<Command> |
registerAndReturn(java.lang.Class<?> cls)
Deprecated.
Register an class that contains commands (denoted by
Command . |
java.util.List<Command> |
registerMethods(java.lang.Class<?> cls,
java.lang.reflect.Method parent)
Deprecated.
Register the methods of a class.
|
void |
setInjector(Injector injector)
Deprecated.
Set the injector for creating new instances.
|
protected static final org.slf4j.Logger logger
protected java.util.Map<java.lang.reflect.Method,java.util.Map<java.lang.String,java.lang.reflect.Method>> commands
Method
. The child map has
the key of the command name (one for each alias) with the
method.protected java.util.Map<java.lang.reflect.Method,java.lang.Object> instances
protected java.util.Map<java.lang.String,java.lang.String> descs
protected Injector injector
protected java.util.Map<java.lang.String,java.lang.String> helpMessages
public void register(java.lang.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
- the class to registerpublic java.util.List<Command> registerAndReturn(java.lang.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
- the class to registerCommand
annotations from registered commands,
for use in eg. a dynamic command registration system.public java.util.List<Command> registerMethods(java.lang.Class<?> cls, java.lang.reflect.Method parent)
cls
- the class to registerparent
- the parent methodpublic boolean hasCommand(java.lang.String command)
command
- the commandpublic java.util.Map<java.lang.String,java.lang.String> getCommands()
public java.util.Map<java.lang.reflect.Method,java.util.Map<java.lang.String,java.lang.reflect.Method>> getMethods()
public java.util.Map<java.lang.String,java.lang.String> getHelpMessages()
protected java.lang.String getUsage(java.lang.String[] args, int level, Command cmd)
args
- the argumentslevel
- the depth of the commandcmd
- the command annotationprotected java.lang.CharSequence getArguments(Command cmd)
protected java.lang.String getNestedUsage(java.lang.String[] args, int level, java.lang.reflect.Method method, T player) throws CommandException
args
- the argumentslevel
- the depth of the commandmethod
- the parent methodplayer
- the playerCommandException
- on some errorpublic void execute(java.lang.String cmd, java.lang.String[] args, T player, java.lang.Object... methodArgs) throws CommandException
cmd
- command to runargs
- argumentsplayer
- command sourcemethodArgs
- method argumentsCommandException
- thrown when the command throws an errorpublic void execute(java.lang.String[] args, T player, java.lang.Object... methodArgs) throws CommandException
args
- the argumentsplayer
- the playermethodArgs
- the arguments for the methodCommandException
- thrown on command errorpublic void executeMethod(java.lang.reflect.Method parent, java.lang.String[] args, T player, java.lang.Object[] methodArgs, int level) throws CommandException
parent
- the parent methodargs
- an array of argumentsplayer
- the playermethodArgs
- the array of method argumentslevel
- the depth of the commandCommandException
- thrown on a command errorprotected void checkPermission(T player, java.lang.reflect.Method method) throws CommandException
CommandException
public void invokeMethod(java.lang.reflect.Method parent, java.lang.String[] args, T player, java.lang.reflect.Method method, java.lang.Object instance, java.lang.Object[] methodArgs, int level) throws CommandException
CommandException
protected boolean hasPermission(java.lang.reflect.Method method, T player)
method
- the methodplayer
- the playerpublic abstract boolean hasPermission(T player, java.lang.String permission)
player
- the playerpermission
- the permissionpublic Injector getInjector()
public void setInjector(Injector injector)
injector
- injector or null