From af58dd8d03d98842a190c15d551569f451e55f33 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 19 Dec 2013 04:45:30 +0100 Subject: [PATCH] Add the ability to look up PacketType from class. --- .../com/comphenix/protocol/PacketType.java | 22 +++++++++++++++++++ .../injector/netty/InjectionFactory.java | 18 +++++++-------- .../injector/netty/NettyProtocolRegistry.java | 2 +- .../injector/packet/PacketRegistry.java | 6 ++--- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index dfcf8cdb..ee9b52bb 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -653,6 +653,28 @@ public class PacketType implements Serializable { return type; } + /** + * Lookup a packet type from a packet class. + * @param packetClass - the packet class. + * @return The corresponding packet type, or NULL if not found. + */ + public static PacketType fromClass(Class packetClass) { + PacketType type = PacketRegistry.getPacketType(packetClass); + + if (type != null) + return type; + throw new IllegalArgumentException("Class " + packetClass + " is not a registered packet."); + } + + /** + * Determine if a given class represents a packet class. + * @param packetClass - the class to lookup. + * @return TRUE if this is a packet class, FALSE otherwise. + */ + public static boolean hasClass(Class packetClass) { + return PacketRegistry.getPacketType(packetClass) != null; + } + /** * Register a particular packet type. *

diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java index 9af27a61..10f81982 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/InjectionFactory.java @@ -79,15 +79,15 @@ class InjectionFactory { * @return The cached injector, or a closed injector if it could not be found. */ public Injector fromName(String name, Player player) { - if (closed) - return new ClosedInjector(player); - Injector injector = nameLookup.get(name); - - // We can only retrieve cached injectors - if (injector != null) { - // Update instance - injector.setUpdatedPlayer(player); - return injector; + if (!closed) { + Injector injector = nameLookup.get(name); + + // We can only retrieve cached injectors + if (injector != null) { + // Update instance + injector.setUpdatedPlayer(player); + return injector; + } } return new ClosedInjector(player); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolRegistry.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolRegistry.java index 57f2e5f0..da794ac1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolRegistry.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolRegistry.java @@ -43,7 +43,7 @@ public class NettyProtocolRegistry { } /** - * Retrieve an immutable view of the class to packet tyåe lookup. + * Retrieve an immutable view of the class to packet type lookup. * @return The packet type lookup. */ public Map, PacketType> getPacketClassLookup() { 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 2168836b..d4214a39 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 @@ -393,14 +393,14 @@ public class PacketRegistry { /** * Retrieve the packet type of a given packet. * @param packet - the class of the packet. - * @return The packet type. - * @throws IllegalArgumentException If this is not a valid packet. + * @return The packet type, or NULL if not found. */ public static PacketType getPacketType(Class packet) { initialize(); if (NETTY != null) return NETTY.getPacketClassLookup().get(packet); - return PacketType.findLegacy(LEGACY.getPacketID(packet)); + final int id = LEGACY.getPacketID(packet); + return PacketType.hasLegacy(id) ? PacketType.findLegacy(id) : null; } }