Play Strawberry Docs
Scripts

Commands and suggestions

Build a command tree with typed arguments, answer suggestions, and keep secrets out of the log.

Build the tree

Command::new(&names, description) uses the first name as the command and the others as aliases. .execute(handler) runs a CommandHandler when the command is typed alone; .then(node) adds a branch. CommandNode::literal(name) is a fixed word and CommandNode::argument(name, &ArgumentType) a typed value; nodes take .execute, .then and .suggest too.

Context::register_command(command, node) registers the tree with the permission node a sender needs. A bare node name (no colon) is prefixed with your script name.

rust
let word = ArgumentType::String(StringType::SingleWord);
let warp = Command::new(&["warp".to_string()], "Travel to a warp.")
    .then(CommandNode::argument("name", &word).suggest(WarpNames).execute(Warp));
context.register_command(warp, "warp");

Hide a sub-command behind a permission

CommandNode::require_permission(node) puts a sub-command or an argument, and everything under it, behind a permission (Brigadier's requires, Paper's hidden commands). Players without the permission do not see it in tab-complete and cannot run it, on Java and Bedrock. The server checks it itself without calling your script; the console has every permission.

rust
let models = Command::new(&["models".into()], "Browse the models")
    .execute(Browse)
    .then(CommandNode::literal("give").require_permission("admin").execute(Give));
context.register_command(models, "browse");
  • A node without a namespace (admin) is your script's own (<plugin>:admin). Register it with Context::register_permission to give it a default, such as an operator level.
  • Several calls add up: all of them must hold.
  • A malformed node stops the registration with an error instead of leaving the sub-command open.

Read the arguments

ConsumedArgs::get_value(name) returns an Arg. Match the variant you expect (Arg::Simple for a string, Arg::Num for numbers, Arg::Players for player selectors) and answer anything else with an error. A handler returns Ok(n) on success and an error otherwise: CommandError::InvalidConsumption, PermissionDenied or CommandFailed(text), whose text the sender sees.

An item argument arrives as Arg::Item and a resource location as Arg::ResourceLocation, each holding the text the player typed, such as minecraft:stick with its components. The server refuses an unknown item or a malformed value before your script runs.

Suggestions

Implement commands::CommandSuggestionHandler::suggest(sender, server, request) -> CommandSuggestions and attach it with .suggest(handler) on an argument node. SuggestionRequest has input, cursor, start and remaining; your answer has start, length and values (a value and an optional tooltip each).

rust
/// /warp <name>: the name must be one of WARPS.
struct Warp;

impl CommandHandler for Warp {
    fn handle(
        &self,
        sender: CommandSender,
        _server: Server,
        args: ConsumedArgs,
    ) -> Result<i32, CommandError> {
        let Arg::Simple(name) = args.get_value("name") else {
            return Err(CommandError::InvalidConsumption(Some("name".into())));
        };
        if !WARPS.contains(&name.as_str()) {
            return Err(CommandError::CommandFailed(TextComponent::text(&format!(
                "There is no warp called {name}."
            ))));
        }
        let Some(player) = sender.as_player() else {
            return Err(CommandError::CommandFailed(TextComponent::text("Only players can warp.")));
        };
        player.send_system_message(TextComponent::text(&format!("Warping to {name}...")), false);
        Ok(1)
    }
}

/// Suggests the warp names that start with what the player typed.
struct WarpNames;

impl CommandSuggestionHandler for WarpNames {
    fn suggest(
        &self,
        _sender: CommandSender,
        _server: Server,
        request: SuggestionRequest,
    ) -> CommandSuggestions {
        let typed = request.remaining.to_lowercase();
        let values = WARPS
            .iter()
            .filter(|warp| warp.starts_with(&typed))
            .map(|warp| CommandSuggestion { value: (*warp).to_string(), tooltip: None })
            .collect();
        CommandSuggestions {
            start: request.start,
            length: request.remaining.len() as u32,
            values,
        }
    }
}

Keep the replacement range inside the request, slice strings only at character boundaries, and never suggest data the sender may not see.

Sensitive commands

A command that carries a password, a PIN or a token is declared sensitive with .sensitive(). The server log then shows the command word and [arguments hidden] instead of the arguments, for every name and alias of the command, in any case and with a namespace, whatever other scripts do with the command event.

As on Paper, the server logs a player's command ("<name> issued server command: /...") before scripts see the command event, so a command a script cancels is logged too.

The command event never carries a command that contains a line break or another control character: the server never runs such a command and never writes it into the console or the log.

rust
let pin = Command::new(&["pin".to_string()], "Enter your PIN.")
    .sensitive()
    .then(CommandNode::argument("code", &word).execute(Pin));
context.register_command(pin, "pin");

Remove a command

Context::remove_command(name) takes your script's command off with all its aliases and their <plugin>:<name> forms (Paper's CommandMap unregister together with Player#updateCommands()). name is the command's name or one of its aliases, with or without your script's prefix. The players who could see the command get their command list again at once, so it leaves their chat suggestions without a restart, and you can register the same name again right away: a renamed command, or a script that was deleted or changed.

The answer is Ok(()) when the command was removed, or a CommandRemovalError when nothing changed: Unknown (no command of that name), NotOwn (another script's command), BuiltIn (a vanilla command or one of the server's own) or Reserved (a platform command such as /marketplace). A script can only remove its own commands. When another script holds the plain name, the plain name means your own <plugin>:<name> command.

Context::unregister_command(name) does the same without an answer. A command removed and registered again as often as you like leaves nothing of the old version behind. Your commands also go by themselves when your script is unloaded.

Run a command as the sender

sender.dispatch_command(command) on the CommandSender your handler receives runs a command at once as that sender, from where it is: a command block's position and facing, a player's place and permissions, or the console (Paper's Bukkit.dispatchCommand(sender, command)). The sender sees what the command answers.

A script command that took a built-in's name, such as tell or msg, can answer the senders it handles itself and hand the others to the built-in with sender.dispatch_command("minecraft:tell ..."), so a command block's @p and relative coordinates keep working.

The call returns false, running nothing, when no command of that name is known. A command that loads or unloads one of Play Strawberry's own scripts or the login script is refused the same way.

Who sees which command

Players are only sent the commands they may use. When a script gives or takes a player's permission, that player's command list updates by itself.

To hide commands from a player's list or stop completions, use the extension events in Tab-complete and command lists.

Names you cannot take

Every command of Play Strawberry's own scripts and the sensitive commands of the server's login script belong to them: another script's command of the same name is taken off, and registering one later is refused with a console line. Names are compared in lower case, without the slash and the namespace.

/marketplace and /friends

These two commands are on every Play Strawberry server, open to every player, and always reach the platform as the player typed them:

  • A script cannot cancel or change them through the command event.
  • A permission does not take them away from a player.
  • Packet hooks still see the player's packets for them (the chat command, the signed command and the completion request), but a cancel or a replacement of such a packet is not applied.

Permissions

Register your permission node with Context::register_permission before the command. A sandbox permission in your metadata is a different thing: it gives your script access, not a player. See Permissions.

On this page