Plugin API: tasks, config and data

Tasks

Tasks run your code later or repeatedly, on the right owner (see Plugin concepts). Time is counted in ticks (20 per second).

Method Runs on Notes
server.schedule(owner, delay, period, task) Owner::Global, World(w), Location(w, pos) or Entity(w, uuid) After delay ticks, then every period ticks; period 0 runs once. The task gets a Ctx for that owner.
server.schedule_entity(world, entity, delay, period, task, retired) The entity's owner If the entity is gone, retired runs once instead.
server.run_later(delay, task) Global Shortcut for schedule(Owner::Global, delay, 0, …).
server.run_async(task) A background thread Off the tick, for files, databases and network. No world access.
server.run_async_timer(delay, period, task) A background thread Repeats until cancelled. Runs never overlap.
server.cancel(task_id) Stops a task; its data is dropped.
let server = ctx.server();
// Read slowly on a background thread, then hand the result to the player's owner.
server.run_async(move || {
    let text = std::fs::read_to_string("motd.txt").unwrap_or_default();
    let _ = server.schedule(Owner::Entity(world, player), 0, 0, move |ctx| {
        let _ = ctx.server().send_message(player, text.clone());
    });
})?;

Tasks of a plugin that is switched off are dropped. A task that panics switches the plugin off; the server keeps running.

Configuration

Each plugin has a data folder plugins/<id>/ (server.data_dir()), created by the server.

  1. In load, call registrar.config(include_str!("../config.toml")). If plugins/<id>/config.toml does not exist yet, the defaults are written (with their comments), so admins get a documented file.
  2. Read values with dotted keys, from any thread:
Method Reads
server.config_string("messages.join") A string, as Option<String>
config_int, config_float, config_bool A number or a boolean (integers convert to floats)
config_toml("key") Any value, also lists and tables, as TOML text
  1. Change and save: server.set_config("stats.count", "5") sets a key to a TOML value, server.save_config() writes the file back.

Server rules from vanillite.toml are readable with server.rule("chat.max-pending-messages"); they are not writable. A config file in the plugin's folder is read when the plugin loads; there is no automatic reload except in development mode (Plugin concepts).

Persistent data

Small values saved with a player or any entity, stored in the entity's own data, so they survive restarts. Use Ctx in the entity's world:

let joins = match ctx.entity_data(world, player, "joins") {
    Ok(Some(DataValue::Int(n))) => n,
    _ => 0,
} + 1;
ctx.set_entity_data(world, player, "joins", &DataValue::Int(joins))?;
Method What
entity_data(world, entity, key) Reads a value (None if there is none)
set_entity_data(world, entity, key, &value) Writes
remove_entity_data Removes one key
entity_data_keys Lists the plugin's keys

DataValue is Byte, Short, Int, Long, Float, Double, Bytes, String, Ints or Longs. Since ABI 1.20 the raw host functions also store a list or a compound (a value with named entries, Bukkit's nested container) as SNBT text; the Rust DataValue has no variant for them yet. Limits: 256 keys per entity, keys up to 256 bytes, values up to 64 KiB. Keys are kept apart per plugin. The same works for items (server.item_with_data, item_data), see Plugin API: players, entities and worlds. For more data than this, keep your own files in the data folder and write them from a background task.

Logging

vanillite_plugin::info!, warn!, error! and debug! take format! arguments and write to the server log with the plugin's id as prefix. Before load the text goes to standard error.

Plugin messages (client mods)

registrar.on_plugin_message("myplugin:sync", priority::NORMAL, |_, ctx, message| { … }) receives what a client mod sends on a channel; server.send_plugin_message(player, "myplugin:sync", &bytes) answers. Up to 128 channels per plugin, 32767 bytes per message. The raw host function register_channel can add a channel in any phase, also after load (older servers accept it during load only and answer WRONG_PHASE later); a plugin that hosts others, such as the Bukkit layer, uses this for the channels its plugins register while they enable.

Server jar, entity ids and data packs (ABI 1.14)

Three host functions of the plugin ABI (since 1.14) are meant for plugins that need Mojang's own classes or that send packets for entities that do not exist on the server. They are part of the C interface; the Rust SDK has no wrapper for them yet.

Function What it does
vanilla_server_jar Gives the path of Mojang's server jar of this Minecraft version. The server downloads it into its cache the first time it is needed and checks it against a fixed checksum; that blocks, so call it from load or a background thread, never from a tick. It fails when the jar cannot be had (offline and no local copy). Vanillite never ships Mojang's code: this is the copy the user's own server downloads.
entity_ids_reserve Reserves 1 to 1,048,576 consecutive network entity ids from the server's counter and gives the first. For entities that exist only in packets a plugin sends (holograms, NPCs), so their ids never collide with the server's own. Works from any thread.
data_packs Gives the world's datapacks folder and the ids of the selected data packs in load order, one per line (vanilla, the name of a feature pack, or file/<name> for a pack in that folder), as saved in level.dat. Works from any thread after load.

The plugins themselves

server.plugins() returns the enabled plugins with their versions in load order. Plugins can offer services to each other, see Plugin API: slots and hosted plugins.

See also: Plugin API: players, entities and worlds.

Next: Plugin API: packets.