Skip to main content

Overview

The packet system handles all network communication between clients and servers. Each packet type represents a specific message or event in the game protocol.

Base Packet Class

Location: Minecraft.World/Packet.h

Core Methods

All packets must implement these virtual methods:

Packet Registration

Registers a packet type with the protocol. Each packet has:
  • Unique ID number
  • Direction flags (client/server)
  • Factory function for creation

Packet I/O

Reading Packets

Reads a packet from the network stream.

Writing Packets

Writes a packet to the network stream.

Connection Packets

LoginPacket (ID: 1)

Establishes a player connection to the server.
Location: Minecraft.World/LoginPacket.h

Fields

Constructors

Client to Server:
Server to Client:

Example Usage

DisconnectPacket (ID: 255)

Notifies of connection termination.
Location: Minecraft.World/DisconnectPacket.h

Disconnect Reasons

Fields

Example Usage

PreLoginPacket

Initiates the connection handshake before login. Location: Minecraft.World/PreLoginPacket.h

Entity Packets

AddEntityPacket (ID: 23)

Spawns a non-living entity in the world.
Location: Minecraft.World/AddEntityPacket.h

Entity Types

Fields

Constructors

Example Usage

AddPlayerPacket

Adds a player entity to the world. Location: Minecraft.World/AddPlayerPacket.h

Fields

AddMobPacket

Spawns a mob (living entity) in the world. Location: Minecraft.World/AddMobPacket.h

RemoveEntitiesPacket

Removes one or more entities from the world. Location: Minecraft.World/RemoveEntitiesPacket.h

Fields

AddExperienceOrbPacket

Spawns an experience orb. Location: Minecraft.World/AddExperienceOrbPacket.h

Movement Packets

MovePlayerPacket (ID: 10-13)

Updates player position and rotation.
Location: Minecraft.World/MovePlayerPacket.h

Fields

Variants

PosRot (ID: 13) - Position and rotation
Pos (ID: 11) - Position only
Rot (ID: 12) - Rotation only

Example Usage

TeleportEntityPacket

Instantly moves an entity to a new position. Location: Minecraft.World/TeleportEntityPacket.h

MoveEntityPacket

Applies relative movement to an entity. Location: Minecraft.World/MoveEntityPacket.h

SetEntityMotionPacket

Sets an entity’s velocity. Location: Minecraft.World/SetEntityMotionPacket.h

World Packets

TileUpdatePacket

Updates a single block in the world. Location: Minecraft.World/TileUpdatePacket.h

Fields

ChunkTilesUpdatePacket

Updates multiple blocks within a chunk. Location: Minecraft.World/ChunkTilesUpdatePacket.h

Fields

BlockRegionUpdatePacket

Updates a rectangular region of blocks. Location: Minecraft.World/BlockRegionUpdatePacket.h

ChunkVisibilityPacket

Controls which chunks are loaded on the client. Location: Minecraft.World/ChunkVisibilityPacket.h

Fields

ChunkVisibilityAreaPacket

Sets visibility for a rectangular area of chunks. Location: Minecraft.World/ChunkVisibilityAreaPacket.h

Gameplay Packets

ChatPacket (ID: 3)

Sends chat messages and game notifications.
Location: Minecraft.World/ChatPacket.h

Message Types

Fields

Example Usage

SetHealthPacket

Updates player health and food. Location: Minecraft.World/SetHealthPacket.h

SetExperiencePacket

Updates player experience and level. Location: Minecraft.World/SetExperiencePacket.h

RespawnPacket

Respawns the player after death. Location: Minecraft.World/RespawnPacket.h

SetSpawnPositionPacket

Sets the world spawn point. Location: Minecraft.World/SetSpawnPositionPacket.h

SetTimePacket

Synchronizes world time. Location: Minecraft.World/SetTimePacket.h

Inventory Packets

ContainerSetContentPacket

Sets all slots in a container. Location: Minecraft.World/ContainerSetContentPacket.h

ContainerSetSlotPacket

Updates a single inventory slot. Location: Minecraft.World/ContainerSetSlotPacket.h

ContainerClickPacket

Handles inventory click interactions. Location: Minecraft.World/ContainerClickPacket.h

SetCarriedItemPacket

Changes the held hotbar slot. Location: Minecraft.World/SetCarriedItemPacket.h

Packet Serialization

Writing Data

Reading Data

Utility Methods

Creating Custom Packets

Step 1: Define Packet Class

Step 2: Register Packet

Step 3: Add Handler

Packet Optimization

Invalidation

Some packets can be replaced by newer versions:

Async Processing

Packet Statistics

The packet system tracks bandwidth usage:
Enable with PACKET_ENABLE_STAT_TRACKING define.

Best Practices

  1. Always validate packet data - Check bounds and validity
  2. Keep packets small - Minimize bandwidth usage
  3. Use batch packets when possible (ChunkTilesUpdate vs individual TileUpdate)
  4. Handle missing entities gracefully - Entity may have despawned
  5. Version your protocol - Use clientVersion field for compatibility
  6. Test packet order - Network may reorder packets
  7. Implement getEstimatedSize() accurately for bandwidth tracking

See Also