Skip to content
This repository has been archived by the owner on Mar 16, 2018. It is now read-only.

Commit

Permalink
Update JavaCord & Sponge API
Browse files Browse the repository at this point in the history
- Get chat post-processing to allow tokens to be processed
- Use cause filter in listeners
  • Loading branch information
Mohron committed Sep 24, 2017
1 parent e900139 commit 080927d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 153 deletions.
30 changes: 8 additions & 22 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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'
}

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

Expand Down Expand Up @@ -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'
}
}
}

build.dependsOn(shadowJar)
13 changes: 9 additions & 4 deletions src/main/java/com/nguyenquyhy/discordbridge/DiscordBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand All @@ -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;
Expand All @@ -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> 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> 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<User> 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<Role> 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);*/
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,73 @@
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> 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);
}
}
}
}
}

@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> 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!");
}
}

}
}

0 comments on commit 080927d

Please sign in to comment.