Skip to main content

Overview

The networking system manages all connected clients, handles player states (configuration and play), and provides utilities for sending packets.

ConnectionManager

Manages all connected clients and player lifecycle.

Player Queries

getOnlinePlayerCount
int
Gets the number of players in the play state (for query response)
getOnlinePlayers
Collection<Player>
Returns an unmodifiable collection of players currently in the play state
getConfigPlayers
Collection<Player>
Returns an unmodifiable collection of players currently in the configuration state
getPlayer
Player
Gets the Player linked to a PlayerConnectionReturns the player (regardless of state) or null
getOnlinePlayerByUsername
Player
Gets the first player in play state matching the username (case-insensitive)Returns the player or null if not found
getOnlinePlayerByUuid
Player
Gets the first player in play state matching the UUIDReturns the player or null if not found
findOnlinePlayer
Player
Finds the closest matching player in play state (fuzzy search)Uses Jaro-Winkler distance for matching. Returns null if no close match.

Configuration

setPlayerProvider
void
Changes the Player provider to use custom Player implementations

Player State Transitions

createPlayer
Player
Creates a Player instance for a connection
transitionLoginToConfig
GameProfile
Transitions a player from login to configuration stateFires AsyncPlayerPreLoginEvent and handles compression
doConfiguration
void
Performs the configuration phase for a playerSends registry data, resource packs, and fires AsyncPlayerConfigurationEvent
transitionPlayToConfig
void
Transitions a player from play to configuration state (for reconfiguration)
transitionConfigToPlay
void
Transitions a player from configuration to play state

Management

removePlayer
void
Removes a player from the connection managerUse PlayerConnection.disconnect() instead of calling this directly
shutdown
void
Shuts down the connection manager by kicking all connected players
tick
void
Ticks the connection manager (handles keep-alive, waiting players)

Registry & Tags

sendRegistryTags
void
Sends the tags packet to a player
invalidateTags
void
Invalidates the cached tags packet (called when registries change)

PlayerConnection

Represents a connection to a player. Implementations include PlayerSocketConnection for real network connections.

Packet Sending

sendPacket
void
Sends a packet to the player
sendPackets
void
Sends multiple packets

Connection Info

getPlayer
Player
Gets the Player associated with this connection (may be null before login)
getRemoteAddress
SocketAddress
Gets the remote socket address
isOnline
boolean
Checks if the connection is still active
disconnect
void
Disconnects the player
kick
void
Kicks the player with a reason

Player State Management

Players transition through different states:
  1. Login - Initial handshake and authentication
  2. Configuration - Receiving registry data, resource packs, brand info
  3. Play - Actively playing in the world
  4. Configuration (reconfiguration) - Can transition back for updates
ConnectionManager manager = MinecraftServer.getConnectionManager();

// Get all players in play state
Collection<Player> playing = manager.getOnlinePlayers();

// Get players being configured
Collection<Player> configuring = manager.getConfigPlayers();

// Total connected (including configuration)
int total = playing.size() + configuring.size();

Custom Player Provider

Create custom Player implementations:
public class CustomPlayer extends Player {
    private int coins = 0;
    
    public CustomPlayer(PlayerConnection connection, GameProfile gameProfile) {
        super(connection, gameProfile);
    }
    
    public int getCoins() { return coins; }
    public void addCoins(int amount) { this.coins += amount; }
}

// Register provider
MinecraftServer.getConnectionManager().setPlayerProvider((connection, profile) -> {
    return new CustomPlayer(connection, profile);
});

// Access custom methods
for (Player p : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
    if (p instanceof CustomPlayer custom) {
        System.out.println(custom.getCoins());
    }
}

Packet Utilities

Sending to Multiple Players

// Send to all online players
for (Player player : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
    player.sendPacket(new SomePacket());
}

// Send to players in instance
Instance instance = /* get instance */;
instance.sendPackets(new SomePacket());

// Send to nearby players
player.sendPacketToViewersAndSelf(new SomePacket());

Configuration Events

MinecraftServer.getGlobalEventHandler()
    .addListener(AsyncPlayerPreLoginEvent.class, event -> {
        // Check whitelist, bans, etc.
        if (banned.contains(event.getPlayer().getUsername())) {
            event.getPlayer().kick(Component.text("You are banned"));
        }
        
        // Modify game profile
        event.setGameProfile(new GameProfile(newUuid, newUsername));
    });

Keep-Alive System

The ConnectionManager automatically sends keep-alive packets and kicks players who don’t respond:
  • Keep-alive sent every ServerFlag.KEEP_ALIVE_DELAY milliseconds (default: 10000)
  • Players kicked after ServerFlag.KEEP_ALIVE_KICK milliseconds (default: 30000)
Player player = /* get player */;

// Check last keep-alive time
long lastKeepAlive = player.getLastKeepAlive();

// Check if player answered
boolean answered = player.didAnswerKeepAlive();
Players are tracked separately during configuration and play states. The ConnectionManager handles state transitions automatically based on packet flow.

Build docs developers (and LLMs) love