Skip to content

CommandAPI command executors should have an overload for arguments accessible via a map #360

@JorelAli

Description

@JorelAli

Description

CommandAPI command executors currently only consist of a command sender and an array of arguments:

(sender, args) -> {
    // ...
}

It is useful to be able to access arguments by node name, rather than by index - especially in the case of using optional arguments:

(CommandSender sender, Map<String, Object> args) -> {
    String input = (String) args.get("blah");
    int amount = (int) args.getOrDefault("amount", 1);
}

It would be ideal to implement an overload for the various executes() methods that instead of accepting a (sender, args), it can instead accept a generalized (executioninfo) which would allow the CommandAPI to be more easily extensible in the future and provide users with a more accessible API.

Expected code

This could be implemented using a record instead:

class ExecutionInfo<Sender extends CommandSender> {
  public Object[] args();
  public Sender sender();
  public Map<String, Object> argsMap();
}

Generic structure for a player execution, using the generic type to enforce the return type of sender() as as Player object:

@FunctionalInterface
public interface PlayerExecution {

    public void run(ExecutionInfo<Player> info);

}

The implementation of argsMap would be as simple as tweaking the implementation of CommandAPIHandler.argsToObjectArr(), and shoving them into a LinkedHashMap. We'll use Map as the main interface for argsMap() for simplicity, but mention in JavaDocs that the implementation is declared in order (so can be iterated upon if users so desire...)

Extra details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestimplemented for next releaseThis has been implemented in the current dev build for the next public release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions