diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index ee9b52bb..f0aa05a7 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -631,6 +631,29 @@ public class PacketType implements Serializable { return getLookup().getFromCurrent(protocol, sender, packetId) != null; } + /** + * Retrieve a packet type from a legacy ID. + *

+ * If no associated packet type could be found, a new will be registered under LEGACY. + * @param id - the legacy ID. + * @param sender - the sender of the packet, or NULL if unknown. + * @return The packet type. + * @throws IllegalArgumentException If the sender is NULL and the packet doesn't exist. + */ + public static PacketType fromLegacy(int id, Sender sender) { + PacketType type = getLookup().getFromLegacy(id); + + if (type == null) { + if (sender == null) + throw new IllegalArgumentException("Cannot find legacy packet " + id); + type = newLegacy(sender, id); + + // As below + scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString()); + } + return type; + } + /** * Retrieve a packet type from a protocol, sender and packet ID. *

diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index 2d29bc19..3e9b81cd 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -1018,7 +1018,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok @Override @Deprecated public int getPacketID(Object packet) { - return getPacketType(packet).getLegacyId(); + return PacketRegistry.getPacketID(packet.getClass()); } @Override diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java index d4214a39..7e1f55e9 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java @@ -396,11 +396,25 @@ public class PacketRegistry { * @return The packet type, or NULL if not found. */ public static PacketType getPacketType(Class packet) { + return getPacketType(packet, null); + } + + /** + * Retrieve the packet type of a given packet. + * @param packet - the class of the packet. + * @param sender - the sender of the packet, or NULL. + * @return The packet type, or NULL if not found. + */ + public static PacketType getPacketType(Class packet, Sender sender) { initialize(); - if (NETTY != null) + if (NETTY != null) { return NETTY.getPacketClassLookup().get(packet); - final int id = LEGACY.getPacketID(packet); - return PacketType.hasLegacy(id) ? PacketType.findLegacy(id) : null; + } else { + final int id = LEGACY.getPacketID(packet); + + return PacketType.hasLegacy(id) ? + PacketType.fromLegacy(id, sender) : null; + } } }