Packet handling

The event of receiving or sending a packet.

This event allows you to trigger code when a packet is received (Client -> Server) or sent (Server -> Client).

on packet:
on packet %packet name%:


The event can be canceled. In this case, the packet will not be received or sent (respectively).

This event has built-in expressions.

  • event-packet - to receive the packet
  • event-player - to get a player

event-packet can be replaced with set event-packet to %packet%, provided that no execution delay was used.

on packet PacketPlayOutOpenSignEditor:
  broadcast "%event-player%"
  broadcast "%event-packet%"
  cancel event

In this example, the player’s username and the packet name are displayed in the chat. And then cancel sending this event to the player.
The packet itself forces the player to open the text editing window of the plate.

Getting a buffer from a packet

After receiving the buffer from the packet, we can read the fields indicated on The Minecraft Wiki

buffer (of|from) %packet%
%packet%'s buffer

Reading the buffer

To get values from the buffer, the following expressions exist, similar to writing to the buffer

read bool[ean] from %bytebuf%
read uuid from %bytebuf%
read string from %bytebuf%
read position from %bytebuf%
read [unsigned] byte from %bytebuf%
read [unsigned] short from %bytebuf%
read float from %bytebuf%
read double from %bytebuf%
read int[eger] from %bytebuf%
read long from %bytebuf%
read angle from %bytebuf%
read var[iable][ ]int[eger] from %bytebuf%
read var[iable][ ]long from %bytebuf%
read utf[(-| )]8 [with [len[gth]]] %number% from %bytebuf%

To read utf-8 you must also specify the length of the text in bytes

Reading occurs in the same order as writing a packet, provided that the packet was not created by you.
If you created a packet and want to read it, then move its reader index to position zero.

In this tutorial, we will use the Open Sign Editor packet. Let’s see how we can read and use it.

Packet IDStateBound ToField NameField TypeNotes
0x32PlayClientLocationPosition
Is Front TextBooleanWhether the opened editor is for the front or on the back of the sign

Now, let’s use the information from this table to create the packet handling code.

First, let’s find the %packet name%.
To get the packet name, we can use the packet name finder ( See Packet Creation )

Second, we need to get the buffer from the packet.
To get the buffer, we can use the expression buffer (of|from) %packet% ( See Getting a buffer from a packet )

Third, let’s read the fields from the buffer.
To read a field, we can use the expression read %field type% from %buffer% ( See Reading the buffer )

Fourth, let’s use the information from the packet.
In this example, we will display the coordinates of the opened sign in the chat.

on packet PacketPlayOutOpenSignEditor:
  set {_buffer} to buffer from event-packet
  set {_position} to read position from {_buffer}
  set {_isFrontText} to read boolean from {_buffer}
  broadcast "%{_position}%"
  cancel event

And that’s it!
We have successfully read the packet and used it.

Tutorial written by @GuavaDealer

Reader index

Each reading of the buffer shifts its Reader index, this is the number of bytes read inside the buffer.
Reader index you can find out or change it using the following expression:

reader index of %bytebuf%
%bytebuf%'s reader index

Disabling or enabling packet handling

You can enable or disable handling of incoming or outgoing packets per player using the following expression.

[(handl(e|ing))|(listen[ing] [of])] (in|out)coming packets of %player%
%player%'s [(handl(e|ing))|(listen[ing] [of])] (in|out)coming packets
on join:
  set handling of incoming packets of player to false