Skip to main content

Overview

The entity system manages all dynamic objects in the world including players, mobs, items, projectiles, and vehicles. Entities are updated every tick and synchronized between client and server.

Entity Base Class

All entities derive from the Entity base class:
Location: Entity.cpp:328

Entity ID System

The game uses a dual ID system: Small IDs (0-2047):
  • Used for networked entities
  • Efficient 11-bit network encoding
  • Allocated from a shared pool
  • Must be explicitly freed
Large IDs (2048+):
  • Used for client-side only entities (particles)
  • Simple incrementing counter
  • No network synchronization
Location: Entity.cpp:37-98

Entity Hierarchy

Core Types

Mob System

Mob Base Class

Location: Mob.cpp:123

AI Components

LookControl - Manages head rotation:
MoveControl - Handles movement:
PathNavigation - Pathfinding system:
Location: Mob.cpp:166-188

Player System

Player Class

Location: Player.cpp:124

Player Types

ServerPlayer - Authoritative server instance:
  • Processes player input
  • Applies game rules
  • Broadcasts actions to clients
  • Handles inventory management
LocalPlayer - Client-side representation:
  • Captures input
  • Predicts movement
  • Renders player model
  • Receives corrections from server
MultiplayerLocalPlayer - Split-screen local player:
  • Extends LocalPlayer
  • Manages viewport
  • Handles local input
  • Connects to local server

Entity Lifecycle

Creation

Addition to World

Location: Level.cpp:1659

Update Cycle

Location: Entity.cpp:475-440

Removal

Location: Level.cpp:1736

Entity Updates

Entities are updated in several phases:

Phase 1: Base Tick

Location: Entity.cpp:480

Phase 2: AI Tick (Mobs)

Phase 3: Physics Tick

Entity Tracking

The server tracks which entities each player can see:
Tracking Logic:
  1. Determine range based on entity type
  2. Find players within range
  3. Send spawn packet if newly visible
  4. Send movement/data updates
  5. Send destroy packet if out of range
Location: EntityTracker.cpp (referenced in MinecraftServer.cpp)

Entity Synchronization

Synched Entity Data

Entities use a data watcher system for network sync:
Common Data Slots:
  • DATA_SHARED_FLAGS_ID = 0 - On fire, crouching, sprinting
  • DATA_AIR_SUPPLY_ID = 1 - Drowning bubbles
  • DATA_CUSTOM_NAME_ID = 2 - Name tag (not in this version)
Location: Referenced in Entity.cpp:338

Movement Synchronization

Absolute Position:
Relative Movement:

Entity Collections

The level maintains multiple entity collections:
Thread Safety:
Location: Level.cpp:522-523

Entity Types

Entity type system for network spawning:
Factory Method:

Riding System

Entities can ride other entities:
Updates:
Location: Referenced in Entity.cpp:485

Key Takeaways

  1. Dual ID System: Efficient network IDs for synchronized entities
  2. Component-Based AI: Separate controls for looking, moving, jumping
  3. Entity Hierarchy: Shared base with specialized derived classes
  4. Lifecycle Management: Create → Add → Tick → Remove
  5. Network Sync: Data watchers and delta movement encoding
  6. Thread Safety: Critical sections protect entity collections