Class PacketType

java.lang.Object
com.comphenix.protocol.PacketType
All Implemented Interfaces:
Serializable, Cloneable, Comparable<PacketType>

public class PacketType extends Object implements Serializable, Cloneable, Comparable<PacketType>
Represents the type of a packet in a specific protocol.

Note that vanilla Minecraft reuses packet IDs per protocol (ping, game, login) and IDs are subject to change, so they are not reliable.

See Also:
  • Field Details

    • UNKNOWN_PACKET

      public static final int UNKNOWN_PACKET
      Represents an unknown packet ID.
      See Also:
  • Constructor Details

    • PacketType

      public PacketType(PacketType.Protocol protocol, PacketType.Sender sender, int currentId, String... names)
      Construct a new packet type.
      Parameters:
      protocol - - the current protocol.
      sender - - client or server.
      currentId - - the current packet ID, or
    • PacketType

      public PacketType(PacketType.Protocol protocol, PacketType.Sender sender, int currentId, MinecraftVersion version, String... names)
      Construct a new packet type.
      Parameters:
      protocol - - the current protocol.
      sender - - client or server.
      currentId - - the current packet ID.
      version - - the version of the current ID.
  • Method Details

    • values

      public static Iterable<PacketType> values()
      Find every packet type known to the current version of ProtocolLib.
      Returns:
      Every packet type.
    • findLegacy

      @Deprecated public static PacketType findLegacy(int packetId)
      Deprecated.
      Legacy IDs haven't functioned properly for some time
      Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
      Parameters:
      packetId - - the legacy packet ID.
      Returns:
      The corresponding packet type.
      Throws:
      IllegalArgumentException - If the legacy packet could not be found.
    • findLegacy

      @Deprecated public static PacketType findLegacy(int packetId, PacketType.Sender preference)
      Deprecated.
      Legacy IDs haven't functioned properly for some time
      Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
      Parameters:
      packetId - - the legacy packet ID.
      preference - - the preferred sender, or NULL for any arbitrary sender.
      Returns:
      The corresponding packet type.
      Throws:
      IllegalArgumentException - If the legacy packet could not be found.
    • hasLegacy

      @Deprecated public static boolean hasLegacy(int packetId)
      Deprecated.
      Legacy IDs haven't functioned properly for some time
      Determine if the given legacy packet exists.
      Parameters:
      packetId - - the legacy packet ID.
      Returns:
      TRUE if it does, FALSE otherwise.
    • findCurrent

      public static PacketType findCurrent(PacketType.Protocol protocol, PacketType.Sender sender, int packetId)
      Retrieve a packet type from a protocol, sender and packet ID.

      It is almost always better to access the packet types statically, like so:

      However, there are some valid uses for packet IDs. Please note that IDs change almost every Minecraft version.
      Parameters:
      protocol - - the current protocol.
      sender - - the sender.
      packetId - - the packet ID.
      Returns:
      The corresponding packet type.
      Throws:
      IllegalArgumentException - If the current packet could not be found.
    • findCurrent

      public static PacketType findCurrent(PacketType.Protocol protocol, PacketType.Sender sender, String name)
    • hasCurrent

      public static boolean hasCurrent(PacketType.Protocol protocol, PacketType.Sender sender, int packetId)
      Determine if the given packet exists.
      Parameters:
      protocol - - the protocol.
      sender - - the sender.
      packetId - - the packet ID.
      Returns:
      TRUE if it exists, FALSE otherwise.
    • fromID

      public static PacketType fromID(PacketType.Protocol protocol, PacketType.Sender sender, int packetId, Class<?> packetClass)
      Retrieve a packet type from a protocol, sender and packet ID, for pre-1.8.

      The packet will automatically be registered if it is missing.

      Parameters:
      protocol - - the current protocol.
      sender - - the sender.
      packetId - - the packet ID. Can be UNKNOWN_PACKET.
      packetClass - - the packet class
      Returns:
      The corresponding packet type.
    • fromCurrent

      public static PacketType fromCurrent(PacketType.Protocol protocol, PacketType.Sender sender, int packetId, Class<?> packetClass)
      Retrieve a packet type from a protocol, sender, ID, and class for 1.8+

      The packet will automatically be registered if it is missing.

      Parameters:
      protocol - - the current protocol.
      sender - - the sender.
      packetId - - the packet ID. Can be UNKNOWN_PACKET.
      packetClass - - the packet class.
      Returns:
      The corresponding packet type.
    • fromClass

      @Deprecated public static PacketType fromClass(Class<?> packetClass)
      Deprecated.
      since 1.20.2 there are packet classes that are shared between protocol states, therefore the result can be invalid.
      Lookup a packet type from a packet class.
      Parameters:
      packetClass - - the packet class.
      Returns:
      The corresponding packet type, never null.
      Throws:
      IllegalArgumentException - if the given packet class is not a registered packet.
    • fromName

      public static Collection<PacketType> fromName(String name)
      Retrieve every packet type with the given UPPER_CAMEL_CASE name.

      Note that the collection is unmodiable.

      Parameters:
      name - - the name.
      Returns:
      Every packet type, or an empty collection.
    • hasClass

      @Deprecated public static boolean hasClass(Class<?> packetClass)
      Deprecated.
      Doesn't really have a purpose
      Determine if a given class represents a packet class.
      Parameters:
      packetClass - - the class to lookup.
      Returns:
      TRUE if this is a packet class, FALSE otherwise.
    • scheduleRegister

      public static void scheduleRegister(PacketType type, String name)
      Register a particular packet type.

      Note that the registration will be performed on the main thread.

      Parameters:
      type - - the type to register.
      name - - the name of the packet.
    • getObjectEnum

      public static PacketTypeEnum getObjectEnum(PacketType type)
      Retrieve the correct object enum from a specific packet type.
      Parameters:
      type - - the packet type.
      Returns:
      The corresponding object enum.
    • isSupported

      public boolean isSupported()
      Determine if this packet is supported on the current server.
      Returns:
      Whether the packet is supported.
    • getProtocol

      public PacketType.Protocol getProtocol()
      Retrieve the protocol (the connection state) the packet type belongs.
      Returns:
      The protocol of this type.
    • getSender

      public PacketType.Sender getSender()
      Retrieve which sender will transmit packets of this type.
      Returns:
      The sender of these packets.
    • isClient

      public boolean isClient()
      Determine if this packet was sent by the client.
      Returns:
      TRUE if it was, FALSE otherwise.
    • isServer

      public boolean isServer()
      Determine if this packet was sent by the server.
      Returns:
      TRUE if it was, FALSE otherwise.
    • getCurrentId

      @Deprecated public int getCurrentId()
      Deprecated.
      Don't rely on packet IDs, they change every version
      Retrieve the current protocol ID for this packet type.

      This is only unique within a specific protocol and target.

      It is unknown if the packet was removed at any point.

      Returns:
      The current ID, or UNKNOWN_PACKET if unknown.
    • getClassNames

      public List<String> getClassNames()
    • getPacketClass

      public Class<?> getPacketClass()
      Retrieve the equivalent packet class.
      Returns:
      The packet class, or NULL if not found.
    • name

      public String name()
      Retrieve the declared enum name of this packet type.
      Returns:
      The enum name.
    • isDeprecated

      public boolean isDeprecated()
      Whether this packet is deprecated. Deprecated packet types have either been renamed, replaced, or removed. Kind of like the thing they use to tell children to recycle except with packets you probably shouldn't be using.
      Returns:
      True if the type is deprecated, false if not
    • isAsyncForced

      public boolean isAsyncForced()
      Whether the processing of this packet must take place on a thread different from the main thread. You don't get a choice. If this is false it's up to you.
      Returns:
      True if async processing is forced, false if not.
    • getCurrentVersion

      public MinecraftVersion getCurrentVersion()
      Retrieve the Minecraft version for the current ID.
      Returns:
      The Minecraft version.
    • isDynamic

      public boolean isDynamic()
      Whether this packet was dynamically created (i.e. we don't have it registered)
      Returns:
      True if dnyamic, false if not.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(PacketType other)
      Specified by:
      compareTo in interface Comparable<PacketType>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public PacketType clone()
      Overrides:
      clone in class Object