From cd0341795c9bc857003bc9e4a61aea7d466c4629 Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Thu, 23 Mar 2017 01:51:30 +0100 Subject: [PATCH] Fix global PacketListeners being registered multiple times Because every PacketListenerData instance held information about all classes their PacketListener was listening to, and a single PacketListenerData instance could be mapped by multiple Packet classes in the packetListeners map, iterating over the packetListener map's value set and registering the PacketListener for every class contained registered global packet listeners as many times as there were classes it listened to. --- .../api/listener/PacketListener.java | 4 ---- .../api/registry/ListenerOwner.java | 7 +++---- .../packetgate/api/registry/PacketGate.java | 20 +++++++++++-------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/crushedpixel/sponge/packetgate/api/listener/PacketListener.java b/src/main/java/eu/crushedpixel/sponge/packetgate/api/listener/PacketListener.java index 8bf96f3..bf77c6a 100644 --- a/src/main/java/eu/crushedpixel/sponge/packetgate/api/listener/PacketListener.java +++ b/src/main/java/eu/crushedpixel/sponge/packetgate/api/listener/PacketListener.java @@ -3,9 +3,6 @@ import eu.crushedpixel.sponge.packetgate.api.event.PacketEvent; import eu.crushedpixel.sponge.packetgate.api.registry.PacketConnection; import lombok.Data; -import net.minecraft.network.Packet; - -import java.util.List; public interface PacketListener { @@ -17,7 +14,6 @@ public interface PacketListener { public class PacketListenerData { private final PacketListener packetListener; private final ListenerPriority priority; - private final List> classes; } public enum ListenerPriority { diff --git a/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/ListenerOwner.java b/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/ListenerOwner.java index d65bf3e..b64f7cd 100644 --- a/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/ListenerOwner.java +++ b/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/ListenerOwner.java @@ -2,7 +2,6 @@ import eu.crushedpixel.sponge.packetgate.api.listener.PacketListener; import eu.crushedpixel.sponge.packetgate.api.listener.PacketListener.PacketListenerData; -import net.minecraft.network.Packet; import java.util.Comparator; import java.util.List; @@ -12,10 +11,10 @@ public abstract class ListenerOwner { - protected final Map, List> packetListeners = new ConcurrentHashMap<>(); + protected final Map> packetListeners = new ConcurrentHashMap<>(); - void register(PacketListenerData packetListenerData) { - for (Class clazz : packetListenerData.getClasses()) { + void register(PacketListenerData packetListenerData, Class... classes) { + for (Class clazz : classes) { List list = packetListeners.get(clazz); if (list == null) list = new CopyOnWriteArrayList<>(); list.add(packetListenerData); diff --git a/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/PacketGate.java b/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/PacketGate.java index 862ea58..14c696e 100644 --- a/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/PacketGate.java +++ b/src/main/java/eu/crushedpixel/sponge/packetgate/api/registry/PacketGate.java @@ -20,7 +20,9 @@ public class PacketGate extends ListenerOwner { public void registerConnection(PacketConnection connection) { // register all global packet listeners to the connection - this.packetListeners.values().forEach(list -> list.forEach(connection::register)); + this.packetListeners.forEach((clazz, list) -> { + list.forEach(data -> connection.register(data, clazz)); + }); connections.add(connection); } @@ -44,7 +46,7 @@ public void registerListener(PacketListener packetListener, ListenerPriority pri public void registerListener(PacketListener packetListener, ListenerPriority priority, PacketConnection connection, Class... packetClasses) { - List> classes = new ArrayList<>(); + List classes = new ArrayList<>(); // if no classes are specified, apply the listener to all Minecraft packet classes if (packetClasses.length == 0) { @@ -65,12 +67,14 @@ public void registerListener(PacketListener packetListener, ListenerPriority pri } } - PacketListenerData packetListenerData = new PacketListenerData(packetListener, priority, classes); + PacketListenerData packetListenerData = new PacketListenerData(packetListener, priority); + + Class[] array = classes.toArray(new Class[classes.size()]); if (connection != null) { - connection.register(packetListenerData); + connection.register(packetListenerData, array); } else { - this.register(packetListenerData); + this.register(packetListenerData, array); } } @@ -83,9 +87,9 @@ public void unregisterListener(PacketListener packetListener) { } @Override - void register(PacketListenerData packetListenerData) { - super.register(packetListenerData); - connections.forEach(packetConnection -> packetConnection.register(packetListenerData)); + void register(PacketListenerData packetListenerData, Class... classes) { + super.register(packetListenerData, classes); + connections.forEach(packetConnection -> packetConnection.register(packetListenerData, classes)); } }