mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-27 21:26:17 +01:00
Simplify the act of creating an asynchronous listener.
This commit is contained in:
parent
63197bbbd5
commit
711990fa15
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user