From df3b68df4cef8fa08bfeaf0345a2e00c87852b75 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 25 Mar 2023 21:45:29 -0500 Subject: [PATCH] Some cleanup Fixes super critical issue #2255 --- .../com/comphenix/protocol/PacketType.java | 44 +++++++++---------- .../injector/SortedPacketListenerList.java | 25 +++++------ .../protocol/injector/StructureCache.java | 3 +- .../netty/channel/NettyChannelInjector.java | 2 +- .../netty/manager/NetworkManagerInjector.java | 4 +- .../injector/packet/PacketRegistry.java | 18 ++++---- .../protocol/utility/MinecraftReflection.java | 4 ++ .../com/comphenix/protocol/utility/Util.java | 10 ----- .../protocol/events/PacketContainerTest.java | 2 +- 9 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/PacketType.java b/src/main/java/com/comphenix/protocol/PacketType.java index 29956062..a42f7cb7 100644 --- a/src/main/java/com/comphenix/protocol/PacketType.java +++ b/src/main/java/com/comphenix/protocol/PacketType.java @@ -41,19 +41,19 @@ public class PacketType implements Serializable, Cloneable, Comparable packets = event.getPacket().getPacketBundles().read(0); List outPackets = new ArrayList<>(); - for(PacketContainer subPacket : packets) { + for (PacketContainer subPacket : packets) { PacketEvent subPacketEvent = PacketEvent.fromServer(this, subPacket, event.getNetworkMarker(), event.getPlayer()); invokeUnpackedPacketSending(reporter, subPacketEvent, priorityFilter); - if(!subPacketEvent.isCancelled()) { - outPackets.add(subPacketEvent.getPacket()); // if the packet event has been cancelled, the packet will be removed from the bundle. + if (!subPacketEvent.isCancelled()) { + // if the packet event has been cancelled, the packet will be removed from the bundle + outPackets.add(subPacketEvent.getPacket()); } } - if(packets.iterator().hasNext()) { // are there still packets in this bundle? + + if (packets.iterator().hasNext()) { event.getPacket().getPacketBundles().write(0, outPackets); } else { - event.setCancelled(true); // cancel packet if each individual packet has been canceled + // cancel entire packet if each individual packet has been cancelled + event.setCancelled(true); } } } @@ -201,14 +202,12 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu * @param event - the related packet event. * @param element - the listener to invoke. */ - private final void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener element) { + private void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener element) { try { event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR); element.getListener().onPacketSending(event); - } catch (OutOfMemoryError e) { - throw e; - } catch (ThreadDeath e) { + } catch (OutOfMemoryError | ThreadDeath e) { throw e; } catch (Throwable e) { // Minecraft doesn't want your Exception. diff --git a/src/main/java/com/comphenix/protocol/injector/StructureCache.java b/src/main/java/com/comphenix/protocol/injector/StructureCache.java index 78ace13e..624c6f93 100644 --- a/src/main/java/com/comphenix/protocol/injector/StructureCache.java +++ b/src/main/java/com/comphenix/protocol/injector/StructureCache.java @@ -137,9 +137,10 @@ public class StructureCache { Class packetClass = PacketRegistry.getPacketClassFromType(type); // We need to map the Bundle Delimiter to the synthetic bundle packet which contains a list of all packets in a bundle - if(MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) { + if (MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) { packetClass = MinecraftReflection.getPackedBundlePacketClass(); } + return new StructureModifier<>(packetClass, MinecraftReflection.getPacketClass(), true); }); } diff --git a/src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java b/src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java index f355c02a..22ea4c8f 100644 --- a/src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java +++ b/src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java @@ -553,7 +553,7 @@ public class NettyChannelInjector implements Injector { } // no listener and no marker - no magic :) - if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !Util.isBundlePacket(packet.getClass())) { + if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !MinecraftReflection.isBundlePacket(packet.getClass())) { return action; } diff --git a/src/main/java/com/comphenix/protocol/injector/netty/manager/NetworkManagerInjector.java b/src/main/java/com/comphenix/protocol/injector/netty/manager/NetworkManagerInjector.java index 8b5542fb..0e75c52e 100644 --- a/src/main/java/com/comphenix/protocol/injector/netty/manager/NetworkManagerInjector.java +++ b/src/main/java/com/comphenix/protocol/injector/netty/manager/NetworkManagerInjector.java @@ -91,7 +91,7 @@ public class NetworkManagerInjector implements ChannelListener { public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMarker marker) { // check if we need to intercept the packet Class packetClass = packet.getClass(); - if (this.outboundListeners.contains(packetClass) || marker != null || Util.isBundlePacket(packetClass)) { + if (marker != null || MinecraftReflection.isBundlePacket(packetClass) || outboundListeners.contains(packetClass)) { // wrap packet and construct the event PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet); PacketEvent packetEvent = PacketEvent.fromServer(this, container, marker, injector.getPlayer()); @@ -109,7 +109,7 @@ public class NetworkManagerInjector implements ChannelListener { public PacketEvent onPacketReceiving(Injector injector, Object packet, NetworkMarker marker) { // check if we need to intercept the packet Class packetClass = packet.getClass(); - if (this.inboundListeners.contains(packetClass) || marker != null) { + if (marker != null || inboundListeners.contains(packetClass)) { // wrap the packet and construct the event PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet); PacketEvent packetEvent = PacketEvent.fromClient(this, container, marker, injector.getPlayer()); diff --git a/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java b/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java index 93bf1098..4dc8c598 100644 --- a/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java +++ b/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java @@ -46,12 +46,12 @@ public class PacketRegistry { */ protected static class Register { // The main lookup table - private final Map>> typeToClass = new ConcurrentHashMap<>(); - private final Map, PacketType> classToType = new ConcurrentHashMap<>(); + final Map>> typeToClass = new ConcurrentHashMap<>(); + final Map, PacketType> classToType = new ConcurrentHashMap<>(); - private volatile Set serverPackets = new HashSet<>(); - private volatile Set clientPackets = new HashSet<>(); - private final List containers = new ArrayList<>(); + volatile Set serverPackets = new HashSet<>(); + volatile Set clientPackets = new HashSet<>(); + final List containers = new ArrayList<>(); public Register() {} @@ -383,10 +383,12 @@ public class PacketRegistry { * @return The packet type, or NULL if not found. */ public static PacketType getPacketType(Class packet) { - if(Util.isBundlePacket(packet)) { - return PacketType.Play.Server.DELIMITER; - } initialize(); + + if (MinecraftReflection.isBundlePacket(packet)) { + return PacketType.Play.Server.BUNDLE; + } + return REGISTER.classToType.get(packet); } diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java b/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java index e75f5261..20a22fbd 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java @@ -632,6 +632,10 @@ public final class MinecraftReflection { return getMinecraftClass("network.protocol.game.ClientboundBundlePacket", "ClientboundBundlePacket"); } + public static boolean isBundlePacket(Class packetClass) { + return MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove() && packetClass.equals(getPackedBundlePacketClass()); + } + public static Class getBundleDelimiterClass() { return getMinecraftClass("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket"); } diff --git a/src/main/java/com/comphenix/protocol/utility/Util.java b/src/main/java/com/comphenix/protocol/utility/Util.java index cdb53d6c..18c5f12d 100644 --- a/src/main/java/com/comphenix/protocol/utility/Util.java +++ b/src/main/java/com/comphenix/protocol/utility/Util.java @@ -60,14 +60,4 @@ public final class Util { } return false; } - - public static boolean isBundlePacket(Class packetClass) { - if(!MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2)) { - return false; - } - if(cachedBundleClass == null) { - cachedBundleClass = MinecraftReflection.getPackedBundlePacketClass(); - } - return packetClass.equals(cachedBundleClass); - } } diff --git a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index bbebadea..13233b8a 100644 --- a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -828,7 +828,7 @@ public class PacketContainerTest { for (PacketType type : PacketType.values()) { // TODO: try to support chat - for now chat contains to many sub classes to properly clone it if (type.isDeprecated() || !type.isSupported() || type.name().contains("CUSTOM_PAYLOAD") || type.name().contains("CHAT") - || type.name().contains("DELIMITER")) { + || type.name().contains("BUNDLE")) { continue; }