From 711990fa15c66d18301b91c6a502ec5e579e49b0 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sun, 30 Sep 2012 00:13:36 +0200 Subject: [PATCH] Simplify the act of creating an asynchronous listener. --- .../comphenix/protocol/ProtocolLibrary.java | 3 ++- .../protocol/async/AsyncFilterManager.java | 19 ++++++++++++++++++- .../protocol/async/AsyncListenerHandler.java | 11 +++++++++++ .../injector/PacketFilterManager.java | 5 +++-- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java index 56668637..a3a7e31d 100644 --- a/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java @@ -53,7 +53,8 @@ public class ProtocolLibrary extends JavaPlugin { @Override public void onLoad() { logger = getLoggerSafely(); - protocolManager = new PacketFilterManager(getClassLoader(), logger); + protocolManager = new PacketFilterManager( + getClassLoader(), getServer().getScheduler(), logger); } @Override diff --git a/ProtocolLib/src/com/comphenix/protocol/async/AsyncFilterManager.java b/ProtocolLib/src/com/comphenix/protocol/async/AsyncFilterManager.java index 569e95d8..c10770b5 100644 --- a/ProtocolLib/src/com/comphenix/protocol/async/AsyncFilterManager.java +++ b/ProtocolLib/src/com/comphenix/protocol/async/AsyncFilterManager.java @@ -6,6 +6,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitScheduler; import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.PacketStream; @@ -35,20 +36,27 @@ public class AsyncFilterManager implements AsynchronousManager { // The likely main thread private Thread mainThread; + // Default scheduler + private BukkitScheduler scheduler; + // Current packet index private AtomicInteger currentSendingIndex = new AtomicInteger(); // Whether or not we're currently cleaning up private volatile boolean cleaningUp; - public AsyncFilterManager(Logger logger, PacketStream packetStream) { + public AsyncFilterManager(Logger logger, BukkitScheduler scheduler, PacketStream packetStream) { + + // Server packets are synchronized already this.serverQueue = new PacketSendingQueue(false); // Client packets must be synchronized this.clientQueue = new PacketSendingQueue(true); this.serverProcessingQueue = new PacketProcessingQueue(serverQueue); this.clientProcessingQueue = new PacketProcessingQueue(clientQueue); + this.packetStream = packetStream; + this.scheduler = scheduler; this.logger = logger; this.mainThread = Thread.currentThread(); @@ -158,6 +166,15 @@ public class AsyncFilterManager implements AsynchronousManager { return clientProcessingQueue.keySet(); } + /** + * Used to create a default asynchronous task. + * @param plugin - the calling plugin. + * @param runnable - the runnable. + */ + public void scheduleAsyncTask(Plugin plugin, Runnable runnable) { + scheduler.scheduleAsyncDelayedTask(plugin, runnable); + } + @Override public boolean hasAsynchronousListeners(PacketEvent packet) { return getProcessingQueue(packet).getListener(packet.getPacketID()).size() > 0; diff --git a/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java b/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java index 4ad4e3de..5687e8ca 100644 --- a/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java +++ b/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java @@ -8,6 +8,7 @@ import org.bukkit.plugin.Plugin; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketListener; +import com.sun.org.apache.bcel.internal.generic.GETSTATIC; /** * Represents a handler for an asynchronous event. @@ -180,4 +181,14 @@ public class AsyncListenerHandler { public Plugin getPlugin() { return listener != null ? listener.getPlugin() : null; } + + /** + * Start the asynchronous listener using the Bukkit scheduler. + */ + public void start() { + if (listener.getPlugin() == null) + throw new IllegalArgumentException("Cannot start task without a valid plugin."); + + filterManager.scheduleAsyncTask(listener.getPlugin(), getListenerLoop()); + } } diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java index cdcd0647..22a5b232 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java @@ -44,6 +44,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; +import org.bukkit.scheduler.BukkitScheduler; import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.ProtocolManager; @@ -121,7 +122,7 @@ public final class PacketFilterManager implements ProtocolManager { /** * Only create instances of this class if protocol lib is disabled. */ - public PacketFilterManager(ClassLoader classLoader, Logger logger) { + public PacketFilterManager(ClassLoader classLoader, BukkitScheduler scheduler, Logger logger) { if (logger == null) throw new IllegalArgumentException("logger cannot be NULL."); if (classLoader == null) @@ -132,7 +133,7 @@ public final class PacketFilterManager implements ProtocolManager { this.classLoader = classLoader; this.logger = logger; this.packetInjector = new PacketInjector(classLoader, this, connectionLookup); - this.asyncFilterManager = new AsyncFilterManager(logger, this); + this.asyncFilterManager = new AsyncFilterManager(logger, scheduler, this); } catch (IllegalAccessException e) { logger.log(Level.SEVERE, "Unable to initialize packet injector.", e); }