From a8f95bcb191b915b9ac1cccf92daad608235b81e Mon Sep 17 00:00:00 2001 From: Nathan Curran <30569566+Focusvity@users.noreply.github.com> Date: Fri, 16 Oct 2020 13:20:20 +1100 Subject: [PATCH] Use Component API for attachments --- .../discord/DiscordToMinecraftListener.java | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java index 5244eb1e..2f4754a4 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java @@ -7,8 +7,12 @@ import me.totalfreedom.totalfreedommod.rank.Title; import me.totalfreedom.totalfreedommod.util.FLog; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -20,7 +24,7 @@ public class DiscordToMinecraftListener extends ListenerAdapter String chat_channel_id = ConfigEntry.DISCORD_CHAT_CHANNEL_ID.getString(); if (event.getMember() != null && !chat_channel_id.isEmpty() && event.getChannel().getId().equals(chat_channel_id)) { - if (!event.getAuthor().getId().equals(Discord.bot.getSelfUser().getId()) && !event.getMessage().getContentDisplay().isEmpty()) + if (!event.getAuthor().getId().equals(Discord.bot.getSelfUser().getId())) { Member member = event.getMember(); String tag = getDisplay(member); @@ -29,15 +33,40 @@ public class DiscordToMinecraftListener extends ListenerAdapter { message += " " + tag; } - message += " " + ChatColor.RED + ChatColor.stripColor(member.getEffectiveName()) + ChatColor.DARK_GRAY + ": " + ChatColor.RESET + ChatColor.stripColor(event.getMessage().getContentDisplay()); - for (Player player : Bukkit.getOnlinePlayers()) + message += " " + ChatColor.RED + ChatColor.stripColor(member.getEffectiveName()) + ChatColor.DARK_GRAY + ": " + ChatColor.RESET; + if (!event.getMessage().getContentDisplay().isEmpty()) { - if (TotalFreedomMod.getPlugin().pl.getData(player).doesDisplayDiscord()) + message += ChatColor.stripColor(event.getMessage().getContentDisplay()); + for (Player player : Bukkit.getOnlinePlayers()) { - player.sendMessage(message); + if (TotalFreedomMod.getPlugin().pl.getData(player).doesDisplayDiscord()) + { + player.sendMessage(message); + } + } + FLog.info(message); + } + else + { + ComponentBuilder builder = new ComponentBuilder(message); + for (Message.Attachment attachment : event.getMessage().getAttachments()) + { + if (attachment.getUrl() == null) + { + continue; + } + TextComponent text = new TextComponent(net.md_5.bungee.api.ChatColor.YELLOW + "[Media]"); + text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, attachment.getUrl())); + builder.append(text).append(" "); + } + for (Player player : Bukkit.getOnlinePlayers()) + { + if (TotalFreedomMod.getPlugin().pl.getData(player).doesDisplayDiscord()) + { + player.spigot().sendMessage(builder.create()); + } } } - FLog.info(message); } } }