Add the ability to look up PacketType from class.

This commit is contained in:
Kristian S. Stangeland 2013-12-19 04:45:30 +01:00
parent acea92ef5c
commit af58dd8d03
4 changed files with 35 additions and 13 deletions

View File

@ -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.
* <p>

View File

@ -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);
}

View File

@ -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<Class<?>, PacketType> getPacketClassLookup() {

View File

@ -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;
}
}