Overview
TheClientConnection class manages the client-side network connection to a Minecraft server. It handles packet processing, level synchronization, and player state management for multiplayer gameplay.
Class Definition
Minecraft.Client/ClientConnection.h
Constructor
Parameters
minecraft- Pointer to the main Minecraft instancesocket- Network socket for communication (NULL for local connection)iUserIndex- User index for the connection (defaults to primary pad if -1)
Connection Lifecycle
Connection States
The client connection progresses through several states during initialization:State Management Methods
tick()
close()
isStarted()
true if the connection has completed initialization and gameplay has started.
isClosed()
true if the connection has been closed or disconnected.
Sending Packets
send()
sendAndDisconnect()
Handling Disconnects
handleDisconnect()
DisconnectPacket.h:10 for disconnect reasons.
onDisconnect()
eDisconnect_Quitting- Player chose to quiteDisconnect_Closed- Connection closed normallyeDisconnect_TimeOut- Connection timed outeDisconnect_ServerFull- Server has reached max playerseDisconnect_OutdatedClient- Client version is incompatibleeDisconnect_Kicked- Player was kicked by server
Packet Handlers
TheClientConnection class implements handlers for all client-received packets. Key handlers include:
Login and Setup
handleLogin()
- Sets player entity ID and dimension
- Creates the multiplayer level
- Initializes game mode
- Configures player privileges and settings
ClientConnection.cpp:158 for full implementation.
Entity Management
handleAddEntity()
ClientConnection.cpp:411.
handleAddPlayer()
ClientConnection.cpp:726.
handleRemoveEntity()
Movement and Animation
handleTeleportEntity()
handleMoveEntity()
handleMovePlayer()
ClientConnection.cpp:961.
World Updates
handleChunkVisibility()
handleTileUpdate()
ClientConnection.cpp:1137.
handleChunkTilesUpdate()
Gameplay Events
handleChat()
ClientConnection.cpp:1328.
handleSetHealth()
handleRespawn()
Properties
Connection State
Game State
Network Player
getNetworkPlayer()
NULL if unavailable.
getSocket()
Best Practices
- Always check
createdOkafter construction before using the connection - Call
tick()every frame while the connection is active - Handle disconnects gracefully to provide good user experience
- Don’t send packets before login completes - wait for
startedto betrue - Clean up resources by calling
close()when done
Thread Safety
TheClientConnection class processes packets on the network thread but updates game state on the main thread. The tick() method should only be called from the main game thread.
See Also
- ServerConnection API - Server-side connection management
- Packet System - Network packet reference
- PlayerConnection - Server-side player connection handling