Class PacketRegistry

java.lang.Object
org.geysermc.mcprotocollib.network.packet.PacketRegistry

public class PacketRegistry extends Object
A protocol for packet sending and receiving. All implementations must have a constructor that takes in a ByteBuf.
  • Constructor Details

    • PacketRegistry

      public PacketRegistry()
  • Method Details

    • clearPackets

      public final void clearPackets()
      Clears all currently registered packets.
    • register

      public final <T extends Packet, H extends PacketCodecHelper> void register(int id, Class<T> packet, PacketSerializer<T,H> serializer)
      Registers a packet to this protocol as both serverbound and clientbound.
      Parameters:
      id - Id to register the packet to.
      packet - Packet to register.
      serializer - The packet serializer.
      Throws:
      IllegalArgumentException - If the packet fails a test creation when being registered as serverbound.
    • register

      public final void register(PacketDefinition<? extends Packet,?> definition)
      Registers a packet to this protocol as both serverbound and clientbound.
      Parameters:
      definition - The packet definition.
      Throws:
      IllegalArgumentException - If the packet fails a test creation when being registered as serverbound.
    • registerServerbound

      public final <T extends Packet, H extends PacketCodecHelper> void registerServerbound(int id, Class<T> packet, PacketSerializer<T,H> serializer)
      Registers a serverbound packet to this protocol.
      Parameters:
      id - Id to register the packet to.
      packet - Packet to register.
      serializer - The packet serializer.
      Throws:
      IllegalArgumentException - If the packet fails a test creation.
    • registerServerbound

      public final void registerServerbound(PacketDefinition<? extends Packet,?> definition)
      Registers a serverbound packet to this protocol.
      Parameters:
      definition - The packet definition.
    • registerClientbound

      public final <T extends Packet, H extends PacketCodecHelper> void registerClientbound(int id, Class<T> packet, PacketSerializer<T,H> serializer)
      Registers a clientbound packet to this protocol.
      Parameters:
      id - Id to register the packet to.
      packet - Packet to register.
      serializer - The packet serializer.
    • registerClientbound

      public final void registerClientbound(PacketDefinition<? extends Packet,?> definition)
      Registers a clientbound packet to this protocol.
      Parameters:
      definition - The packet definition.
    • createClientboundPacket

      public <H extends PacketCodecHelper> Packet createClientboundPacket(int id, io.netty.buffer.ByteBuf buf, H codecHelper)
      Creates a new instance of a clientbound packet with the given id and read the clientbound input.
      Parameters:
      id - Id of the packet to create.
      buf - The buffer to read the packet from.
      codecHelper - The codec helper.
      Returns:
      The created packet.
      Throws:
      IllegalArgumentException - If the packet ID is not registered.
    • getClientboundId

      public int getClientboundId(Class<? extends Packet> packetClass)
      Gets the registered id of a clientbound packet class.
      Parameters:
      packetClass - Class of the packet to get the id for.
      Returns:
      The packet's registered id.
      Throws:
      IllegalArgumentException - If the packet is not registered.
    • getClientboundId

      public int getClientboundId(Packet packet)
      Gets the registered id of a clientbound Packet instance.
      Parameters:
      packet - Instance of Packet to get the id for.
      Returns:
      The packet's registered id.
      Throws:
      IllegalArgumentException - If the packet is not registered.
    • getClientboundClass

      public Class<? extends Packet> getClientboundClass(int id)
      Gets the packet class for a packet id.
      Parameters:
      id - The packet id.
      Returns:
      The registered packet's class
      Throws:
      IllegalArgumentException - If the packet ID is not registered.
    • createServerboundPacket

      public <H extends PacketCodecHelper> Packet createServerboundPacket(int id, io.netty.buffer.ByteBuf buf, H codecHelper)
      Creates a new instance of a serverbound packet with the given id and read the serverbound input.
      Parameters:
      id - Id of the packet to create.
      buf - The buffer to read the packet from.
      codecHelper - The codec helper.
      Returns:
      The created packet.
      Throws:
      IllegalArgumentException - If the packet ID is not registered.
    • getServerboundId

      public int getServerboundId(Class<? extends Packet> packetClass)
      Gets the registered id of a serverbound packet class.
      Parameters:
      packetClass - Class of the packet to get the id for.
      Returns:
      The packet's registered id.
      Throws:
      IllegalArgumentException - If the packet is not registered.
    • getServerboundId

      public int getServerboundId(Packet packet)
      Gets the registered id of a serverbound Packet instance.
      Parameters:
      packet - Instance of Packet to get the id for.
      Returns:
      The packet's registered id.
      Throws:
      IllegalArgumentException - If the packet is not registered.
    • getServerboundClass

      public Class<? extends Packet> getServerboundClass(int id)
      Gets the packet class for a packet id.
      Parameters:
      id - The packet id.
      Returns:
      The registered packet's class
      Throws:
      IllegalArgumentException - If the packet ID is not registered.
    • getServerboundDefinition

      public PacketDefinition<?,?> getServerboundDefinition(int id)
      Gets the serverbound packet definition for the given packet id.
      Parameters:
      id - The packet id.
      Returns:
      The registered packet's class
    • getClientboundDefinition

      public PacketDefinition<?,?> getClientboundDefinition(int id)
      Gets the clientbound packet definition for the given packet id.
      Parameters:
      id - The packet id.
      Returns:
      The registered packet's class