Script lifecycle
Register in on_load, clean up what you own in on_unload, and know what the server removes for you.
Implement Plugin
Plugin needs new() -> Self and metadata(&self) -> PluginMetadata. The optional on_load(&self, context: Context) and on_unload(&self, context: Context) return Result<(), String>; handle_ipc_message(&self, ...) answers other scripts. Register the type once with register_plugin!(YourPlugin).
The methods take &self, not &mut self: the server can call into your script again while one of your calls is waiting for the server (a block you break fires an event your own handler receives). Keep mutable state behind a Mutex or atomics and never hold a lock across a server call.
Load
Use on_load to read your configuration and register permission nodes, commands, event handlers and tasks. Return an error when the script cannot work: the load is refused and the error is shown in the console.
Unload
When your script is unloaded (by the owner, before a reload, or while the server runs because its version was withdrawn) the server:
- stops calling your event handlers, removes your commands and cancels your scheduled tasks;
- calls your
on_unload; - removes your permission nodes and the other things it holds for you.
In on_unload, save your data and remove what you created in the world: entities and displays, scoreboard objectives and teams. Make cleanup safe after a partial load and when it runs twice.
What the server removes for you
You do not need to clean these up yourself:
- your boss bars, which also go when a script that failed is restarted;
- the equipment looks, effects, attached parts and screen tints you showed to players;
- your pages and screens, and the windows over inventories you made;
- your chunk tickets, permission nodes, translations and files in the server's pack;
- the entities you hid from players, except on a reload, where the new instance keeps them.
Failures and restarts
If your script faults or runs past the call deadline, the server discards that instance and loads the script again from the same file. A script that fails three times within ten minutes is not restarted until the owner reloads or removes it. Read Limits, failures and restarts.
Loading while the server stops
When the server begins to stop, no new script load starts. A load that is already running gets up to 30 seconds to finish. A script whose on_load ends after the stop began is not started: everything it registered is withdrawn. Do not start long work in on_load; schedule it as a task instead.
Repeated unload and load must leave nothing of yours running. A dropped handle is not always the same as removing the game object; use the remove functions of what you created.
Read files after a reload
A new instance of your script runs on_load only after the write_atomic writes of its earlier instances have finished, so reading with std::fs there sees the newest content. The wait is at most 30 seconds; then the script loads anyway and the server log names the unfinished writes.
Declare a version reload safe
Declare it with register_plugin!(MyPlugin, reload_safe) when everything the script needs after a load is read again in on_load. The declaration is recorded in the script's check result as reload_safe, so an update to such a version can be swapped on a running server. Without it (the default), a version is not treated as reload safe.