Class PacketContainer

java.lang.Object
com.comphenix.protocol.events.AbstractStructure
com.comphenix.protocol.events.PacketContainer
All Implemented Interfaces:
Serializable

public class PacketContainer extends AbstractStructure implements Serializable
Represents a Minecraft packet indirectly.
See Also:
  • Constructor Details

    • PacketContainer

      public PacketContainer(PacketType type)
      Creates a packet container for a new packet.
      Parameters:
      type - - the type of the packet to create.
    • PacketContainer

      public PacketContainer(PacketType type, Object handle)
      Creates a packet container for an existing packet.
      Parameters:
      type - - Type of the given packet.
      handle - - contained packet.
    • PacketContainer

      public PacketContainer(PacketType type, Object handle, StructureModifier<Object> structure)
      Creates a packet container for an existing packet.
      Parameters:
      type - - Type of the given packet.
      handle - - contained packet.
      structure - - structure modifier.
    • PacketContainer

      protected PacketContainer()
      For serialization.
  • Method Details

    • fromPacket

      public static PacketContainer fromPacket(Object packet)
      Construct a new packet container from a given handle.
      Parameters:
      packet - - the NMS packet.
      Returns:
      The packet container.
    • getHandle

      public Object getHandle()
      Retrieves the underlying Minecraft packet.
      Overrides:
      getHandle in class AbstractStructure
      Returns:
      Underlying Minecraft packet.
    • getModifier

      public StructureModifier<Object> getModifier()
      Retrieves the generic structure modifier for this packet.
      Overrides:
      getModifier in class AbstractStructure
      Returns:
      Structure modifier.
    • getStructures

      public StructureModifier<InternalStructure> getStructures()
    • getOptionalStructures

      public StructureModifier<Optional<InternalStructure>> getOptionalStructures()
    • getId

      @Deprecated public int getId()
      Deprecated.
      Packet IDs are unreliable
    • getType

      public PacketType getType()
      Retrieve the packet type of this packet.
      Returns:
      The packet type.
    • shallowClone

      public PacketContainer shallowClone()
      Create a shallow copy of the current packet.

      This merely writes the content of each field to the new class directly, without performing any expensive copies.

      Returns:
      A shallow copy of the current packet.
    • deepClone

      public PacketContainer deepClone()
      Create a deep copy of the current packet.

      This will perform a full copy of the entire object tree, only skipping known immutable objects and primitive types.

      Note that the inflated buffers in packet 51 and 56 will be copied directly to save memory.

      Returns:
      A deep copy of the current packet.
    • createPacketBuffer

      @Deprecated public static io.netty.buffer.ByteBuf createPacketBuffer()
      Construct a new packet data serializer.
      Returns:
      The packet data serializer.
    • deserializeFromBuffer

      public static Object deserializeFromBuffer(PacketType packetType, Object buffer)
    • serializeToBuffer

      public Object serializeToBuffer()
    • getMeta

      public <T> Optional<T> getMeta(String key)
      Gets the metadata value for a given key if it exists. Packet metadata expires after a minute, which is far longer than a packet will ever be held in processing.
      Type Parameters:
      T - Metadata type
      Parameters:
      key - Metadata key
      Returns:
      The metadata value, or an empty optional
    • setMeta

      public <T> void setMeta(String key, T value)
      Sets the metadata value at a given key. Packet metadata expires after a minute, which is far longer than a packet will ever be held in processing.
      Type Parameters:
      T - Metadata type
      Parameters:
      key - Metadata key
      value - Metadata value
    • removeMeta

      public void removeMeta(String key)
      Removes the metadata for a given key if it exists.
      Parameters:
      key - Key to remove meta for
    • toString

      public String toString()
      Overrides:
      toString in class Object