Skip to content
ZERNYTH.

Commands

TypedCommand provides validated arguments, aliases, permissions and subcommands. Register it from onEnable.

package dev.example;

import dev.zernyth.api.command.TypedCommand;
import dev.zernyth.api.plugin.JavaPlugin;
import dev.zernyth.api.text.Component;
import java.util.List;

public final class CommandsPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        var count = TypedCommand.Argument.integer("count", 1, 100).optional(3);
        var mode = TypedCommand.Argument.choice("mode", List.of("fast", "safe"));
        var give = new TypedCommand("give", "Give items", "example.give", false,
            List.of("g"), List.of(mode, count), List.of(), context -> {
                context.sender().sendMessage(Component.text(context.get(mode) + " x" + context.get(count)));
                return true;
            });
        new TypedCommand("example", "Example", "", false,
            List.of("ex"), List.of(), List.of(give), null).register(getServer());
    }
}

Rules

  • Branch commands have children and no positional arguments. Leaf commands have an executor.
  • Required arguments come before optional ones.
  • Use an empty string for no permission. Permissions are checked along the whole path.
  • Registering a name that is already taken is rejected.
  • Custom parsers should report expected input errors as IllegalArgumentException and must not block.

Tab completion

suggest(sender, tokens) computes suggestions locally. Native dynamic tab completion for Bedrock clients is not implemented.

Simple commands

Server.registerCommand takes a name, description, permission and a CommandExecutor that receives raw string arguments. Command.close() removes the command.

A CommandSender is only valid during the command callback. Do not keep it for later.

Was this page helpful?