Plugin API: commands and permissions

Plugin commands join the server's own command tree. Clients get syntax highlighting and Tab completion, like for Vanilla commands. You build a command like a Brigadier tree: literals (fixed words) and arguments (typed values).

A command

use vanillite_plugin::prelude::*;

registrar.command(
    Command::literal("home")                        // /home
        .requires_permission("myplugin.home")
        .executes(|plugin, ctx, invocation| {       // runs for plain /home
            invocation.reply(ctx, "Going home");
            Ok(1)
        })
        .then(
            Command::argument("name", ArgType::Word)  // /home <name>
                .suggests(|_, ctx, request| {
                    // Tab completion: the host filters by what was typed.
                    vec!["bed".to_owned(), "base".to_owned()]
                })
                .executes(|plugin, ctx, invocation| {
                    let name = invocation.string("name").unwrap_or("home");
                    invocation.reply(ctx, format!("Going to {name}"));
                    Ok(1)
                }),
        ),
);
Part What it does
Command::literal(name) A fixed word. The first literal is the command's name.
Command::argument(name, ArgType) A typed value, read later by its name.
.then(child) Adds a child node.
.executes(f) Runs f when the input ends at this node. Ok(n) is the command's result (like Vanilla's); Err(text) sends the text in red to the sender.
.suggests(f) Answers Tab completion of an argument with a list of strings. request.remaining() is what was typed so far.
.requires_level(n) Needs a Vanilla permission level: 0 everyone, 2 game masters, 3 admins, 4 owners.
.requires_permission(node) Needs a permission node, see below.

A command runs on the owner of its sender (the player's world), but with no world access: it cannot read or set blocks directly. Schedule a task on the player's owner instead (server.schedule(Owner::Entity(world, player), 0, 0, move |ctx| { … })); see Plugin concepts and Plugin API: tasks, config and data. The example hello-plugin does this for /hello pad.

server.run_command("…") runs any command as the console in the next global phase.

Argument types

ArgType Reads Getter on Invocation
Bool true or false bool(name)
Integer {min, max}, Long {min, max} whole numbers with optional bounds int(name)
Float {min, max}, Double {min, max} decimal numbers with optional bounds float(name)
Word one word string(name)
String a word or a quoted string string(name)
GreedyString the rest of the input string(name)
Player, Players a name or a selector like @p, @a players(name)
Entity, Entities a selector like @e[type=cow,limit=1] or a UUID entities(name)
BlockPos a block position; ~ ~1 ~ is resolved block_pos(name)
Vec3 a position; ~ and ^ are resolved vec3(name)
Rotation yaw and pitch rotation(name)
Identifier a resource location like minecraft:stone text(name)
BlockState a block state with optional NBT, like oak_log[axis=x] text(name)
ItemStack an item with components, like diamond_sword[damage=5] text(name)
Component a text component text(name)
NbtCompound an SNBT compound text(name)
GameMode a game mode game_mode(name)
Time a duration like 5s or 2d, in ticks time(name)
Uuid a UUID text(name)

All types after Entities are since ABI 1.5. Other Invocation methods: sender(), world() (the sender's world, none for the console), position(), line() (the command line without /) and reply(ctx, text) (feedback to the sender: a system message, or a log line for the console).

Who runs a command

invocation.sender() is a Sender: Console, Player(uuid), CommandBlock (also a command block minecart) or Entity(uuid) (an entity that is no player, for example through execute as). Plugin commands also work from command blocks and entities.

Late registration (ABI 1.12)

A tree registered in load keeps its place for the life of the server. Since ABI 1.12 the host function register_command also works after load: the server adds trees registered during a tick at its next tick and sends every player the new command list. A top-level name that already exists is skipped. Trees cannot be removed; hide them with a node permission that the permission provider denies. The Bukkit layer uses this for plugins that register commands when they start. The Rust SDK's Registrar is only valid inside load, so late registration is for plugins that call the ABI directly. player_update_commands (ABI 1.10) sends one player its command tree again, for example after permissions changed.

Permissions

A permission is a text node such as myplugin.home.

registrar.permission("myplugin.home", PermissionDefault::True);   // everyone
registrar.permission("myplugin.admin", PermissionDefault::Op);    // operators
PermissionDefault Who has the node when nobody decides
True Everyone
False Nobody
Op Operators (also the default of nodes nobody registered)
NotOp Everyone but operators

server.has_permission(player, node) asks. Wildcards (node.*) are not expanded by the server's registration.

Becoming the permission provider

One plugin per server may answer permission questions (this is what a permission plugin does):

registrar.permission_provider(|player, node| {
    if node == "myplugin.home" { Permission::Allow } else { Permission::Undefined }
});

Permission::Allow grants, Deny denies, Undefined leaves it to the default (operators and the console have it, other players not). The function is called from several threads at once, so it must be thread-safe and fast.

How it fits with the server (from the admin's view): Managing plugins. The Bukkit layer is the provider when Java plugins run, so LuckPerms works.

See also: Plugin API: events (for on_command, which sees every command before it is parsed).

Next: Plugin API: players, entities and worlds.