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
Minecraft.World/Packet.h
Core Methods
All packets must implement these virtual methods:Packet Registration
- Unique ID number
- Direction flags (client/server)
- Factory function for creation
Packet I/O
Reading Packets
Writing Packets
Connection Packets
LoginPacket (ID: 1)
Establishes a player connection to the server.Minecraft.World/LoginPacket.h
Fields
Constructors
Client to Server:Example Usage
DisconnectPacket (ID: 255)
Notifies of connection termination.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.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.Minecraft.World/MovePlayerPacket.h
Fields
Variants
PosRot (ID: 13) - Position and rotationExample 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.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:PACKET_ENABLE_STAT_TRACKING define.
Best Practices
- Always validate packet data - Check bounds and validity
- Keep packets small - Minimize bandwidth usage
- Use batch packets when possible (ChunkTilesUpdate vs individual TileUpdate)
- Handle missing entities gracefully - Entity may have despawned
- Version your protocol - Use clientVersion field for compatibility
- Test packet order - Network may reorder packets
- Implement getEstimatedSize() accurately for bandwidth tracking
See Also
- ClientConnection API - Client packet handling
- ServerConnection API - Server packet handling
- PlayerConnection - Per-player packet processing