Skip to main content

Overview

The ClientConnection 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

Location: Minecraft.Client/ClientConnection.h

Constructor

Parameters

  • minecraft - Pointer to the main Minecraft instance
  • socket - 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()

Processes incoming packets and flushes outgoing data. Call this every game tick to maintain the connection. Example:

close()

Cleanly closes the connection and releases resources.

isStarted()

Returns true if the connection has completed initialization and gameplay has started.

isClosed()

Returns true if the connection has been closed or disconnected.

Sending Packets

send()

Sends a packet to the server. Example:

sendAndDisconnect()

Sends a packet and then gracefully disconnects from the server.

Handling Disconnects

handleDisconnect()

Processes a disconnect packet received from the server. See DisconnectPacket.h:10 for disconnect reasons.

onDisconnect()

Called when the connection is lost or terminated. Common Disconnect Reasons:
  • eDisconnect_Quitting - Player chose to quit
  • eDisconnect_Closed - Connection closed normally
  • eDisconnect_TimeOut - Connection timed out
  • eDisconnect_ServerFull - Server has reached max players
  • eDisconnect_OutdatedClient - Client version is incompatible
  • eDisconnect_Kicked - Player was kicked by server
Example:

Packet Handlers

The ClientConnection class implements handlers for all client-received packets. Key handlers include:

Login and Setup

handleLogin()

Processes the login response from the server, initializes the level, and sets up the local player.
  • Sets player entity ID and dimension
  • Creates the multiplayer level
  • Initializes game mode
  • Configures player privileges and settings
See ClientConnection.cpp:158 for full implementation.

Entity Management

handleAddEntity()

Spawns a new entity in the world (boats, minecarts, projectiles, etc.). See ClientConnection.cpp:411.

handleAddPlayer()

Adds a remote player to the world. See ClientConnection.cpp:726.

handleRemoveEntity()

Removes entities from the world when they’re destroyed.

Movement and Animation

handleTeleportEntity()

Teleports an entity to a new position with rotation.

handleMoveEntity()

Updates entity position with relative movement.

handleMovePlayer()

Processes server-side player position corrections. See ClientConnection.cpp:961.

World Updates

handleChunkVisibility()

Updates chunk visibility state for level of detail management.

handleTileUpdate()

Updates a single block in the world. See ClientConnection.cpp:1137.

handleChunkTilesUpdate()

Updates multiple blocks in a chunk efficiently.

Gameplay Events

handleChat()

Processes chat messages and localized game events. See ClientConnection.cpp:1328.

handleSetHealth()

Updates local player health from server.

handleRespawn()

Handles player respawn after death.

Properties

Connection State

Game State

Network Player

getNetworkPlayer()

Returns the network player interface for this connection, or NULL if unavailable.

getSocket()

Returns the underlying socket for platform-specific network operations.

Best Practices

  1. Always check createdOk after construction before using the connection
  2. Call tick() every frame while the connection is active
  3. Handle disconnects gracefully to provide good user experience
  4. Don’t send packets before login completes - wait for started to be true
  5. Clean up resources by calling close() when done

Thread Safety

The ClientConnection 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