Skip to main content
The MinecraftServer class manages server-side game logic, including level management, player connections, tick processing, and world saving.

Constructor

MinecraftServer()
Creates a new MinecraftServer instance with default settings.

Initialization

run

void run(__int64 seed, void *lpParameter)
Starts the server with the specified world seed.
seed
__int64
World generation seed
lpParameter
void*
Platform-specific parameters

main

static void main(__int64 seed, void *lpParameter)
Static entry point for starting the server.

Server Control

halt

void halt()
Stops the server gracefully, saving all data.

HaltServer

static void HaltServer(bool bPrimaryPlayerSignedOut = false)
Static method to halt the server.
bPrimaryPlayerSignedOut
bool
default:"false"
Whether to skip saving due to primary player sign-out

Suspend

void Suspend()
Suspends the server (for console suspend/resume).

IsSuspending

bool IsSuspending()
Returns whether the server is currently suspending.

IsServerPaused

bool IsServerPaused()
Returns whether the server is paused.

Tick System

tick

private:
void 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
int
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.
dimension
int
Dimension ID
level
ServerLevel*
The server level instance

Player Management

getPlayers

PlayerList *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.
save
bool
Whether to save on exit

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.
maxBuildHeight
int
Maximum Y coordinate for building

getMaxBuildHeight

int getMaxBuildHeight()
Returns the maximum build height.

isAnimals

bool isAnimals()
Returns whether animal spawning is enabled.

setAnimals

void setAnimals(bool animals)
Enables or disables animal spawning.

isNpcsEnabled

bool isNpcsEnabled()
Returns whether NPC (villager) spawning is enabled.

setNpcsEnabled

void setNpcsEnabled(bool npcs)
Enables or disables NPC spawning.

isPvpAllowed

bool isPvpAllowed()
Returns whether PvP is enabled.

setPvpAllowed

void setPvpAllowed(bool pvp)
Enables or disables player vs player combat.

isFlightAllowed

bool isFlightAllowed()
Returns whether flight is allowed in survival mode.

setFlightAllowed

void setFlightAllowed(bool allowFlight)
Enables or disables flight in survival mode.

isNetherEnabled

bool isNetherEnabled()
Returns whether the Nether dimension is enabled.

isHardcore

bool isHardcore()
Returns whether the server is in hardcore mode.

Console Commands

handleConsoleInput

void handleConsoleInput(const wstring& msg, ConsoleInputSource *source)
Processes a console command.
msg
const wstring&
The command message
source
ConsoleInputSource*
The source of the command

handleConsoleInputs

void handleConsoleInputs()
Processes all queued console commands.

getCommandDispatcher

CommandDispatcher *getCommandDispatcher()
Returns the command dispatcher.

getConsoleName

wstring getConsoleName()
Returns the console name for command output.

Time Management

SetTimeOfDay

static void SetTimeOfDay(__int64 time)
Sets the time of day (0-24000).
time
__int64
Time in ticks (0 = dawn, 6000 = noon, 12000 = dusk, 18000 = midnight)

SetTime

static void SetTime(__int64 time)
Sets the total world time.
time
__int64
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.
player
INetworkPlayer*
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.
status
const wstring&
Status message
progress
int
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

static void resetFlags()
Resets server state flags.

Public Member Variables

connection
ServerConnection*
Server network connection handler
settings
Settings*
Server settings
levels
ServerLevelArray
Array of server levels (one per dimension)
running
bool
Whether the server is running
stopped
bool
Whether the server has stopped
tickCount
int
Current tick counter
progressStatus
wstring
Current progress status message
progress
int
Current progress value
onlineMode
bool
Whether online mode is enabled
animals
bool
Whether animal spawning is enabled
npcs
bool
Whether NPC spawning is enabled
pvp
bool
Whether PvP is enabled
allowFlight
bool
Whether flight is allowed
motd
wstring
Message of the day
maxBuildHeight
int
Maximum build height
m_ugcPlayersVersion
DWORD
UGC (User Generated Content) players version counter
m_texturePackId
DWORD
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.

Performance Considerations

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.

Build docs developers (and LLMs) love