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 theEntity base class:
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
- Used for client-side only entities (particles)
- Simple incrementing counter
- No network synchronization
Entity.cpp:37-98
Entity Hierarchy
Core Types
Mob System
Mob Base Class
Mob.cpp:123
AI Components
LookControl - Manages head rotation:Mob.cpp:166-188
Player System
Player Class
Player.cpp:124
Player Types
ServerPlayer - Authoritative server instance:- Processes player input
- Applies game rules
- Broadcasts actions to clients
- Handles inventory management
- Captures input
- Predicts movement
- Renders player model
- Receives corrections from server
- Extends LocalPlayer
- Manages viewport
- Handles local input
- Connects to local server
Entity Lifecycle
Creation
Addition to World
Level.cpp:1659
Update Cycle
Entity.cpp:475-440
Removal
Level.cpp:1736
Entity Updates
Entities are updated in several phases:Phase 1: Base Tick
Entity.cpp:480
Phase 2: AI Tick (Mobs)
Phase 3: Physics Tick
Entity Tracking
The server tracks which entities each player can see:- Determine range based on entity type
- Find players within range
- Send spawn packet if newly visible
- Send movement/data updates
- Send destroy packet if out of range
EntityTracker.cpp (referenced in MinecraftServer.cpp)
Entity Synchronization
Synched Entity Data
Entities use a data watcher system for network sync:DATA_SHARED_FLAGS_ID = 0- On fire, crouching, sprintingDATA_AIR_SUPPLY_ID = 1- Drowning bubblesDATA_CUSTOM_NAME_ID = 2- Name tag (not in this version)
Entity.cpp:338
Movement Synchronization
Absolute Position:Entity Collections
The level maintains multiple entity collections:Level.cpp:522-523
Entity Types
Entity type system for network spawning:Riding System
Entities can ride other entities:Entity.cpp:485
Related Systems
- Game Loop - Entity updates every tick
- World Management - Entities stored in chunks
- Multiplayer - Entity network synchronization
Key Takeaways
- Dual ID System: Efficient network IDs for synchronized entities
- Component-Based AI: Separate controls for looking, moving, jumping
- Entity Hierarchy: Shared base with specialized derived classes
- Lifecycle Management: Create → Add → Tick → Remove
- Network Sync: Data watchers and delta movement encoding
- Thread Safety: Critical sections protect entity collections