Synchronously send broadcast messages to the game.

This commit is contained in:
Chris Naude 2016-03-11 10:53:40 -07:00
parent 2e9abd1a53
commit dfb22860c1
4 changed files with 119 additions and 2 deletions

View File

@ -53,7 +53,8 @@ public class MessageListener extends ListenerAdapter {
Channel channel = event.getChannel();
User user = event.getUser();
plugin.logDebug("Message caught <" + user.getNick() + ">: " + message);
plugin.logDebug("IRC message caught [channel: " + channel + "] [nick: " + user.getNick() + "] [[message: " + message + "]");
try {
if (plugin.shortifyHook != null && ircBot.isShortifyEnabled(channel.getName())) {
plugin.logDebug("Shortifying message (before): " + message);
@ -70,6 +71,7 @@ public class MessageListener extends ListenerAdapter {
}
} catch (Exception ex) {
plugin.logError("onMessage: " + ex.getMessage());
ex.printStackTrace();
}
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2014 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;
/**
*
* @author Chris Naude
*/
public class Message {
public String message;
public String permission;
public Message(String message, String permission) {
this.message = message;
this.permission = permission;
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2014 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 java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
*
* @author Chris Naude Poll the command queue and dispatch to Bukkit
*/
public class MessageQueueWatcher {
private final PurpleIRC plugin;
private int bt;
private final Queue<Message> queue = new ConcurrentLinkedQueue<>();
/**
*
* @param plugin the PurpleIRC plugin
*/
public MessageQueueWatcher(final PurpleIRC plugin) {
this.plugin = plugin;
startWatcher();
}
private void startWatcher() {
plugin.logDebug("Starting broadcast queue");
bt = this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
@Override
public void run() {
queueAndSend();
}
}, 0, 5);
}
private void queueAndSend() {
Message message = queue.poll();
if (message != null) {
plugin.getServer().broadcast(message.message, message.permission);
}
}
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 broadcast queue: " + size;
}
/**
* Add an IRCCommand to the command queue
*
* @param message
*/
public void add(Message message) {
plugin.logDebug("Adding message to broadcast queue: " + message.message);
queue.offer(message);
}
}

View File

@ -203,6 +203,7 @@ public class PurpleIRC extends JavaPlugin {
public IRCMessageHandler ircMessageHandler;
public CommandQueueWatcher commandQueue;
public MessageQueueWatcher messageQueue;
public UpdateChecker updateChecker;
public ChatTokenizer tokenizer;
private File heroConfigFile;
@ -333,6 +334,7 @@ public class PurpleIRC extends JavaPlugin {
botWatcher = new BotWatcher(this);
ircMessageHandler = new IRCMessageHandler(this);
commandQueue = new CommandQueueWatcher(this);
messageQueue = new MessageQueueWatcher(this);
updateChecker = new UpdateChecker(this);
}
@ -363,6 +365,7 @@ public class PurpleIRC extends JavaPlugin {
logInfo("Disconnecting IRC bots.");
for (PurpleBot ircBot : ircBots.values()) {
commandQueue.cancel();
messageQueue.cancel();
ircBot.stopTailers();
if (autoSave) {
ircBot.saveConfig(getServer().getConsoleSender());
@ -1751,7 +1754,7 @@ public class PurpleIRC extends JavaPlugin {
String fixedMessage = message.replace("\u200B", "");
if (broadcastChatToConsole) {
logDebug("Broadcast All [" + permission + "]: " + fixedMessage);
getServer().broadcast(fixedMessage, permission);
messageQueue.add(new Message(fixedMessage, permission));
} else {
logDebug("Broadcast Players [" + permission + "]: " + fixedMessage);
for (Player player : getServer().getOnlinePlayers()) {