Skip to content
sk89q edited this page Aug 19, 2014 · 4 revisions

Commands locals is a "context" for commands. Its purpose is to pass extra information related to the execution of a command without passing it as an argument in the command library.

Context instead of parameters

For example, instead of

command.call(player, arguments)

the player would be passed as:

CommandLocals locals = new CommandLocals();
locals.put(Player.class, player);

command.call(arguments, locals, /* ... */);

Unfortunately, this does make for additional code. On the other hand, if you are using the parametric builder, this is not a problem because there is no need to directly work with the call() method. The builder will generate its own command handler to call yours.

The reasoning behind this design decision was to:

  • Allow the passing of many contextual items at once.
  • Avoid coupling the command handler to a certain manner of calling commands.
  • Avoid adding generics to all the command classes (i.e. CommandCallable<Player>, Dispatcher<Player>, etc.)

Reducing the boilerplate

Even if you are writing your own CommandCallable instances, you can work around the problem with some simple inheritance:

abstract class BaseCommand implements CommandCallable {

    public final boolean call(String arguments, CommandLocals locals, String[] parentCommands) {
        Player player = locals.get(Player.class);
        return execute(player, arguments);
    }

    public abstract boolean execute(Player player, String arguments);

}

Shortcuts

If you store an object under its own class, such as in locals.put(Player.class, player), there is a shortcut method to pull it out again with just locals.get(Player.class).

Clone this wiki locally