Plugin API: events
Events let a plugin react to what happens in the game. This page lists every event of the native plugin API (ABI 1.24) and the Rust method that listens to it. The list comes from abi::event in vanillite-abi; the Rust methods are in registrar.rs. Each event's fields are documented on its constant in the ABI file.
How to listen
Register in load. Every on_* method takes a priority and a handler:
registrar.on_chat(priority::NORMAL, |plugin, ctx, chat| {
if chat.text().contains("badword") {
chat.set_cancelled(true); // the message is not sent
}
});
The handler gets the plugin object, a Ctx (see Plugin concepts) and the event. Whether the context can read the world depends on the event: for example join, leave, respawn and world change give you the player's world, and so do the events a world fires while it handles a player's packets (move, sneak, sprint, held item, flight, resource pack, swap hands, drop, consume) and the entity interact, attack, portal and experience change events; login and mob death do not (the world is in the middle of its tick). The damage, resurrect, death and drops events of players give the whole world too, whether the damage comes from a mob, a projectile, another player, /damage and /kill, or from the player's own tick (a fall, fire, drowning, starvation or an effect): a handler can read the victim's vehicle, passengers, nearby entities and position, a mob's target, and a dying player's inventory. Where the world is given, a handler can read it but not change it: a change answers WRONG_OWNER. If you need to change the world, schedule a task on its owner (Plugin API: tasks, config and data).
| Priority | Value | Use |
|---|---|---|
LOWEST, LOW |
-200, -100 | Run first. |
NORMAL |
0 | Default. |
HIGH, HIGHEST |
100, 200 | Run late; HIGHEST has the last word. |
MONITOR |
1000 | Only watches the result. Must not change the event. |
- Cancelling: for events marked "yes" below,
set_cancelled(true)stops the action. Vanilla corrects the client where needed. - Skip cancelled events:
registrar.ignore_cancelled().on_chat(…)skips the next handler while its event is cancelled (like Bukkit'signoreCancelled). - Stop listening:
server.unsubscribe(kind)removes the plugin's handlers of one event kind at any time. - One-batch event:
on_players_movedis called once per world and tick with all players that moved, not once per player. - Events from your own actions (ABI 1.21): by default a plugin is not called while one of its own calls is still running, so an event that its own action fires (spawning an entity, an explosion, damage) does not reach its handlers. A handler subscribed with the flag
subscribe_flag::NESTEDdoes get it at once, as in Bukkit, with a context of its own. The Rust SDK has no switch for it yet; use the raw host functionsubscribe_flags. Details and limits: Plugin concepts. - Cost: a kind without a listener costs one check where the game fires it. The Bukkit layer subscribes only to events some Java plugin listens to.
Typed events
These have their own types in the SDK, with methods such as player(), name(), world():
| Method | Type | Useful methods |
|---|---|---|
on_login |
Login |
name(), address(), reason(), refuse(text), allow() |
on_join |
PlayerJoin |
player(), name(), message(), set_message(text) (the join message everyone sees) |
on_leave |
PlayerLeave |
same, for the leave message |
on_chat, on_command |
PlayerText |
text(), set_text(…), set_cancelled(…) |
on_players_moved |
PlayersMoved |
world(), moves() |
on_entity_damage |
EntityDamage |
entity(), damage_type(), attacker(), amount(), set_amount(…) |
on_block_break, on_block_place, bed events |
BlockChange |
world(), player(), pos(), state() |
on_death |
PlayerDeath |
set_message(…), set_keep_inventory(bool), killer() |
on_entity_death |
EntityDeath |
entity(), killer() |
on_respawn, on_change_world |
PlayerWorld |
player(), world() |
on_game_mode_change |
GameModeChange |
to() |
on_interact |
Interact |
hand(), item(), action(), block() |
on_plugin_message |
PluginMessage |
channel(), data() |
on_advancement |
AdvancementDone |
advancement() |
on_plugin_event |
CustomEvent |
name(), source(), data() |
All other events: GameEvent
Since ABI 1.6 the other events share one type, GameEvent. Which fields an event fills is documented per kind in the ABI. The getters are entity(), other(), player(), source(), pos(), state(), item(), other_item(), text(), cause(), value(), extra(), amount() and flags(). What a handler may change has setters: set_to(location) (move target), set_item(snbt) (replace drops, results), set_text(…), set_amount(…), set_value(…) (experience, cost, burn time), set_flags(…). You can also listen to any kind by number with registrar.on_event(kind, priority, handler).
Players
| Method | When it fires | Cancel | Since ABI |
|---|---|---|---|
on_join |
A player joined (after the join message) | - | 1.0 |
on_leave |
A player left. It fires while the player is still in its world (the world can be read, not changed), before the player is saved, its vehicle removed and the entity despawned, like Paper's quit event. The leave message is still sent where Vanilla sends it. Since ABI 1.24 the player's persistent data is taken out of the world while the handlers run, so they can change it (as in Paper's quit event); it is put back and saved with the player | - | 1.0 |
on_chat |
A player sends a chat message | yes | 1.0 |
on_command |
A player runs a command (any command, before it is parsed) | yes | 1.0 |
on_players_moved |
The players of a world that moved this tick | - | 1.0 |
on_bed_enter |
A player is about to fall asleep in a bed | yes | 1.3 |
on_bed_leave |
A player left its bed | - | 1.3 |
on_spawn_change |
A bed or respawn anchor is about to become a player's respawn point | yes | 1.3 |
on_login |
A player logs in, after the server's checks (bans, whitelist, player limit), before it joins. A refusal is a login disconnect; a plugin can also let a refused player in | yes (refuses) | 1.4 |
on_death |
A player died: change the death message or keep the inventory | - | 1.4 |
on_respawn |
A player respawned, after death or the end credits | - | 1.4 |
on_change_world |
A player arrived in another world through a portal or a teleport | - | 1.4 |
on_game_mode_change |
A player's game mode is about to change (/gamemode or the forced default) |
yes | 1.4 |
on_interact |
A player uses its item on a block or in the air | yes | 1.4 |
on_plugin_message |
A player sent a message on a channel the plugin registered | - | 1.5 |
on_advancement |
A player completed an advancement | - | 1.5 |
on_player_move |
A player moved or turned. Cancelling sets the player back; a handler may change the target | yes | 1.6 |
on_player_teleport |
A player is about to be teleported (target and world can change) | yes | 1.6 |
on_player_interact_entity |
A player uses its item on an entity | yes | 1.6 |
on_player_attack_entity |
A player is about to attack an entity (left click) | yes | 1.6 |
on_player_drop_item |
A player drops an item | yes | 1.6 |
on_entity_pickup_item |
An entity (a player or a mob) picks up an item, arrow or trident | yes | 1.6 |
on_player_toggle_sneak |
A player starts or stops sneaking | - | 1.6 |
on_player_toggle_sprint |
A player starts or stops sprinting | - | 1.6 |
on_player_toggle_flight |
A player starts or stops flying | yes | 1.6 |
on_player_item_held |
A player selects another hotbar slot | yes | 1.6 |
on_player_item_consume |
A player finishes eating or drinking an item | yes | 1.6 |
on_player_kick |
A player is about to be kicked (the reason can change; not for protocol errors and shutdown) | yes | 1.6 |
on_player_statistic |
A player's statistic changes | yes | 1.6 |
on_player_resource_pack |
A player's client reports a resource pack status | - | 1.6 |
on_player_swap_hands |
A player swaps the items of its hands | yes | 1.6 |
on_player_exp_change |
A player gains experience points from an orb (the amount can change) | - | 1.6 |
on_player_launch_projectile |
A player is about to throw or shoot a projectile from an item | yes | 1.6 |
on_player_fish |
A fishing rod action of a player | yes | 1.6 |
on_event(event::BEDROCK_FORM_RESPONSE, …) (kind 110, no own method) |
A Bedrock player answered or closed a form that the plugin sent with bedrock_send_form (Plugin API: players, entities and worlds). The event has the player, the form's id, whether it was closed and the answer as JSON text. It runs on the global owner in the next global phase |
- | 1.22 |
Entities
| Method | When it fires | Cancel | Since ABI |
|---|---|---|---|
on_entity_damage |
An entity is about to take damage (the amount can change) | yes | 1.0 |
on_entity_death |
A mob died | - | 1.4 |
on_entity_spawn |
An entity is about to be added to a world, with the reason (not for players and entities loaded from disk) | yes | 1.6 |
on_entity_drops |
An entity died and is about to drop loot and experience; both can change. Players' drops fire it too, with the attacker set | - | 1.6 |
on_entity_target |
A mob is about to change its target | yes | 1.6 |
on_entity_explode |
An explosion is about to happen (radius, flags and the list of blocks can change) | yes | 1.6 |
on_projectile_launch |
A projectile is launched | yes | 1.6 |
on_projectile_hit |
A projectile hits something | yes | 1.6 |
on_vehicle_enter |
An entity is about to mount a vehicle | yes | 1.6 |
on_vehicle_exit |
An entity is about to leave its vehicle | yes | 1.6 |
on_entity_tame |
An animal is about to be tamed | yes | 1.6 |
on_entity_breed |
Two animals are about to make a child | yes | 1.6 |
on_entity_portal |
An entity is about to travel through a portal (the destination, also its world, can change) | yes | 1.6 |
on_entity_transform |
An entity is about to turn into another (a villager hit by lightning) | yes | 1.6 |
on_entity_resurrect |
A totem of undying is about to save an entity | yes | 1.6 |
on_sheep_dye_wool |
A player is about to dye a sheep | yes | 1.6 |
on_entity_drop_item |
An entity drops an item on its own (a chicken's egg) | yes | 1.6 |
Blocks and world
| Method | When it fires | Cancel | Since ABI |
|---|---|---|---|
on_block_break |
A player is about to break a block | yes | 1.0 |
on_block_place |
A player is about to place a block | yes | 1.0 |
on_block_drop |
A broken block is about to drop items and experience (both can change) | yes | 1.6 |
on_block_grow |
A crop, stem, cactus, cane or sapling stage grows | yes | 1.6 |
on_block_form |
A block forms (snow, ice, concrete, obsidian, cobblestone) | yes | 1.6 |
on_block_spread |
A block spreads (grass, mycelium, vines, fire, sculk) | yes | 1.6 |
on_block_fade |
A block fades (ice and snow melt, coral dies, fire goes out, farmland dries) | yes | 1.6 |
on_leaves_decay |
Leaves decay | yes | 1.6 |
on_block_fluid_flow |
A fluid flows into a block | yes | 1.6 |
on_block_burn |
Fire destroys a block | yes | 1.6 |
on_block_ignite |
A block catches fire | yes | 1.6 |
on_block_fertilize |
A player uses bone meal | yes | 1.6 |
on_structure_grow |
A sapling or mushroom grows into a tree or huge mushroom | yes | 1.6 |
on_block_piston |
A piston extends or retracts (the text lists the blocks it moves and breaks) | yes | 1.6 |
on_sign_change |
A player changes a sign's text (the lines can change) | yes | 1.6 |
on_block_dispense |
A dispenser or dropper is about to dispense | yes | 1.6 |
on_chunk_load |
A chunk was loaded into memory (and whether it was generated now) | - | 1.6 |
on_chunk_unload |
A chunk is about to be unloaded | - | 1.6 |
on_world_save |
A world is about to be saved | - | 1.6 |
on_weather_change |
A world's weather is about to change | yes | 1.6 |
on_time_skip |
A world's time is about to skip ahead by sleeping (the ticks can change) | yes | 1.6 |
on_portal_create |
A nether portal is about to be created | yes | 1.6 |
on_world_load |
A world was loaded: an extra world of vanillite.toml or one a plugin created |
- | 1.8 |
on_world_unload |
An extra world is about to be unloaded | yes | 1.8 |
on_world_overload |
A world's overload regulator changed its level. The event has the new level (0 = no load shed), the old one, the smoothed tick time in milliseconds and a text of what the level sheds | - | 1.14 |
Inventories and crafting
| Method | When it fires | Cancel | Since ABI |
|---|---|---|---|
on_inventory_open |
A player's menu opens | yes | 1.6 |
on_inventory_click |
A player clicks in its open menu (the flag menu_flag::PLUGIN marks a plugin's own menu) |
yes | 1.6 |
on_inventory_drag |
A player drags its cursor stack over slots | yes | 1.6 |
on_inventory_close |
A player's menu closed | - | 1.6 |
on_craft_prepare |
A crafting grid's result is computed (the result can change) | - | 1.6 |
on_craft_item |
A player takes a crafting result | yes | 1.6 |
on_anvil_prepare |
An anvil's result is computed (result and level cost can change) | - | 1.6 |
on_furnace_smelt |
A furnace, smoker or blast furnace finishes an item (the result can change) | yes | 1.6 |
on_furnace_burn |
A furnace starts burning fuel (the burn time can change) | yes | 1.6 |
on_furnace_extract |
A player takes results out of a furnace (the experience can change) | - | 1.6 |
on_inventory_move_item |
A hopper or hopper minecart moves an item | yes | 1.6 |
on_enchant_item |
A player enchants an item at an enchanting table | yes | 1.6 |
on_brew |
A brewing stand finishes brewing (the results can change) | yes | 1.6 |
Events between plugins
A plugin can fire its own events for other plugins. Names are free text, such as economy:payment.
// In plugin A: listen (skips payments another plugin cancelled).
registrar.ignore_cancelled().on_plugin_event("economy:payment", priority::MONITOR, |_, ctx, event| {
let text = String::from_utf8_lossy(event.data()).into_owned();
// …
});
// In plugin B: fire it. Returns whether a handler cancelled it.
let cancelled = ctx.fire_event("economy:payment", b"alice paid 5")?;
The firing plugin's own handlers are not called. Event kinds 192 to 255 are free for such events. Plugins can also receive plugin messages that clients send on a channel (on_plugin_message("myplugin:sync", …)), and share function tables as services (Plugin API: slots and hosted plugins).
See also: Plugin API: commands and permissions, Plugin API: packets (for things no event covers).