Plugin API: packets
Packets are the raw messages between server and client. Use the packet interface for what no event covers: hide name tags, make signs read-only, add a prefix to every system message, add custom effects. Since ABI 1.7; feeding packets in since 1.8.
Examples with working code: packet-plugin and packet-bench, see Your first plugin.
Listen
use vanillite_plugin::packet::PacketFilter;
// By name, without any packet types: cancel the client's sign edit.
registrar.on_packet(PacketFilter::serverbound("sign_update"), |event| event.cancel());
on_packet (on Registrar, or on Server at any time and from any thread) takes a PacketFilter and a listener. It returns a PacketListener handle (Server version) that server.remove_packet_listener(handle) removes again.
| Filter | Gets |
|---|---|
PacketFilter::clientbound("set_entity_data") |
Play packets to clients, by Vanilla name |
PacketFilter::serverbound("sign_update") |
Play packets from clients |
PacketFilter::of::<P>() |
The packet type P of vanillite-protocol (feature packets) |
all_clientbound(), all_serverbound() |
Every packet. Puts a call on every packet of every player: name the packets instead. |
Modifiers: .state(State::…) for another protocol state than play (Handshake, Status, Login, Configuration), .priority(…) (same scale as events, monitors only observe), .ignore_cancelled(), .per_broadcast() (see below). A name that does not exist gives Error::NotFound.
What a listener can do
event is a PacketEvent:
| Method | What |
|---|---|
direction(), state(), id(), name() |
Which packet |
player() |
The receiver or sender, if known (none for broadcast calls) |
body(), reader() |
The raw bytes (the packet id first, then the fields); reader() has varint, string, uuid, bool and more |
protocol() |
The protocol version the body is encoded for |
cancel(), set_cancelled(bool), is_cancelled() |
Drop the packet |
replace(bytes) |
Replace the body with another one |
decode::<P>(), replace_with(&packet) |
The same with typed packets (feature packets) |
With the feature on, server.on_packet_of::<SystemChat>(filter, |event, packet| { … }) calls you with the decoded packet:
vanillite-plugin = { git = "…", tag = "…", features = ["packets"] }
The connection's address (ABI 1.16). The raw event (abi::PacketEvent) also has an address field: the remote address of the connection, as text. It is there in every protocol state, also before login (a server list ping, for example). Behind a proxy with forwarding (Velocity), it is the player's forwarded address after the login, as on Paper. The text is made only when a listener runs. The Rust SDK has no address() method on PacketEvent yet.
Per receiver and per broadcast
Vanillite encodes a packet once and sends the same bytes to all players who get it (see Chunks and networking). Listeners keep that:
- A listener per receiver (the default) is called for each receiver. Reading and cancelling copy nothing.
replacegives only that one receiver its own copy. - A listener per broadcast (
.per_broadcast(), clientbound only) must not depend on the receiver. It runs once per encoded packet, with no player. Cancelling holds for all receivers, and a replacement is again one buffer shared by all. Broadcast listeners run before the others.
A packet type nobody listens to costs nothing: one bit test where packets are written and read. A bundle of packets is only searched when a listener wants one of its types.
Send and feed in
| Method | What |
|---|---|
server.send(player, &packet) / send_packet(player, bytes) |
Sends a play packet to a player, in order with what the server sends. Listeners see it. |
send_packet_unlistened |
The same, past all listeners. |
server.receive(player, &packet) / receive_packet(player, bytes) |
Feeds in a serverbound packet as if the player's client had sent it (ProtocolLib's receiveClientPacket). It is validated, counts against the rate limits and is handled in order with the client's own packets. |
receive_packet_unlistened |
The same, past all listeners. |
packet_id(state, direction, name), packet_name(…), protocol_version() |
Look up ids and names. |
A body may be up to 8 MiB. The example /punch does ctx.server().receive(player, &Punch).
Threads and limits
- Listeners run where packets are written and read: on network threads for many connections at once, and per broadcast also on the thread that encodes the packet. They do not own the player or its world, and get no plugin object and no
Ctx. Keep your state in atomics or locks, and to act on the world schedule a task on the player's owner (Plugin API: tasks, config and data). - A listener is
Fn + Send + Sync. Declarethreading = "concurrent"inplugin.toml; withserializedevery packet call waits for the plugin's other handlers. - The packets of one connection arrive in order.
- Bodies follow the Minecraft version of the server. A plugin that reads fields is tied to that version. Names change rarely, so subscribe by name. Clients of other versions and editions are translated behind the listeners.
- The server does not check what you write into a clientbound packet: a body the client cannot read disconnects that client.
- A replaced serverbound packet is validated like any packet of the client, and a cancelled one still counts against the rate limits.
- Cancelling packets the protocol waits for (keep-alives, teleport confirmations, acknowledgements in login and configuration) ends that connection after its timeout. The server itself is not affected.
Plugins from other languages use the ABI functions packet_subscribe, packet_send and packet_receive. The Bukkit layer implements ProtocolLib's API on top of them: Bukkit layer for developers.
See also: Security by design (limits and checks that apply to packets).