Move IRCMessageEvent to synchronous queue

This commit is contained in:
cnaude 2019-04-28 15:57:56 -07:00
parent 0db1b2e9cd
commit c63f7626bc
4 changed files with 88 additions and 25 deletions

23
pom.xml
View File

@ -6,7 +6,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Bukkit API Version, change if out dated -->
<bukkit.version>1.13.2</bukkit.version>
<bukkit.version>1.14</bukkit.version>
<build.number>SNAPSHOT</build.number>
</properties>
@ -50,10 +50,10 @@
</repository>
<!-- Vault
<repository>
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository> -->
<repository>
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository> -->
<!--
<repository>
@ -158,22 +158,15 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<version>1.12</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.6</version>
</dependency>
<!-- Following dependency is provided by CraftBukkit -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- NTheEndAgain -->
<dependency>
<groupId>com.cnaude.nplugins</groupId>

View File

@ -42,12 +42,7 @@ public class CommandQueueWatcher {
private void startWatcher() {
plugin.logDebug("Starting command queue");
bt = this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
@Override
public void run() {
queueAndSend();
}
}, 0, 5);
bt = this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, this::queueAndSend, 0, 5);
}
private void queueAndSend() {

View File

@ -220,6 +220,7 @@ public class PurpleIRC extends JavaPlugin {
public IRCMessageHandler ircMessageHandler;
public CommandQueueWatcher commandQueue;
public SynchronousEventWatcher eventQueue;
public MessageQueueWatcher messageQueue;
public UpdateChecker updateChecker;
public ChatTokenizer tokenizer;
@ -372,6 +373,7 @@ public class PurpleIRC extends JavaPlugin {
commandQueue = new CommandQueueWatcher(this);
messageQueue = new MessageQueueWatcher(this);
updateChecker = new UpdateChecker(this);
eventQueue = new SynchronousEventWatcher(this);
}
/**
@ -1827,13 +1829,11 @@ public class PurpleIRC extends JavaPlugin {
}
public void broadcastToGame(final String message, final String channel, final String permission) {
IRCMessageEvent event = new IRCMessageEvent(message, channel, permission);
getServer().getPluginManager().callEvent(event);
eventQueue.add(new IRCMessageEvent(message, channel, permission));
}
public void broadcastToPlayer(final String message, final String channel, final String permission, final Player player) {
IRCMessageEvent event = new IRCMessageEvent(message, channel, permission, player);
getServer().getPluginManager().callEvent(event);
eventQueue.add(new IRCMessageEvent(message, channel, permission, player));
}
/**

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2019 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import com.cnaude.purpleirc.Events.IRCMessageEvent;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
*
* @author cnaude
*/
public class SynchronousEventWatcher {
private final PurpleIRC plugin;
private int bt;
private final Queue<IRCMessageEvent> queue = new ConcurrentLinkedQueue<>();
/**
*
* @param plugin the PurpleIRC plugin
*/
public SynchronousEventWatcher(final PurpleIRC plugin) {
this.plugin = plugin;
startWatcher();
}
private void startWatcher() {
plugin.logDebug("Starting synchronous event queue");
bt = this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, this::queueAndSend, 0, 5);
}
private void queueAndSend() {
IRCMessageEvent event = queue.poll();
if (event != null) {
plugin.getServer().getPluginManager().callEvent(event);
}
}
public void cancel() {
this.plugin.getServer().getScheduler().cancelTask(bt);
}
public String clearQueue() {
int size = queue.size();
if (!queue.isEmpty()) {
queue.clear();
}
return "Elements removed from event queue: " + size;
}
/**
* Add an IRCMessageEvent to the event queue
*
* @param event the IRC event to add to the queue
*/
public void add(IRCMessageEvent event) {
plugin.logDebug("Adding event to queue: " + event.getMessage());
queue.offer(event);
}
}