The MinecraftServer class manages server-side game logic, including level management, player connections, tick processing, and world saving.
Constructor
Creates a new MinecraftServer instance with default settings.
Initialization
run
void run(__int64 seed, void *lpParameter)
Starts the server with the specified world seed.
Platform-specific parameters
main
static void main(__int64 seed, void *lpParameter)
Static entry point for starting the server.
Server Control
halt
Stops the server gracefully, saving all data.
HaltServer
static void HaltServer(bool bPrimaryPlayerSignedOut = false)
Static method to halt the server.
Whether to skip saving due to primary player sign-out
Suspend
Suspends the server (for console suspend/resume).
IsSuspending
Returns whether the server is currently suspending.
IsServerPaused
Returns whether the server is paused.
Tick System
tick
Processes one server tick (called 20 times per second). Handles:
- Level updates
- Entity updates
- Player updates
- Chunk loading/unloading
- Scheduled events
Level Management
getLevel
ServerLevel *getLevel(int dimension)
Returns the server level for a specific dimension.
Dimension ID:
0 - Overworld
-1 - Nether (Hell)
1 - The End
setLevel
void setLevel(int dimension, ServerLevel *level)
Sets the server level for a specific dimension.
The server level instance
Player Management
getPlayers
Returns the player list manager.
setPlayers
void setPlayers(PlayerList *players)
Sets the player list manager.
getPlayerList
static PlayerList *getPlayerList()
Static method to get the player list from the server instance.
Connection Management
getConnection
ServerConnection *getConnection()
Returns the server connection handler.
Saving
saveAllChunks
private:
void saveAllChunks()
Saves all loaded chunks across all dimensions.
saveGameRules
private:
void saveGameRules()
Saves the current game rules to disk.
setSaveOnExit
void setSaveOnExit(bool save)
Sets whether to save when the server exits.
broadcastStartSavingPacket
void broadcastStartSavingPacket()
Notifies all clients that saving has started.
broadcastStopSavingPacket
void broadcastStopSavingPacket()
Notifies all clients that saving has completed.
Game Settings
setMaxBuildHeight
void setMaxBuildHeight(int maxBuildHeight)
Sets the maximum build height for the world.
Maximum Y coordinate for building
getMaxBuildHeight
Returns the maximum build height.
isAnimals
Returns whether animal spawning is enabled.
setAnimals
void setAnimals(bool animals)
Enables or disables animal spawning.
isNpcsEnabled
Returns whether NPC (villager) spawning is enabled.
setNpcsEnabled
void setNpcsEnabled(bool npcs)
Enables or disables NPC spawning.
isPvpAllowed
Returns whether PvP is enabled.
setPvpAllowed
void setPvpAllowed(bool pvp)
Enables or disables player vs player combat.
isFlightAllowed
Returns whether flight is allowed in survival mode.
setFlightAllowed
void setFlightAllowed(bool allowFlight)
Enables or disables flight in survival mode.
isNetherEnabled
Returns whether the Nether dimension is enabled.
isHardcore
Returns whether the server is in hardcore mode.
Console Commands
void handleConsoleInput(const wstring& msg, ConsoleInputSource *source)
Processes a console command.
The source of the command
void handleConsoleInputs()
Processes all queued console commands.
getCommandDispatcher
CommandDispatcher *getCommandDispatcher()
Returns the command dispatcher.
getConsoleName
Returns the console name for command output.
Time Management
SetTimeOfDay
static void SetTimeOfDay(__int64 time)
Sets the time of day (0-24000).
Time in ticks (0 = dawn, 6000 = noon, 12000 = dusk, 18000 = midnight)
SetTime
static void SetTime(__int64 time)
Sets the total world time.
Total world time in ticks
Network Packet Queue Management
canSendOnSlowQueue
static bool canSendOnSlowQueue(INetworkPlayer *player)
Checks if a player can send packets on the slow queue.
The network player to check
cycleSlowQueueIndex
static void cycleSlowQueueIndex()
Cycles to the next player in the slow queue rotation.
Progress Updates
setProgress
private:
void setProgress(const wstring& status, int progress)
Updates the progress status during level loading.
Progress percentage (0-100)
endProgress
private:
void endProgress()
Ends the progress display.
Static Helpers
getInstance
static MinecraftServer *getInstance()
Returns the singleton server instance.
serverHalted
static bool serverHalted()
Returns whether the server has been halted.
saveOnExitAnswered
static bool saveOnExitAnswered()
Returns whether the save-on-exit question has been answered.
resetFlags
Resets server state flags.
Public Member Variables
Server network connection handler
Array of server levels (one per dimension)
Whether the server is running
Whether the server has stopped
Current progress status message
Whether online mode is enabled
Whether animal spawning is enabled
Whether NPC spawning is enabled
Whether flight is allowed
UGC (User Generated Content) players version counter
Currently loaded texture pack ID
Static Constants
static const wstring VERSION;
Server version string.
static const int TICK_STATS_SPAN = SharedConstants::TICKS_PER_SECOND * 5;
Tick statistics collection span (100 ticks).
static const int MS_PER_TICK = 1000 / SharedConstants::TICKS_PER_SECOND;
Milliseconds per tick (50ms for 20 TPS).
Example Usage
Starting a Server
// Create server instance
MinecraftServer *server = new MinecraftServer();
// Start server with seed
__int64 worldSeed = 12345678;
server->run(worldSeed, NULL);
Managing Game Settings
MinecraftServer *server = MinecraftServer::getInstance();
// Configure game rules
server->setPvpAllowed(true);
server->setAnimals(true);
server->setNpcsEnabled(true);
server->setFlightAllowed(false);
server->setMaxBuildHeight(256);
Working with Levels
MinecraftServer *server = MinecraftServer::getInstance();
// Get overworld
ServerLevel *overworld = server->getLevel(0);
// Get nether
ServerLevel *nether = server->getLevel(-1);
// Get The End
ServerLevel *theEnd = server->getLevel(1);
Time Management
// Set time to noon
MinecraftServer::SetTimeOfDay(6000);
// Set time to midnight
MinecraftServer::SetTimeOfDay(18000);
// Set time to dawn
MinecraftServer::SetTimeOfDay(0);
Saving
MinecraftServer *server = MinecraftServer::getInstance();
// Notify clients saving is starting
server->broadcastStartSavingPacket();
// Server saves automatically in tick()
// Notify clients saving is complete
server->broadcastStopSavingPacket();
Graceful Shutdown
MinecraftServer *server = MinecraftServer::getInstance();
// Enable saving on exit
server->setSaveOnExit(true);
// Halt the server
server->halt();
Thread Safety
The server uses a separate thread for post-processing chunks during level creation. Always use the provided synchronization primitives when accessing shared data.
The server runs at 20 ticks per second (TPS). Each tick must complete within 50ms to maintain performance. The slow queue system prevents packet flooding by rotating which player can send packets each tick.