Events and commands
How a script reacts to game events and adds commands, with the details of clicks, items, fishing, projectiles and packets.
React to game events
Context::register_event_handler::<E, _>(handler, priority, blocking) registers a typed handler; register_event_handler_with_options adds Paper's full options (six priorities, ignore-cancelled) and a failure verdict. EventHandler<E>::handle receives the Server and the event's data and returns the data. Only a blocking handler's returned data is used. See Events and handler options.
Add a command
Build a Command, attach a CommandHandler with .execute(handler), add argument nodes with .then(node), then call Context::register_command(command, node). See Commands and suggestions.
Know the caller
CommandHandler::handle receives the CommandSender, the Server and the parsed ConsumedArgs and returns Result<i32, CommandError>. Use sender.as_player() before player-only work; the console and command blocks can run commands too.
Check before you act
- Check permissions, values and current state each time a command runs.
- Respect cancellation before you apply side effects.
- Keep suggestions and handlers short: a blocking handler holds up the action it decides.
- Your first script puts a command and a handler together.
Left clicks
For a Java player, PlayerInteractEvent fires for a left click at the same moments as on Paper:
left-click-blockfires once, when the player starts digging a block, not again while the dig goes on. Cancel it and no dig starts and the block is not hit. A player in adventure mode gets it from the arm swing at a block.left-click-airfires from the arm swing when nothing the click could act on is within reach: no block, and no entity within attack reach. An entity the player attacks fires no click.- Cancelling a Java player's left click in the air does not hide the swing from other players; cancel the animation event for that.
- A Bedrock player's left click fires the same event, with the block and its face. A swing for placing a block or using an item fires the animation event only.
Right clicks
A cancelled right click does nothing, for Java and Bedrock players alike: no door or chest opens, nothing is placed, thrown, eaten or used up, and armour stays in the hand. The player's hand and the block are put back as they were, so a login or protection script can rely on it.
A right click has two uses, the block's and the item's, and you can answer them apart. Set them with PlayerInteractEvent::set_use_interacted_block and set_use_item_in_hand (allow, deny or default) and read them with use_interacted_block() and use_item_in_hand().
- Deny only the block: a door stays shut, and the item use that follows goes on.
- Deny only the item: the door opens and the item is not used.
cancelleddenies both.
Eating and drinking
In a PlayerItemConsumeEvent handler:
PlayerItemConsumeEvent::item()reads the item being consumed.set_item(item)consumes another item instead.set_replacement(item)names what takes the hand afterwards: an empty stack leaves the hand empty,Nonegoes back to the default.
Buckets
- On a bucket event,
events::set_bucket_result(item)sets what the player gets in place of the vanilla result: the filled bucket, or what the hand holds after emptying one. The event types have it asset_result. PlayerBucketEntityEvent(a mob scooped into a bucket) fires after the filled bucket is made, as on Paper.hand()is the hand used,original_bucket()the stack in it before, andset_entity_bucket(item)replaces the filled bucket. Cancel it and the mob stays; the player's client is shown the hand and the mob again.
Fishing
- A fish that starts to approach the hook has the state
lured. Read it withPlayerFishEvent::state(); thestatefield of the event data showsInGroundfor it, so checkstate()first. Cancel it and the fish does not approach. PlayerFishEvent::caught()is the catch: the item entity that will fly to the player, whose stack you can read and replace, or the hooked entity being pulled.
Projectiles
In a ProjectileLaunchEvent handler:
ProjectileLaunchEvent::projectile()is what is launched: a snowball, an arrow, a wind charge, an ender pearl and so on.- When a player threw or shot it,
item()is a copy of the item used andhand()the hand that held it.
server.get_entity_by_id(id) finds any entity, players included, from the id an event carries. A projectile enters the world only after its launch event, so use projectile() inside that handler.
Leashes
PlayerUnleashEntityEventfires only when a player takes a leash off: untying a mob the player holds, taking a leash over from a mob, or snapping leashes with a firework. It fires before the leash is taken. Cancel it and the leash stays, no lead drops and the player's client is shown the leash again.- A leash that comes off for any other reason (holder or mob gone, too far, world change, a boat) fires only
EntityUnleashEvent. - A player's unleash also fires
EntityUnleashEvent, with the reasonPLAYER_UNLEASH.events::set_unleash_drops_lead(false)stops the lead from dropping. - Cancel
EntityUnleashEventand the mob stays leashed and no lead drops.
Packet hooks
context.packet_id(direction, name)gives the id of a packet by its name.context.set_packet_hook_filter_names(direction, names)narrows your packet hooks to the named packets. An unknown name is refused and the filter stays as it was, so a typo never opens the hook to every packet.- Packet ids repeat from one connection state to the next, so a packet of another state with the same number reaches your hook too.
- A Java player's
send_packetcan sendc-update-attributes, which changes an entity's attributes on the client, for example thescaleof a packet figure (0.0625 to 16 in vanilla). Each property'sidis the attribute's position in theattributeenum ofattributes. Each modifier has a key likemyplugin:bonusand an operation 0 (add), 1 (multiply base) or 2 (multiply total). An unknown id, another operation or an unreadable key fails the call. - A spectator's click reaches a script as
s-spectate-entitywith the clicked entity's UUID, or the nil UUID when the click names no entity. To filter for it, use the packet names-spectator-action.