Command Library
| GitHub |
The Command Library is a simple system for creating commands and registering them with parent commands or the server. The intention of this system is to provide the missing Command
class that Sponge lacks for structuring commands, storing CommandSpec
s, and managing instances. Both an annotation framework and settings (builder-like) system can be used.
This library was added in PR #1 by Simon_Flash.
Command
The Command
[link] class is an abstract class implementing CommandExecutor
that represents a single command. This class stores the command’s CommandSpec
, children, aliases, permission, description, and usage (whew!).
An implementing class should extend Command
and provide a constructor accepting a Command.Settings
, which will contain any settings defined through annotations. The constructor must be public or (ideally) annotated with @Inject
. Examples with both the annotation framework and settings system are shown below (the reader is left to implement Command#execute
as they see fit.
s
@Singleton
@Aliases({"one", "two"})
@Permission("permission.node")
public class ExampleCommand extends Command {
@Inject
private ExampleCommand(Settings settings) {
super(settings);
}
}
@Singleton
public class ExampleCommand extends Command {
@Inject
private ExampleCommand(Settings settings) {
super(settings.aliases("one", "two").permission("permission.node"));
}
}
In addition to a command’s aliases and permission node, it’s description and children may also be defined using either system. However, because annotations only support compile-time values, it is not possible to define higher-level features like arguments in them. Instead, the Settings#arguments
method must be used to define any arguments the command has.
CommandService
The piece that connects these commands together is the CommandService
[link], which stores command instances and is necessary for structuring child commands. To register our command above, create a CommandService
with your PluginContainer
and register the ExampleCommand.class
command. The service is responsible for instantiating any child commands necessary.
PluginContainer container;
CommandService commands = CommandService.of(container);
commands.register(ExampleCommand.class);