Skip to main content

Overview

PacketManager handles the registration and processing of packets in the Ember Client Server API. It maintains a bidirectional mapping between packet IDs and packet classes, and provides methods for serializing and deserializing packets.

Class Definition

public class PacketManager

Packet Registration

The PacketManager automatically registers all built-in packets with their corresponding IDs:

Outgoing Packets (Server to Client)

OutAttestationRegister
Class<?>
ID: 1 - Requests the client to register an attestation key
OutAttestationSign
Class<?>
ID: 2 - Requests the client to sign data with their attestation key

Incoming Packets (Client to Server)

InAttestationRegister
Class<?>
ID: 1001 - Response from client with attestation registration result
InAttestationSign
Class<?>
ID: 1002 - Response from client with signed attestation data

Methods

getData()

Serializes a packet into a ByteBufWrapper for transmission.
public ByteBufWrapper getData(Packet packet)
packet
Packet
required
The packet to serialize
return
ByteBufWrapper
A ByteBufWrapper containing the serialized packet with its ID
Example:
PacketManager packetManager = ECServerAPI.getInstance().getPacketManager();
OutAttestationRegister packet = new OutAttestationRegister();
ByteBufWrapper data = packetManager.getData(packet);
// ByteBufWrapper contains: [packet ID (varint)] + [packet data]
This method is primarily used internally by ECServerAPI.sendPacket(). You typically don’t need to call it directly.

handle()

Deserializes and processes an incoming packet from a player.
public void handle(Player player, ByteBufWrapper buf)
player
Player
required
The player who sent the packet
buf
ByteBufWrapper
required
The buffer containing the packet data
Processing Flow:
  1. Reads the packet ID from the buffer
  2. Looks up the corresponding packet class
  3. Instantiates the packet
  4. Calls packet.read(buf) to deserialize data
  5. Calls packet.handle(player) to process the packet
Example:
// This is typically called internally by the MessageListener
PacketManager packetManager = ECServerAPI.getInstance().getPacketManager();
ByteBufWrapper buf = // ... received data
packetManager.handle(player, buf);
This method is used internally by the API’s message listener. Manual usage is not recommended unless you’re implementing custom packet channels.

Packet ID Mapping

The PacketManager uses a bidirectional map (BiMap) to maintain packet ID associations:
private final BiMap<Integer, Class<? extends Packet>> packets = HashBiMap.create();

ID Convention

  • Outgoing packets (Server → Client): IDs 1-999
  • Incoming packets (Client → Server): IDs 1000+
This convention helps distinguish packet direction at a glance.

Internal Structure

Constructor

public PacketManager() {
    this.packets.put(1, OutAttestationRegister.class);
    this.packets.put(1001, InAttestationRegister.class);
    this.packets.put(2, OutAttestationSign.class);
    this.packets.put(1002, InAttestationSign.class);
}

getPacketId()

Retrieves the packet ID for a given packet instance (private method).
private int getPacketId(Packet packet)
Uses the inverse BiMap to look up the ID from the packet’s class.

Error Handling

  • If an unknown packet ID is received, it is silently ignored
  • If packet instantiation fails, the exception is printed to console
  • Invalid packet data may throw exceptions during read() operations

Example: Custom Packet Handling

import com.emberclient.serverapi.ECServerAPI;
import com.emberclient.serverapi.packet.Packet;
import com.emberclient.serverapi.packet.PacketManager;
import com.emberclient.serverapi.ByteBufWrapper;
import org.bukkit.entity.Player;

public class PacketExample {
    
    public void sendCustomPacket(Player player, Packet packet) {
        ECServerAPI api = ECServerAPI.getInstance();
        PacketManager manager = api.getPacketManager();
        
        // Serialize the packet
        ByteBufWrapper data = manager.getData(packet);
        
        // Data is ready for transmission
        // (typically handled automatically by ECServerAPI.sendPacket)
    }
}

Build docs developers (and LLMs) love