From e0fed34fe4d23ffb32e02833ba48eaf0b67a487e Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 30 Dec 2013 15:25:34 +0100 Subject: [PATCH] Make it possible to look up the enum name of a PacketType. --- .../com/comphenix/protocol/PacketType.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index f0aa05a7..455513ae 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -713,20 +713,7 @@ public class PacketType implements Serializable { ObjectEnum objEnum; // A bit ugly, but performance is critical - switch (type.getProtocol()) { - case HANDSHAKING: - objEnum = type.isClient() ? Handshake.Client.getInstance() : Handshake.Server.getInstance(); break; - case PLAY: - objEnum = type.isClient() ? Play.Client.getInstance() : Play.Server.getInstance(); break; - case STATUS: - objEnum = type.isClient() ? Status.Client.getInstance() : Status.Server.getInstance(); break; - case LOGIN: - objEnum = type.isClient() ? Login.Client.getInstance() : Login.Server.getInstance(); break; - case LEGACY: - objEnum = type.isClient() ? Legacy.Client.getInstance() : Legacy.Server.getInstance(); break; - default: - throw new IllegalStateException("Unexpected protocol: " + type.getProtocol()); - } + objEnum = getObjectEnum(type); if (objEnum.registerMember(type, name)) { getLookup().addPacketTypes(Arrays.asList(type)); @@ -747,6 +734,28 @@ public class PacketType implements Serializable { return ProtocolLibrary.getExecutorSync().submit(callable); } + /** + * Retrieve the correct object enum from a specific packet type. + * @param type - the packet type. + * @return The corresponding object enum. + */ + public static ObjectEnum getObjectEnum(final PacketType type) { + switch (type.getProtocol()) { + case HANDSHAKING: + return type.isClient() ? Handshake.Client.getInstance() : Handshake.Server.getInstance(); + case PLAY: + return type.isClient() ? Play.Client.getInstance() : Play.Server.getInstance(); + case STATUS: + return type.isClient() ? Status.Client.getInstance() : Status.Server.getInstance(); + case LOGIN: + return type.isClient() ? Login.Client.getInstance() : Login.Server.getInstance(); + case LEGACY: + return type.isClient() ? Legacy.Client.getInstance() : Legacy.Server.getInstance(); + default: + throw new IllegalStateException("Unexpected protocol: " + type.getProtocol()); + } + } + /** * Construct a new packet type. * @param protocol - the current protocol. @@ -846,6 +855,14 @@ public class PacketType implements Serializable { return null; } } + + /** + * Retrieve the declared enum name of this packet type. + * @return The enum name. + */ + public String name() { + return getObjectEnum(this).getDeclaredName(this); + } /** * Retrieve the Minecraft version for the current ID.