From f378b7bbd99d8ec56e5fcfe6624a5d4bb34e4680 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 26 Jul 2014 17:28:05 +0200 Subject: [PATCH] Added support for closing asynchronous listeners by packet listener. --- .../protocol/AsynchronousManager.java | 6 ++++ .../protocol/async/AsyncFilterManager.java | 35 +++++++++++++++++-- .../protocol/events/ListeningWhitelist.java | 7 ++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/AsynchronousManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/AsynchronousManager.java index 9067b144..95a38b30 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/AsynchronousManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/AsynchronousManager.java @@ -50,6 +50,12 @@ public interface AsynchronousManager { */ public abstract void unregisterAsyncHandler(AsyncListenerHandler handler); + /** + * Unregisters and closes the first asynchronous handler associated with the given listener. + * @param handler - asynchronous handler. + */ + public abstract void unregisterAsyncHandler(PacketListener listener); + /** * Unregisters every asynchronous handler associated with this plugin. * @param plugin - the original plugin. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncFilterManager.java index f05100b4..dac4d905 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/async/AsyncFilterManager.java @@ -175,12 +175,15 @@ public class AsyncFilterManager implements AsynchronousManager { ListeningWhitelist sendingWhitelist = listener.getSendingWhitelist(); ListeningWhitelist receivingWhitelist = listener.getReceivingWhitelist(); + if (!hasValidWhitelist(sendingWhitelist) && !hasValidWhitelist(receivingWhitelist)) { + throw new IllegalArgumentException("Listener has an empty sending and receiving whitelist."); + } + // Add listener to either or both processing queue if (hasValidWhitelist(sendingWhitelist)) { PacketFilterManager.verifyWhitelist(listener, sendingWhitelist); serverProcessingQueue.addListener(handler, sendingWhitelist); } - if (hasValidWhitelist(receivingWhitelist)) { PacketFilterManager.verifyWhitelist(listener, receivingWhitelist); clientProcessingQueue.addListener(handler, receivingWhitelist); @@ -191,7 +194,6 @@ public class AsyncFilterManager implements AsynchronousManager { handler.setNullPacketListener(new NullPacketListener(listener)); manager.addPacketListener(handler.getNullPacketListener()); } - return handler; } @@ -214,6 +216,35 @@ public class AsyncFilterManager implements AsynchronousManager { } } + @Override + public void unregisterAsyncHandler(PacketListener listener) { + if (listener == null) + throw new IllegalArgumentException("listener cannot be NULL."); + + AsyncListenerHandler handler = + findHandler(serverProcessingQueue, listener.getSendingWhitelist(), listener); + + if (handler == null) { + handler = findHandler(clientProcessingQueue, listener.getReceivingWhitelist(), listener); + } + unregisterAsyncHandler(handler); + } + + // Search for the first correct handler + private AsyncListenerHandler findHandler(PacketProcessingQueue queue, ListeningWhitelist search, PacketListener target) { + if (ListeningWhitelist.isEmpty(search)) + return null; + + for (PacketType type : search.getTypes()) { + for (PrioritizedListener element : queue.getListener(type)) { + if (element.getListener().getAsyncListener() == target) { + return element.getListener(); + } + } + } + return null; + } + @Override public void unregisterAsyncHandler(AsyncListenerHandler handler) { if (handler == null) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java index 1c62dbe4..940028b7 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java @@ -218,7 +218,7 @@ public class ListeningWhitelist { else if (whitelist == null) return true; else - return whitelist.getWhitelist().isEmpty(); + return whitelist.getTypes().isEmpty(); } @Override @@ -294,9 +294,10 @@ public class ListeningWhitelist { * @author Kristian */ public static class Builder { - private ListenerPriority priority; + // Default values + private ListenerPriority priority = ListenerPriority.NORMAL; private Set types = Sets.newHashSet(); - private GamePhase gamePhase; + private GamePhase gamePhase = GamePhase.PLAYING; private Set options = Sets.newHashSet(); /**