Either fixing or breaking mcchat

Made it a few days ago
Using a queue to send messages to Discord
This commit is contained in:
Norbi Peti 2018-01-03 19:36:11 +01:00
parent a4ca1bb6d3
commit ee2e979e12

View file

@ -2,6 +2,7 @@ package buttondevteam.discordplugin.listeners;
import java.awt.Color;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -12,6 +13,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitTask;
import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
@ -29,12 +31,20 @@ import sx.blah.discord.handle.obj.*;
import sx.blah.discord.util.*;
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
private BukkitTask sendtask;
private LinkedBlockingQueue<TBMCChatEvent> sendevents = new LinkedBlockingQueue<>();
@EventHandler // Minecraft
public void onMCChat(TBMCChatEvent e) {
if (e.isCancelled())
public void onMCChat(TBMCChatEvent ev) {
if (ev.isCancelled())
return;
Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
synchronized (this) {
sendevents.add(ev);
if (sendtask != null)
return;
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
try { // Runs forever - Not good, but most plugins don't support reloading the server anyways
while (true) {
val e = sendevents.take(); // Wait until an element is available
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " //
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
+ (DPUtils.sanitizeString(e.getSender() instanceof Player //
@ -83,8 +93,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if ((e.getChannel() == Channel.GlobalChat || e.getChannel().ID.equals("rp"))
&& isdifferentchannel.test(DiscordPlugin.chatchannel))
doit.accept(
lastmsgdata == null ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
doit.accept(lastmsgdata == null
? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
: lastmsgdata);
for (LastMsgData data : lastmsgPerUser) {
@ -93,6 +103,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
doit.accept(data);
}
}
} catch (Exception ex) {
TBMCCoreAPI.SendException("Error while sending mesasge to Discord!", ex);
}
});
}