Skip to content

Using the CommandAPI

ancap-kun edited this page Feb 21, 2023 · 6 revisions

CommandAPI is an API for processing user commands in Minecraft.

Advantages over the Bukkit Framework API

  • Full asynchrony (when using the API implementation in the form of AncapPlugin)
  • More complete implementation of features from Minecraft Command API (for example, features such as chat previews and color hints)
  • Rich toolkit for creating highly loaded command handlers
  • Ability to process commands in a declarative style

Code example

How the processing of commands with Bukkit looks like...

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] arguments) {
        if (arguments.length == 0) {
            sender.sendMessage(LAPI.localized(AncapPlugin.MESSAGE_DOMAIN+"enter-language", sender.getName()));
            return true;
        }
        if (arguments[0].equals("set")) {
            if (arguments.length < 2) {
                Event event = new NotEnoughArgsEvent(sender, 1);
                Bukkit.getPluginManager().callEvent(event);
            }
            Event event = new LanguageChangeEvent(sender, arguments[1]);
            Bukkit.getPluginManager().callEvent(event);
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] arguments) {
        if (arguments.length == 0) {
            return List.of("set");
        }
        if (arguments[0].equals("set")) {
            return List.of(
                    "ru", 
                    "en", 
                    "uk"
            );
        }
        return Collections.emptyList();
    }

And how would it be written with CommandAPI from AncapFramework...

public LanguageChangeInput() {
    super(new Delegate(
        new Raw(new Advice(new LAPIMessage(Artifex.class, "command.language.enter-language"))),
        new SubCommand(
            new StringDelegatePattern("set"),
            new Arguments(
                new Accept(new Argument("language", new Self())),
                dispatch -> {
                    LAPI.setupLanguage(dispatch.source().sender().getName(), Language.of(dispatch.arguments().get("language", String.class)));
                    new Communicator(dispatch.source().sender()).send(new LAPIMessage(Artifex.class, "command.language.setup"));
                }
            )
        )
    ));
}

Command registration

Commands are registered via AncapPluginAPI via the this.registerExecutor(String CommandName, CommandOperator operator) method the AncapPlugin heir.

All commands must be written in ancapplugin.yml:

    commands:
      list:
        - language
      aliases:
        language:
          - lang

(aliases do not have to be prescribed)

Features of the CommandAPI implementation in AncapPlugin

AncapPlugin creates a new thread to process each new command.

You can easily create long-term queries to a remote database and are not afraid that everything will hang. This is implemented by processing player packets, packets are also processed asynchronously.

Also, all objects passed by the implementation can be safely moved between threads. They are all thread-safe.

Due to the features of Bukkit, in addition to ancapplugin.yml, commands must also be registered in plugin.yml, otherwise it will be impossible to complete the command.

Clone this wiki locally