From 080927daa4b369714dc063f4e0d8fa8fa9f0e958 Mon Sep 17 00:00:00 2001 From: Mohron Date: Sun, 24 Sep 2017 15:13:49 -0500 Subject: [PATCH] Update JavaCord & Sponge API - Get chat post-processing to allow tokens to be processed - Use cause filter in listeners --- build.gradle | 30 ++--- .../discordbridge/DiscordBridge.java | 13 +- .../discordbridge/listeners/ChatListener.java | 121 ++++++------------ .../listeners/ClientConnectionListener.java | 85 ++++++------ 4 files changed, 96 insertions(+), 153 deletions(-) diff --git a/build.gradle b/build.gradle index b799bce..83c00ba 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { - id "com.qixalite.spongestart" version "1.5.2" id 'java' + id "com.qixalite.spongestart" version "1.5.2" id 'com.github.johnrengelman.shadow' version '1.2.3' //id 'org.spongepowered.plugin' version '0.5.1' } @@ -8,7 +8,7 @@ plugins { defaultTasks 'build', 'shadowJar' group = 'com.nguyenquyhy' -version = '2.4.0' +version = '2.4.1' description = 'A plugin to connect Discord and Minecraft' sourceCompatibility = 1.8 @@ -40,35 +40,21 @@ spongestart{ } dependencies { - compile('org.spongepowered:spongeapi:4.1.0') - //compile('org.spongepowered:spongeapi:5.0.0') - compile files('libs/javacord-2.0.14-shaded.jar') -// compile('de.btobastian.javacord:javacord:2.0.11:shaded') -// compile "com.github.austinv11:Discord4j:2.4.9:shaded" -// compile 'org.apache.httpcomponents:httpcore:4.3.2' -// compile 'org.apache.httpcomponents:httpclient:4.3.3' -// compile 'org.java-websocket:Java-WebSocket:1.3.0' -// compile 'com.googlecode.json-simple:json-simple:1.1.1' -// compile 'net.jodah:typetools:0.4.3' -} - -task fatJar(type: Jar) { - baseName = project.name + '-all' - from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } - with jar + compile('org.spongepowered:spongeapi:5.1.0') + compile('de.btobastian.javacord:javacord:2.0.17:shaded') } shadowJar { dependencies { + include dependency('de.btobastian.javacord:javacord:2.0.17:shaded') include dependency('commons-logging:commons-logging') include dependency('org.apache.commons:commons-lang3') include dependency('org.apache.httpcomponents:httpcore') include dependency('org.apache.httpcomponents:httpclient') -// include dependency('com.googlecode.json-simple:json-simple') -// include dependency('org.java-websocket:Java-WebSocket') -// include dependency('net.jodah:typetools') relocate 'org.apache.http', 'shaded.apache.http' relocate 'org.apache.commons', 'shaded.apache.commons' } -} \ No newline at end of file +} + +build.dependsOn(shadowJar) \ No newline at end of file diff --git a/src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java b/src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java index 5b3bacc..f7c7ac1 100644 --- a/src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java +++ b/src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java @@ -32,8 +32,13 @@ /** * Created by Hy on 1/4/2016. */ -@Plugin(id = "discordbridge", name = "Discord Bridge", version = "2.3.0", - description = "A Sponge plugin to connect your Minecraft server with Discord", authors = {"Hy", "Mohron"}) +@Plugin( + id = "discordbridge", + name = "Discord Bridge", + version = "2.4.1", + description = "A Sponge plugin to connect your Minecraft server with Discord", + authors = {"Hy", "Mohron"} +) public class DiscordBridge { private DiscordAPI consoleClient = null; @@ -79,8 +84,8 @@ public void onServerStop(GameStoppingServerEvent event) { if (botClient != null) { for (ChannelConfig channelConfig : config.channels) { if (StringUtils.isNotBlank(channelConfig.discordId) - && channelConfig.discord != null - && StringUtils.isNotBlank(channelConfig.discord.serverDownMessage)) { + && channelConfig.discord != null + && StringUtils.isNotBlank(channelConfig.discord.serverDownMessage)) { Channel channel = botClient.getChannelById(channelConfig.discordId); if (channel != null) { ChannelUtil.sendMessage(channel, channelConfig.discord.serverDownMessage); diff --git a/src/main/java/com/nguyenquyhy/discordbridge/listeners/ChatListener.java b/src/main/java/com/nguyenquyhy/discordbridge/listeners/ChatListener.java index c71ae2e..3f7a1bd 100644 --- a/src/main/java/com/nguyenquyhy/discordbridge/listeners/ChatListener.java +++ b/src/main/java/com/nguyenquyhy/discordbridge/listeners/ChatListener.java @@ -12,17 +12,17 @@ import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Order; +import org.spongepowered.api.event.filter.cause.First; import org.spongepowered.api.event.message.MessageChannelEvent; -import org.spongepowered.api.text.Text; import org.spongepowered.api.text.channel.MessageChannel; -import java.util.Optional; import java.util.UUID; /** * Created by Hy on 10/13/2016. */ public class ChatListener { + DiscordBridge mod = DiscordBridge.getInstance(); /** @@ -31,15 +31,10 @@ public class ChatListener { * @param event */ @Listener(order = Order.LATE) - public void onChat(MessageChannelEvent.Chat event) { - - if (event.isCancelled() || event.isMessageCancelled()) return; + public void onChat(MessageChannelEvent.Chat event, @First Player player) { - sendToDiscord(event); - formatForMinecraft(event); - } + if (event.isMessageCancelled()) return; - private void sendToDiscord(MessageChannelEvent.Chat event) { GlobalConfig config = mod.getConfig(); boolean isStaffChat = false; @@ -54,95 +49,57 @@ else if (!channel.getClass().getName().startsWith("org.spongepowered.api.text.ch String plainString = event.getRawMessage().toPlain().trim(); if (StringUtils.isBlank(plainString) || plainString.startsWith("/")) return; - plainString = TextUtil.formatMinecraftMessage(plainString); - Optional player = event.getCause().first(Player.class); + // Replace with processed message body - Allows token processing to occur + plainString = event.getFormatter().getBody().toText().toPlain().trim(); - if (player.isPresent()) { - UUID playerId = player.get().getUniqueId(); + plainString = TextUtil.formatMinecraftMessage(plainString); - DiscordAPI client = mod.getBotClient(); - boolean isBotAccount = true; - if (mod.getHumanClients().containsKey(playerId)) { - client = mod.getHumanClients().get(playerId); - isBotAccount = false; - } + DiscordAPI client = mod.getBotClient(); + boolean isBotAccount = true; + if (mod.getHumanClients().containsKey(player.getUniqueId())) { + client = mod.getHumanClients().get(player.getUniqueId()); + isBotAccount = false; + } - if (client != null) { - for (ChannelConfig channelConfig : config.channels) { - if (StringUtils.isNotBlank(channelConfig.discordId) && channelConfig.discord != null) { - String template = null; - if (!isStaffChat && channelConfig.discord.publicChat != null) { - template = isBotAccount ? channelConfig.discord.publicChat.anonymousChatTemplate : channelConfig.discord.publicChat.authenticatedChatTemplate; - } else if (isStaffChat && channelConfig.discord.staffChat != null) { - template = isBotAccount ? channelConfig.discord.staffChat.anonymousChatTemplate : channelConfig.discord.staffChat.authenticatedChatTemplate; - } + if (client != null) { + for (ChannelConfig channelConfig : config.channels) { + if (StringUtils.isNotBlank(channelConfig.discordId) && channelConfig.discord != null) { + String template = null; + if (!isStaffChat && channelConfig.discord.publicChat != null) { + template = isBotAccount ? channelConfig.discord.publicChat.anonymousChatTemplate : channelConfig.discord.publicChat.authenticatedChatTemplate; + } else if (isStaffChat && channelConfig.discord.staffChat != null) { + template = isBotAccount ? channelConfig.discord.staffChat.anonymousChatTemplate : channelConfig.discord.staffChat.authenticatedChatTemplate; + } - if (StringUtils.isNotBlank(template)) { - Channel channel = client.getChannelById(channelConfig.discordId); + if (StringUtils.isNotBlank(template)) { + Channel channel = client.getChannelById(channelConfig.discordId); - if (channel == null) { - ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); - return; - } + if (channel == null) { + ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); + return; + } - // Format Mentions for Discord - plainString = TextUtil.formatMinecraftMention(plainString, channel.getServer(), player.get(), isBotAccount); + // Format Mentions for Discord + plainString = TextUtil.formatMinecraftMention(plainString, channel.getServer(), player, isBotAccount); - if (isBotAccount) { + if (isBotAccount) { // if (channel == null) { // LoginHandler.loginBotAccount(); // } - String content = String.format( - template.replace("%a", - TextUtil.escapeForDiscord(player.get().getName(), template, "%a")), - plainString); - ChannelUtil.sendMessage(channel, content); - } else { + String content = String.format( + template.replace("%a", + TextUtil.escapeForDiscord(player.getName(), template, "%a")), + plainString); + ChannelUtil.sendMessage(channel, content); + } else { // if (channel == null) { // LoginHandler.loginHumanAccount(player.get()); // } - ChannelUtil.sendMessage(channel, String.format(template, plainString)); - } + ChannelUtil.sendMessage(channel, String.format(template, plainString)); } } } } } } - - private void formatForMinecraft(MessageChannelEvent.Chat event) { - Text rawMessage = event.getRawMessage(); - Optional player = event.getCause().first(Player.class); - - if (player.isPresent()) { -/* UUID playerId = player.get().getUniqueId(); - - for (ChannelConfig channelConfig : config.channels) { - String template = null; - - Channel channel = client.getChannelById(channelConfig.discordId); - - Optional userOptional = DiscordUtil.getUserByName(player.get().getName(), channel.getServer()); - if (userOptional.isPresent()) { - User user = userOptional.get(); - } - - ChannelMinecraftConfigCore minecraftConfig = channelConfig.minecraft; - if (channelConfig.minecraft.roles != null) { - Collection roles = message.getAuthor().getRoles(message.getChannelReceiver().getServer()); - for (String roleName : channelConfig.minecraft.roles.keySet()) { - if (roles.stream().anyMatch(r -> r.getName().equals(roleName))) { - ChannelMinecraftConfigCore roleConfig = channelConfig.minecraft.roles.get(roleName); - roleConfig.inherit(channelConfig.minecraft); - minecraftConfig = roleConfig; - break; - } - } - } - } - - event.setMessage(rawMessage);*/ - } - - } -} \ No newline at end of file +} diff --git a/src/main/java/com/nguyenquyhy/discordbridge/listeners/ClientConnectionListener.java b/src/main/java/com/nguyenquyhy/discordbridge/listeners/ClientConnectionListener.java index cf21804..0d8d1fe 100644 --- a/src/main/java/com/nguyenquyhy/discordbridge/listeners/ClientConnectionListener.java +++ b/src/main/java/com/nguyenquyhy/discordbridge/listeners/ClientConnectionListener.java @@ -12,42 +12,39 @@ import org.apache.commons.lang3.StringUtils; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.filter.cause.First; import org.spongepowered.api.event.network.ClientConnectionEvent; -import java.util.Optional; import java.util.UUID; /** * Created by Hy on 10/13/2016. */ public class ClientConnectionListener { + @Listener - public void onJoin(ClientConnectionEvent.Join event) { + public void onJoin(ClientConnectionEvent.Join event, @First Player player) { DiscordBridge mod = DiscordBridge.getInstance(); GlobalConfig config = mod.getConfig(); - Optional player = event.getCause().first(Player.class); - if (player.isPresent()) { - UUID playerId = player.get().getUniqueId(); - boolean loggingIn = false; - if (!mod.getHumanClients().containsKey(playerId)) { - loggingIn = LoginHandler.loginHumanAccount(player.get()); - } + boolean loggingIn = false; + if (!mod.getHumanClients().containsKey(player.getUniqueId())) { + loggingIn = LoginHandler.loginHumanAccount(player); + } - if (!loggingIn && mod.getBotClient() != null) { - // Use Bot client to send joined message - for (ChannelConfig channelConfig : config.channels) { - if (StringUtils.isNotBlank(channelConfig.discordId) - && channelConfig.discord != null - && StringUtils.isNotBlank(channelConfig.discord.joinedTemplate)) { - Channel channel = mod.getBotClient().getChannelById(channelConfig.discordId); - if (channel != null) { - String content = String.format(channelConfig.discord.joinedTemplate, - TextUtil.escapeForDiscord(player.get().getName(), channelConfig.discord.joinedTemplate, "%s")); - ChannelUtil.sendMessage(channel, content); - } else { - ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); - } + if (!loggingIn && mod.getBotClient() != null) { + // Use Bot client to send joined message + for (ChannelConfig channelConfig : config.channels) { + if (StringUtils.isNotBlank(channelConfig.discordId) + && channelConfig.discord != null + && StringUtils.isNotBlank(channelConfig.discord.joinedTemplate)) { + Channel channel = mod.getBotClient().getChannelById(channelConfig.discordId); + if (channel != null) { + String content = String.format(channelConfig.discord.joinedTemplate, + TextUtil.escapeForDiscord(player.getName(), channelConfig.discord.joinedTemplate, "%s")); + ChannelUtil.sendMessage(channel, content); + } else { + ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); } } } @@ -55,35 +52,33 @@ public void onJoin(ClientConnectionEvent.Join event) { } @Listener - public void onDisconnect(ClientConnectionEvent.Disconnect event) { + public void onDisconnect(ClientConnectionEvent.Disconnect event, @First Player player) { DiscordBridge mod = DiscordBridge.getInstance(); GlobalConfig config = mod.getConfig(); - Optional player = event.getCause().first(Player.class); - if (player.isPresent()) { - UUID playerId = player.get().getUniqueId(); + UUID playerId = player.getUniqueId(); - DiscordAPI client = mod.getHumanClients().get(playerId); - if (client == null) client = mod.getBotClient(); - if (client != null) { - for (ChannelConfig channelConfig : config.channels) { - if (StringUtils.isNotBlank(channelConfig.discordId) - && channelConfig.discord != null - && StringUtils.isNotBlank(channelConfig.discord.leftTemplate)) { - Channel channel = client.getChannelById(channelConfig.discordId); - if (channel != null) { - String content = String.format(channelConfig.discord.leftTemplate, - TextUtil.escapeForDiscord(player.get().getName(), channelConfig.discord.leftTemplate, "%s")); - ChannelUtil.sendMessage(channel, content); - } else { - ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); - } + DiscordAPI client = mod.getHumanClients().get(playerId); + if (client == null) client = mod.getBotClient(); + if (client != null) { + for (ChannelConfig channelConfig : config.channels) { + if (StringUtils.isNotBlank(channelConfig.discordId) + && channelConfig.discord != null + && StringUtils.isNotBlank(channelConfig.discord.leftTemplate)) { + Channel channel = client.getChannelById(channelConfig.discordId); + if (channel != null) { + String content = String.format(channelConfig.discord.leftTemplate, + TextUtil.escapeForDiscord(player.getName(), channelConfig.discord.leftTemplate, "%s")); + ChannelUtil.sendMessage(channel, content); + } else { + ErrorMessages.CHANNEL_NOT_FOUND.log(channelConfig.discordId); } - mod.removeAndLogoutClient(playerId); - //unauthenticatedPlayers.remove(playerId); - mod.getLogger().info(player.get().getName() + " has disconnected!"); } + mod.removeAndLogoutClient(playerId); + //unauthenticatedPlayers.remove(playerId); + mod.getLogger().info(player.getName() + " has disconnected!"); } } + } }