Separated waiting and not waiting msg sends

This commit is contained in:
Norbi Peti 2017-10-28 01:01:31 +02:00
parent 1244b50560
commit 4ada764a04
4 changed files with 44 additions and 19 deletions

View file

@ -128,7 +128,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
.withTitle("Server started - chat connected.").build()); .withTitle("Server started - chat connected.").build());
getConfig().set("serverup", true); getConfig().set("serverup", true);
saveConfig(); saveConfig();
perform(() -> { performNoWait(() -> {
try { try {
List<IMessage> msgs = genchannel.getPinnedMessages(); List<IMessage> msgs = genchannel.getPinnedMessages();
for (int i = msgs.size() - 1; i >= 10; i--) { // Unpin all pinned messages except the newest 10 for (int i = msgs.size() - 1; i >= 10; i--) { // Unpin all pinned messages except the newest 10
@ -262,7 +262,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
} }
} }
if (msgsb.length() > 0) if (msgsb.length() > 0)
genchannel.pin(sendMessageToChannel(genchannel, msgsb.toString())); genchannel.pin(sendMessageToChannelWait(genchannel, msgsb.toString()));
if (modmsgsb.length() > 0) if (modmsgsb.length() > 0)
sendMessageToChannel(annchannel, modmsgsb.toString()); sendMessageToChannel(annchannel, modmsgsb.toString());
if (lastannouncementtime != lastanntime) { if (lastannouncementtime != lastanntime) {
@ -282,11 +282,23 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
} }
} }
public static IMessage sendMessageToChannel(IChannel channel, String message) { public static void sendMessageToChannel(IChannel channel, String message) {
return sendMessageToChannel(channel, message, null); sendMessageToChannel(channel, message, null);
} }
public static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed) { public static void sendMessageToChannel(IChannel channel, String message, EmbedObject embed) {
sendMessageToChannel(channel, message, null, false);
}
public static IMessage sendMessageToChannelWait(IChannel channel, String message) {
return sendMessageToChannelWait(channel, message, null);
}
public static IMessage sendMessageToChannelWait(IChannel channel, String message, EmbedObject embed) {
return sendMessageToChannel(channel, message, null, true);
}
private static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed, boolean wait) {
if (message.length() > 1900) { if (message.length() > 1900) {
message = message.substring(0, 1900); message = message.substring(0, 1900);
Bukkit.getLogger() Bukkit.getLogger()
@ -298,8 +310,14 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
else if (channel.isPrivate()) else if (channel.isPrivate())
MCChatListener.resetLastMessage(channel); MCChatListener.resetLastMessage(channel);
final String content = message; final String content = message;
return perform( RequestBuffer.IRequest<IMessage> r = () -> embed == null ? channel.sendMessage(content)
() -> embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false)); : channel.sendMessage(content, embed, false);
if (wait)
return perform(r);
else {
performNoWait(r);
return null;
}
} catch (Exception e) { } catch (Exception e) {
Bukkit.getLogger().warning( Bukkit.getLogger().warning(
"Failed to deliver message to Discord! Channel: " + channel.getName() + " Message: " + message); "Failed to deliver message to Discord! Channel: " + channel.getName() + " Message: " + message);
@ -360,8 +378,20 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
return RequestBuffer.request(action).get(); // Let the pros handle this return RequestBuffer.request(action).get(); // Let the pros handle this
} }
public static void performNoWait(IVoidRequest action) {
if (SafeMode)
return;
RequestBuffer.request(action);
}
public static <T> void performNoWait(IRequest<T> action) {
if (SafeMode)
return;
RequestBuffer.request(action);
}
public static void updatePlayerList() { public static void updatePlayerList() {
perform(() -> { performNoWait(() -> {
String[] s = chatchannel.getTopic().split("\\n----\\n"); String[] s = chatchannel.getTopic().split("\\n----\\n");
if (s.length < 3) if (s.length < 3)
return; return;

View file

@ -13,7 +13,7 @@ public class AutoUpdaterListener implements Listener {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
try { try {
DiscordPlugin.perform(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream() DiscordPlugin.performNoWait(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream()
.filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle() .filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle()
.contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString())) .contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString()))
.findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION)); .findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION));

View file

@ -62,7 +62,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|| !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName()) || !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName())
|| lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120 || lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120
|| !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) { || !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) {
lastmsgdata.message = DiscordPlugin.sendMessageToChannel(lastmsgdata.channel, dmsg, lastmsgdata.message = DiscordPlugin.sendMessageToChannelWait(lastmsgdata.channel, dmsg,
embedObject); embedObject);
lastmsgdata.time = nanoTime; lastmsgdata.time = nanoTime;
lastmsgdata.mcchannel = e.getChannel(); lastmsgdata.mcchannel = e.getChannel();
@ -87,12 +87,6 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null) : lastmsgdata); ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null) : lastmsgdata);
for (LastMsgData data : lastmsgPerUser) { for (LastMsgData data : lastmsgPerUser) {
// System.out.println("Data: " + data);
// System.out.println("Data.channel: " + data.channel);
// System.out.println("Sender: " + e.getSender());
// System.out.println("Sender channel: " + ((DiscordSenderBase) e.getSender()).getChannel()); // TODO
// System.out.println("Predicate: " + isdifferentchannel);
// System.out.println("DP: " + data.dp); - Didn't update the constructor
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);
@ -337,12 +331,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
try { try {
final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION); final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION);
if (reaction != null) if (reaction != null)
DiscordPlugin.perform(() -> m.removeReaction(DiscordPlugin.dc.getOurUser(), reaction)); DiscordPlugin
.performNoWait(() -> m.removeReaction(DiscordPlugin.dc.getOurUser(), reaction));
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e); TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
} }
}); });
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION)); DiscordPlugin.performNoWait(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
} }
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e); TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);

View file

@ -124,7 +124,7 @@ public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerMute(MuteStatusChangeEvent e) { public void onPlayerMute(MuteStatusChangeEvent e) {
try { try {
DiscordPlugin.perform(() -> { DiscordPlugin.performNoWait(() -> {
final IRole role = DiscordPlugin.dc.getRoleByID(164090010461667328L); final IRole role = DiscordPlugin.dc.getRoleByID(164090010461667328L);
final CommandSource source = e.getAffected().getSource(); final CommandSource source = e.getAffected().getSource();
if (!source.isPlayer()) if (!source.isPlayer())