Play Strawberry Docs
Get started

Your first script

Write a complete script with a /hello command, a permission node and a custom join message.

Configure the library

A script is a cdylib library in Rust edition 2024. Cargo fetches the SDK from the Play Strawberry registry. Name it once in ~/.cargo/config.toml, or in .cargo/config.toml in your repository; Get the SDK has the SDK licence.

~/.cargo/config.toml
[registries.strawberry]
index = "sparse+https://sdk.playstrawberry.net/index/"

Then the script's Cargo.toml:

Cargo.toml
[package]
name = "hello-strawberry"
version = "0.1.0"
edition = "2024"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
strawberry-plugin-api = { version = "=0.2.4", registry = "strawberry" }

Write the script

The script registers a permission node, the /hello command and a join-message handler. The handler is registered as blocking: only a blocking handler's returned event is used by the server.

src/lib.rs
use strawberry_plugin_api::{
    Context, Plugin, PluginMetadata, Result, Server,
    command::{Command, CommandError, CommandSender, ConsumedArgs},
    commands::CommandHandler,
    events::{EventHandler, EventPriority, PlayerJoinEvent},
    events_wit::PlayerJoinEventData,
    permission::{Permission, PermissionDefault},
    register_plugin,
    text::TextComponent,
};

/// Answers /hello.
struct HelloCommand;

impl CommandHandler for HelloCommand {
    fn handle(
        &self,
        sender: CommandSender,
        _server: Server,
        _args: ConsumedArgs,
    ) -> Result<i32, CommandError> {
        let name = sender.get_name();
        sender.send_message(TextComponent::text(&format!("Hello, {name}!")));
        Ok(1)
    }
}

/// Replaces the join message. The handler is registered as blocking,
/// so the event it returns is the one the server uses.
struct JoinMessage;

impl EventHandler<PlayerJoinEvent> for JoinMessage {
    fn handle(&self, _server: Server, mut event: PlayerJoinEventData) -> PlayerJoinEventData {
        let name = event.player.get_name();
        event.join_message = TextComponent::text(&format!("{name} joined. Say /hello!"));
        event
    }
}

struct HelloStrawberry;

impl Plugin for HelloStrawberry {
    fn new() -> Self {
        Self
    }

    fn metadata(&self) -> PluginMetadata {
        PluginMetadata {
            name: "hello-strawberry".into(),
            version: env!("CARGO_PKG_VERSION").into(),
            authors: vec!["Your name".into()],
            description: "One command and a join message.".into(),
            dependencies: Vec::new(),
            permissions: Vec::new(),
        }
    }

    fn on_load(&self, context: Context) -> Result<()> {
        // A bare node name is prefixed with the script's name: "hello-strawberry:hello".
        context.register_permission(&Permission {
            node: "hello-strawberry:hello".into(),
            description: "Use /hello".into(),
            default: PermissionDefault::Allow,
            children: vec![],
        })?;

        let command = Command::new(&["hello".to_string()], "Says hello.").execute(HelloCommand);
        context.register_command(command, "hello");

        context.register_event_handler::<PlayerJoinEvent, _>(
            JoinMessage,
            EventPriority::Normal,
            true, // blocking: the returned event counts
        )?;
        Ok(())
    }
}

register_plugin!(HelloStrawberry);

What each part does

  • metadata names the script and lists the sandbox permissions it asks for (none here).
  • on_load gets the Context by value and registers everything. Return an error to refuse the load.
  • register_command(command, "hello") makes hello-strawberry:hello the node a player needs: a bare node name is prefixed with the script name.
  • register_event_handler::<PlayerJoinEvent, _>(handler, priority, blocking) subscribes to joins. For more options see Events and handler options.

Build the component

bash
cargo build --release --target wasm32-wasip2

The result is target/wasm32-wasip2/release/hello_strawberry.wasm. A successful build shows that the script compiles and links against the contract; it does not show that it behaves correctly in game.

Test it

  1. Upload the build and deploy it to your dev server.
  2. Run /hello as a player and from the console.
  3. Join with a second account and check the join message. Its name must be on the dev server's list.
  4. Restart the dev server, then repeat steps 2 and 3.

On this page