Understand Packet Serialization with ProtoDef and YAML
masterbedrock-protocol uses ProtoDef to handle the serialization and deserialization of Minecraft packets. While the final JavaScript code is generated from JSON, the project maintains human-readable YAML files for easier maintenance.
proto.yml: Contains packet definitions (fields starting withpacket_).types.yml: Contains custom data type definitions.
YAML Syntax Rules
- Data Types: Uses standard types like
li32(little-endian 32-bit integer),lu32(little-endian 32-bit unsigned integer),lf32(little-endian 32-bit float), andbool. - Packet Metadata: Fields starting with
!are ignored by the parser but used for documentation or mapping.!id: Used by the parser to generate the packet map.!bound: Used for documentation (e.g.,clientorserver).
- Mapping/Enums: Use the
=>syntax to map integers to strings (e.g.,u8 => 0: value). - Switch Statements: Use the
?operator to create conditional logic based on previously read values. - Arrays: Use square brackets
[]. The syntax is[Type][LengthPrefixType].- Example:
Position[]varintreads an array ofPositionobjects where the length is aVarInt.
- Example:
- Anonymous Structures: Use
_as a field name to inline a data structure.
# Example of a custom type and a packet definition
Position:
x: li32
z: lu32
y: lf32
packet_player_position:
!id: 0x29
!bound: client
on_ground: bool
position: Position
movement_reason: u8 =>
0: player_jump
1: player_autojump
_: movement_reason ?
if player_jump or player_autojump:
original_position: Position
jump_tick: li64