Either fixing or breaking mcchat
Made it a few days ago Using a queue to send messages to Discord
This commit is contained in:
parent
a4ca1bb6d3
commit
ee2e979e12
1 changed files with 71 additions and 58 deletions
|
@ -2,6 +2,7 @@ package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -12,6 +13,7 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.*;
|
import buttondevteam.discordplugin.*;
|
||||||
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
||||||
|
@ -29,69 +31,80 @@ import sx.blah.discord.handle.obj.*;
|
||||||
import sx.blah.discord.util.*;
|
import sx.blah.discord.util.*;
|
||||||
|
|
||||||
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
||||||
|
private BukkitTask sendtask;
|
||||||
|
private LinkedBlockingQueue<TBMCChatEvent> sendevents = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
@EventHandler // Minecraft
|
@EventHandler // Minecraft
|
||||||
public void onMCChat(TBMCChatEvent e) {
|
public void onMCChat(TBMCChatEvent ev) {
|
||||||
if (e.isCancelled())
|
if (ev.isCancelled())
|
||||||
return;
|
return;
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
|
sendevents.add(ev);
|
||||||
synchronized (this) {
|
if (sendtask != null)
|
||||||
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " //
|
return;
|
||||||
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
|
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
|
||||||
+ (DPUtils.sanitizeString(e.getSender() instanceof Player //
|
try { // Runs forever - Not good, but most plugins don't support reloading the server anyways
|
||||||
? ((Player) e.getSender()).getDisplayName() //
|
while (true) {
|
||||||
: e.getSender().getName()));
|
val e = sendevents.take(); // Wait until an element is available
|
||||||
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer)
|
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " //
|
||||||
.withDescription(e.getMessage()).withColor(new Color(e.getChannel().color.getRed(),
|
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
|
||||||
e.getChannel().color.getGreen(), e.getChannel().color.getBlue()));
|
+ (DPUtils.sanitizeString(e.getSender() instanceof Player //
|
||||||
// embed.appendField("Channel", ((e.getSender() instanceof DiscordSenderBase ? "d|" : "")
|
? ((Player) e.getSender()).getDisplayName() //
|
||||||
// + DiscordPlugin.sanitizeString(e.getChannel().DisplayName)), false);
|
: e.getSender().getName()));
|
||||||
if (e.getSender() instanceof Player)
|
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer)
|
||||||
DPUtils.embedWithHead(
|
.withDescription(e.getMessage()).withColor(new Color(e.getChannel().color.getRed(),
|
||||||
embed.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=minecraft&id="
|
e.getChannel().color.getGreen(), e.getChannel().color.getBlue()));
|
||||||
+ ((Player) e.getSender()).getUniqueId()),
|
// embed.appendField("Channel", ((e.getSender() instanceof DiscordSenderBase ? "d|" : "")
|
||||||
((Player) e.getSender()).getName());
|
// + DiscordPlugin.sanitizeString(e.getChannel().DisplayName)), false);
|
||||||
else if (e.getSender() instanceof DiscordSenderBase)
|
if (e.getSender() instanceof Player)
|
||||||
embed.withAuthorIcon(((DiscordSenderBase) e.getSender()).getUser().getAvatarURL())
|
DPUtils.embedWithHead(
|
||||||
.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=discord&id="
|
embed.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=minecraft&id="
|
||||||
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
|
+ ((Player) e.getSender()).getUniqueId()),
|
||||||
// embed.withFooterText(e.getChannel().DisplayName);
|
((Player) e.getSender()).getName());
|
||||||
final long nanoTime = System.nanoTime();
|
else if (e.getSender() instanceof DiscordSenderBase)
|
||||||
Consumer<LastMsgData> doit = lastmsgdata -> {
|
embed.withAuthorIcon(((DiscordSenderBase) e.getSender()).getUser().getAvatarURL())
|
||||||
final EmbedObject embedObject = embed.build();
|
.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=discord&id="
|
||||||
if (lastmsgdata.message == null || lastmsgdata.message.isDeleted()
|
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
|
||||||
|| !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName())
|
// embed.withFooterText(e.getChannel().DisplayName);
|
||||||
|| lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120
|
final long nanoTime = System.nanoTime();
|
||||||
|| !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) {
|
Consumer<LastMsgData> doit = lastmsgdata -> {
|
||||||
lastmsgdata.message = DiscordPlugin.sendMessageToChannelWait(lastmsgdata.channel, "",
|
final EmbedObject embedObject = embed.build();
|
||||||
embedObject); // TODO Use ChromaBot API
|
if (lastmsgdata.message == null || lastmsgdata.message.isDeleted()
|
||||||
lastmsgdata.time = nanoTime;
|
|| !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName())
|
||||||
lastmsgdata.mcchannel = e.getChannel();
|
|| lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120
|
||||||
lastmsgdata.content = embedObject.description;
|
|| !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) {
|
||||||
} else
|
lastmsgdata.message = DiscordPlugin.sendMessageToChannelWait(lastmsgdata.channel, "",
|
||||||
try {
|
embedObject); // TODO Use ChromaBot API
|
||||||
lastmsgdata.content = embedObject.description = lastmsgdata.content + "\n"
|
lastmsgdata.time = nanoTime;
|
||||||
+ embedObject.description;// The message object doesn't get updated
|
lastmsgdata.mcchannel = e.getChannel();
|
||||||
final LastMsgData _lastmsgdata = lastmsgdata;
|
lastmsgdata.content = embedObject.description;
|
||||||
DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
|
} else
|
||||||
} catch (MissingPermissionsException | DiscordException e1) {
|
try {
|
||||||
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
lastmsgdata.content = embedObject.description = lastmsgdata.content + "\n"
|
||||||
}
|
+ embedObject.description;// The message object doesn't get updated
|
||||||
};
|
final LastMsgData _lastmsgdata = lastmsgdata;
|
||||||
// Checks if the given channel is different than where the message was sent from
|
DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
|
||||||
Predicate<IChannel> isdifferentchannel = ch -> !(e.getSender() instanceof DiscordSenderBase)
|
} catch (MissingPermissionsException | DiscordException e1) {
|
||||||
|| ((DiscordSenderBase) e.getSender()).getChannel().getLongID() != ch.getLongID();
|
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Checks if the given channel is different than where the message was sent from
|
||||||
|
Predicate<IChannel> isdifferentchannel = ch -> !(e.getSender() instanceof DiscordSenderBase)
|
||||||
|
|| ((DiscordSenderBase) e.getSender()).getChannel().getLongID() != ch.getLongID();
|
||||||
|
|
||||||
if ((e.getChannel() == Channel.GlobalChat || e.getChannel().ID.equals("rp"))
|
if ((e.getChannel() == Channel.GlobalChat || e.getChannel().ID.equals("rp"))
|
||||||
&& isdifferentchannel.test(DiscordPlugin.chatchannel))
|
&& isdifferentchannel.test(DiscordPlugin.chatchannel))
|
||||||
doit.accept(
|
doit.accept(lastmsgdata == null
|
||||||
lastmsgdata == null ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
|
? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
|
||||||
: lastmsgdata);
|
: lastmsgdata);
|
||||||
|
|
||||||
for (LastMsgData data : lastmsgPerUser) {
|
for (LastMsgData data : lastmsgPerUser) {
|
||||||
if (data.dp.isMinecraftChatEnabled() && isdifferentchannel.test(data.channel)
|
if (data.dp.isMinecraftChatEnabled() && isdifferentchannel.test(data.channel)
|
||||||
&& e.shouldSendTo(getSender(data.channel, data.user, data.dp)))
|
&& e.shouldSendTo(getSender(data.channel, data.user, data.dp)))
|
||||||
doit.accept(data);
|
doit.accept(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
TBMCCoreAPI.SendException("Error while sending mesasge to Discord!", ex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue