From 351dc7f3df94e92a3efcde57c70fcb541286b398 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Wed, 6 Feb 2013 22:40:17 +0100 Subject: [PATCH] Ensure that ProtocolLib discoveres mod-specific packets. --- .../injector/PacketFilterManager.java | 13 ++++----- .../injector/packet/PacketRegistry.java | 27 +++++++++++++------ 2 files changed, 26 insertions(+), 14 deletions(-) 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 eea43aae..0c62ebab 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -133,8 +133,8 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok private AsyncFilterManager asyncFilterManager; // Valid server and client packets - private Set serverPackets; - private Set clientPackets; + private boolean knowsServerPackets; + private boolean knowsClientPackets; // Ensure that we're not performing too may injections private AtomicInteger phaseLoginCount = new AtomicInteger(0); @@ -214,8 +214,8 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok // Attempt to load the list of server and client packets try { - this.serverPackets = PacketRegistry.getServerPackets(); - this.clientPackets = PacketRegistry.getClientPackets(); + knowsServerPackets = PacketRegistry.getServerPackets() != null; + knowsClientPackets = PacketRegistry.getClientPackets() != null; } catch (FieldAccessException e) { reporter.reportWarning(this, "Cannot load server and client packet list.", e); } @@ -466,7 +466,8 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok for (int packetID : packets) { // Only register server packets that are actually supported by Minecraft if (side.isForServer()) { - if (serverPackets != null && serverPackets.contains(packetID)) + // Note that we may update the packet list here + if (!knowsServerPackets || PacketRegistry.getServerPackets().contains(packetID)) playerInjection.addPacketHandler(packetID); else reporter.reportWarning(this, String.format( @@ -477,7 +478,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok // As above, only for client packets if (side.isForClient() && packetInjector != null) { - if (clientPackets != null && clientPackets.contains(packetID)) + if (!knowsClientPackets || PacketRegistry.getClientPackets().contains(packetID)) packetInjector.addPacketHandler(packetID); else reporter.reportWarning(this, String.format( 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 73f4e587..918546f4 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 @@ -47,8 +47,12 @@ public class PacketRegistry { private static Map packetToID; // Whether or not certain packets are sent by the client or the server - private static Set serverPackets; - private static Set clientPackets; + private static ImmutableSet serverPackets; + private static ImmutableSet clientPackets; + + // The underlying sets + private static Set serverPacketsRef; + private static Set clientPacketsRef; // New proxy values private static Map overwrittenPackets = new HashMap(); @@ -120,21 +124,21 @@ public class PacketRegistry { @SuppressWarnings("unchecked") private static void initializeSets() throws FieldAccessException { - if (serverPackets == null || clientPackets == null) { + if (serverPacketsRef == null || clientPacketsRef == null) { List sets = getPacketRegistry().getFieldListByType(Set.class); try { if (sets.size() > 1) { - serverPackets = (Set) FieldUtils.readStaticField(sets.get(0), true); - clientPackets = (Set) FieldUtils.readStaticField(sets.get(1), true); + serverPacketsRef = (Set) FieldUtils.readStaticField(sets.get(0), true); + clientPacketsRef = (Set) FieldUtils.readStaticField(sets.get(1), true); // Impossible - if (serverPackets == null || clientPackets == null) + if (serverPacketsRef == null || clientPacketsRef == null) throw new FieldAccessException("Packet sets are in an illegal state."); // NEVER allow callers to modify the underlying sets - serverPackets = ImmutableSet.copyOf(serverPackets); - clientPackets = ImmutableSet.copyOf(clientPackets); + serverPackets = ImmutableSet.copyOf(serverPacketsRef); + clientPackets = ImmutableSet.copyOf(clientPacketsRef); } else { throw new FieldAccessException("Cannot retrieve packet client/server sets."); @@ -143,6 +147,13 @@ public class PacketRegistry { } catch (IllegalAccessException e) { throw new FieldAccessException("Cannot access field.", e); } + + } else { + // Copy over again if it has changed + if (serverPacketsRef != null && serverPacketsRef.size() != serverPackets.size()) + serverPackets = ImmutableSet.copyOf(serverPacketsRef); + if (clientPacketsRef != null && clientPacketsRef.size() != clientPackets.size()) + clientPackets = ImmutableSet.copyOf(clientPacketsRef); } }