From 96f24167bb86f7f3fba5f9cb2aed436363f87dcd Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 14 Dec 2013 18:08:08 +0100 Subject: [PATCH] Don't synchronize with the main thread when registering async listeners --- .../protocol/async/NullPacketListener.java | 4 ++- .../protocol/events/ListeningWhitelist.java | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/async/NullPacketListener.java b/ProtocolLib/src/main/java/com/comphenix/protocol/async/NullPacketListener.java index 70d9449b..4668b1df 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/async/NullPacketListener.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/async/NullPacketListener.java @@ -19,6 +19,7 @@ package com.comphenix.protocol.async; import org.bukkit.plugin.Plugin; +import com.comphenix.protocol.events.ListenerOptions; import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListeningWhitelist; import com.comphenix.protocol.events.PacketEvent; @@ -67,7 +68,8 @@ class NullPacketListener implements PacketListener { private ListeningWhitelist cloneWhitelist(ListenerPriority priority, ListeningWhitelist whitelist) { if (whitelist != null) - return ListeningWhitelist.newBuilder(whitelist).priority(priority).build(); + // We don't use the Bukkit API, so don't engage the ProtocolLib synchronization code + return ListeningWhitelist.newBuilder(whitelist).priority(priority).mergeOptions(ListenerOptions.ASYNC).build(); else return 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 0c3b2c5e..1c234aa4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/ListeningWhitelist.java @@ -420,7 +420,6 @@ public class ListeningWhitelist { public Builder gamePhaseBoth() { return gamePhase(GamePhase.BOTH); } - /** * Set the options to copy when constructing new whitelists. * @param options - the options. @@ -431,6 +430,16 @@ public class ListeningWhitelist { return this; } + /** + * Set the options to copy when constructing new whitelists. + * @param options - the options. + * @return This builder, for chaining. + */ + public Builder options(Collection options) { + this.options = safeSet(options); + return this; + } + /** * Set the options to copy when constructing new whitelists. * @param options - the options array. @@ -441,6 +450,29 @@ public class ListeningWhitelist { return this; } + /** + * Options to merge into the current set of options. + * @param options - the options array. + * @return This builder, for chaining. + */ + public Builder mergeOptions(ListenerOptions... serverOptions) { + return mergeOptions(Arrays.asList(serverOptions)); + } + + /** + * Options to merge into the current set of options. + * @param options - the options array. + * @return This builder, for chaining. + */ + public Builder mergeOptions(Collection serverOptions) { + if (options == null) + return options(serverOptions); + + // Merge the options + this.options.addAll(serverOptions); + return this; + } + /** * Construct a new whitelist from the values in this builder. * @return The new whitelist.