From a718cc9067441fbba8a13ee2919880fe0acc3b2a Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 31 May 2015 14:42:52 -0400 Subject: [PATCH] Add a test to make sure all packets are registered --- .../com/comphenix/protocol/PacketType.java | 25 ++++++++++++++++++- .../comphenix/protocol/PacketTypeTest.java | 24 ++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index 730975bd..bd3b4173 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -523,6 +523,7 @@ public class PacketType implements Serializable, Comparable { private final int currentId; private final int legacyId; private final MinecraftVersion version; + private final boolean dynamic; /** * Retrieve the current packet/legacy lookup. @@ -678,7 +679,7 @@ public class PacketType implements Serializable, Comparable { PacketType type = getLookup().getFromCurrent(protocol, sender, packetId); if (type == null) { - type = new PacketType(protocol, sender, packetId, legacyId); + type = new PacketType(protocol, sender, packetId, legacyId, PROTOCOL_VERSION, true); // Many may be scheduled, but only the first will be executed scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString()); @@ -797,11 +798,25 @@ public class PacketType implements Serializable, Comparable { * @param version - the version of the current ID. */ public PacketType(Protocol protocol, Sender sender, int currentId, int legacyId, MinecraftVersion version) { + this(protocol, sender, currentId, legacyId, version, false); + } + + /** + * Construct a new packet type. + * @param protocol - the current protocol. + * @param sender - client or server. + * @param currentId - the current packet ID. + * @param legacyId - the legacy packet ID. + * @param version - the version of the current ID. + * @param dynamic - if this type was created dynamically. + */ + public PacketType(Protocol protocol, Sender sender, int currentId, int legacyId, MinecraftVersion version, boolean dynamic) { this.protocol = Preconditions.checkNotNull(protocol, "protocol cannot be NULL"); this.sender = Preconditions.checkNotNull(sender, "sender cannot be NULL"); this.currentId = currentId; this.legacyId = legacyId; this.version = version; + this.dynamic = dynamic; } /** @@ -903,6 +918,14 @@ public class PacketType implements Serializable, Comparable { return legacyId; } + /** + * Whether or not this packet was dynamically created (ie we don't have it registered) + * @return True if dnyamic, false if not. + */ + public boolean isDynamic() { + return dynamic; + } + @Override public int hashCode() { return Objects.hashCode(protocol, sender, currentId, legacyId); diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/PacketTypeTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/PacketTypeTest.java index aa744859..897a8096 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/PacketTypeTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/PacketTypeTest.java @@ -1,21 +1,41 @@ package com.comphenix.protocol; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.Map; +import java.util.Map.Entry; import org.junit.BeforeClass; import org.junit.Test; import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.PacketType.Sender; +import com.comphenix.protocol.injector.netty.NettyProtocolRegistry; public class PacketTypeTest { + @BeforeClass public static void initializeReflection() throws IllegalAccessException { BukkitInitialization.initializePackage(); } - + @Test public void testFindCurrent() { assertEquals(PacketType.Play.Client.STEER_VEHICLE, PacketType.findCurrent(Protocol.PLAY, Sender.CLIENT, 12)); } -} + + @Test + public void ensureAllExist() { + NettyProtocolRegistry registry = new NettyProtocolRegistry(); + Map> lookup = registry.getPacketTypeLookup(); + for (Entry> entry : lookup.entrySet()) { + PacketType type = entry.getKey(); + Class clazz = entry.getValue(); + + if (type.isDynamic()) { + fail("Packet " + clazz + " does not have a corresponding PacketType!"); + } + } + } +} \ No newline at end of file