Search in sources :

Example 1 with DiscordMessageContent

use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method onAdvancementSend.

@Subscribe(priority = ListenerPriority.HIGHEST)
public void onAdvancementSend(AchievementMessagePostProcessEvent event) {
    if (event.isCancelled()) {
        return;
    }
    Debug.debug("Triggered onAdvancementSend");
    Message message = event.getDiscordMessage();
    if (!message.getContentRaw().contains("<ICA=")) {
        return;
    }
    String text = message.getContentRaw();
    Set<Integer> matches = new LinkedHashSet<>();
    synchronized (RESEND_WITH_ATTACHMENT) {
        for (int key : RESEND_WITH_ATTACHMENT.keySet()) {
            if (text.contains("<ICA=" + key + ">")) {
                matches.add(key);
            }
        }
    }
    event.setCancelled(true);
    DiscordMessageContent content = new DiscordMessageContent(message);
    for (int key : matches) {
        AttachmentData data = RESEND_WITH_ATTACHMENT.remove(key);
        if (data != null) {
            content.addAttachment(data.getName(), data.getData());
        }
    }
    TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getChannel());
    Debug.debug("onAdvancementSend sending message to discord");
    if (event.isUsingWebhooks()) {
        String webHookUrl = WebhookUtil.getWebhookUrlToUseForChannel(destinationChannel);
        WebhookClient client = WebhookClient.withUrl(webHookUrl);
        if (client == null) {
            throw new NullPointerException("Unable to get the Webhook client URL for the TextChannel " + destinationChannel.getName());
        }
        client.send(content.toWebhookMessageBuilder().setUsername(event.getWebhookName()).setAvatarUrl(event.getWebhookAvatarUrl()).build());
        client.close();
    } else {
        content.toJDAMessageRestAction(destinationChannel).queue();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) WebhookClient(club.minnced.discord.webhook.WebhookClient) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) AttachmentData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Example 2 with DiscordMessageContent

use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method onDeathMessageSend.

@Subscribe(priority = ListenerPriority.HIGHEST)
public void onDeathMessageSend(DeathMessagePostProcessEvent event) {
    if (event.isCancelled()) {
        return;
    }
    if (!InteractiveChatDiscordSrvAddon.plugin.deathMessageItem) {
        return;
    }
    Debug.debug("Triggered onDeathMessageSend");
    ItemStack item = DEATH_BY.remove(event.getPlayer().getUniqueId());
    if (item == null || item.getType().equals(Material.AIR)) {
        return;
    }
    if (!item.hasItemMeta()) {
        return;
    }
    ItemMeta meta = item.getItemMeta();
    if (meta == null || !meta.hasDisplayName() || meta.getDisplayName().length() <= 0) {
        return;
    }
    Color color = null;
    if (!event.getDiscordMessage().getEmbeds().isEmpty()) {
        color = event.getDiscordMessage().getEmbeds().get(0).getColor();
    }
    if (color == null) {
        color = Color.black;
    }
    Player player = event.getPlayer();
    DiscordMessageContent content = new DiscordMessageContent(ChatColorUtils.stripColor(meta.getDisplayName()), "attachment://Item.png", color);
    try {
        BufferedImage image = ImageGeneration.getItemStackImage(item, ICPlayerFactory.getICPlayer(player), InteractiveChatDiscordSrvAddon.plugin.itemAltAir);
        byte[] itemData = ImageUtils.toArray(image);
        DiscordDescription description = DiscordItemStackUtils.getDiscordDescription(item, player);
        content.addAttachment("Item.png", itemData);
        if (InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImage) {
            DiscordToolTip discordToolTip = DiscordItemStackUtils.getToolTip(item, player);
            if (!discordToolTip.isBaseItem() || InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImageOnBaseItem) {
                BufferedImage tooltip = ImageGeneration.getToolTipImage(discordToolTip.getComponents());
                byte[] tooltipData = ImageUtils.toArray(tooltip);
                content.addAttachment("ToolTip.png", tooltipData);
                content.addImageUrl("attachment://ToolTip.png");
            } else {
                content.addDescription(description.getDescription().orElse(null));
            }
        } else {
            content.addDescription(description.getDescription().orElse(null));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> {
        Debug.debug("onDeathMessageSend sending item to discord");
        TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getChannel());
        if (event.isUsingWebhooks()) {
            String webHookUrl = WebhookUtil.getWebhookUrlToUseForChannel(destinationChannel);
            WebhookClient client = WebhookClient.withUrl(webHookUrl);
            if (client == null) {
                throw new NullPointerException("Unable to get the Webhook client URL for the TextChannel " + destinationChannel.getName());
            }
            client.send(content.toWebhookMessageBuilder().setUsername(event.getWebhookName()).setAvatarUrl(event.getWebhookAvatarUrl()).build());
            client.close();
        } else {
            content.toJDAMessageRestAction(destinationChannel).queue();
        }
    }, 5);
}
Also used : ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) Player(org.bukkit.entity.Player) MineverseChatPlayer(mineverse.Aust1n46.chat.api.MineverseChatPlayer) DiscordDescription(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordDescription) WebhookClient(club.minnced.discord.webhook.WebhookClient) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) Color(java.awt.Color) DiscordToolTip(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Example 3 with DiscordMessageContent

use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method discordMessageSent.

// =====
@Subscribe(priority = ListenerPriority.HIGHEST)
public void discordMessageSent(DiscordGuildMessageSentEvent event) {
    Debug.debug("Triggered discordMessageSent");
    Message message = event.getMessage();
    String textOriginal = message.getContentRaw();
    TextChannel channel = event.getChannel();
    if (!InteractiveChatDiscordSrvAddon.plugin.isEnabled()) {
        return;
    }
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChatDiscordSrvAddon.plugin, () -> {
        String text = textOriginal;
        if (!text.contains("<ICD=")) {
            return;
        }
        Set<Integer> matches = new LinkedHashSet<>();
        synchronized (DATA) {
            for (int key : DATA.keySet()) {
                if (text.contains("<ICD=" + key + ">")) {
                    text = text.replace("<ICD=" + key + ">", "");
                    matches.add(key);
                }
            }
        }
        if (matches.isEmpty()) {
            Debug.debug("discordMessageSent keys empty");
            return;
        }
        message.editMessage(text + " ...").queue();
        ICPlayer player = DATA.get(matches.iterator().next()).getPlayer();
        List<DiscordDisplayData> dataList = new ArrayList<>();
        for (int key : matches) {
            DiscordDisplayData data = DATA.remove(key);
            if (data != null) {
                dataList.add(data);
            }
        }
        dataList.sort(DISPLAY_DATA_COMPARATOR);
        Debug.debug("discordMessageSent creating contents");
        List<DiscordMessageContent> contents = createContents(dataList, player.isLocal() ? player.getLocalPlayer() : (Bukkit.getOnlinePlayers().isEmpty() ? null : Bukkit.getOnlinePlayers().iterator().next()));
        DiscordImageEvent discordImageEvent = new DiscordImageEvent(channel, textOriginal, text, contents, false, true);
        Bukkit.getPluginManager().callEvent(discordImageEvent);
        Debug.debug("discordMessageSent sending to discord, Cancelled: " + discordImageEvent.isCancelled());
        if (discordImageEvent.isCancelled()) {
            message.editMessage(discordImageEvent.getOriginalMessage()).queue();
        } else {
            text = discordImageEvent.getNewMessage();
            MessageAction action = message.editMessage(text);
            List<MessageEmbed> embeds = new ArrayList<>();
            for (DiscordMessageContent content : contents) {
                embeds.addAll(content.toJDAMessageEmbeds());
                for (Entry<String, byte[]> attachment : content.getAttachments().entrySet()) {
                    action = action.addFile(attachment.getValue(), attachment.getKey());
                }
            }
            action.setEmbeds(embeds).queue();
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) MessageEmbed(github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message) DiscordImageEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent) DiscordDisplayData(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) ArrayList(java.util.ArrayList) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) MessageAction(github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Example 4 with DiscordMessageContent

use of com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method createContents.

private static List<DiscordMessageContent> createContents(List<DiscordDisplayData> dataList, Player player) {
    List<DiscordMessageContent> contents = new ArrayList<>();
    for (DiscordDisplayData data : dataList) {
        if (data instanceof ImageDisplayData) {
            ImageDisplayData iData = (ImageDisplayData) data;
            ImageDisplayType type = iData.getType();
            String title = iData.getTitle();
            if (iData.getItemStack().isPresent()) {
                Debug.debug("createContents creating item discord content");
                ItemStack item = iData.getItemStack().get();
                Color color = DiscordItemStackUtils.getDiscordColor(item);
                if (color == null || color.equals(Color.WHITE)) {
                    color = new Color(0xFFFFFE);
                }
                try {
                    BufferedImage image = ImageGeneration.getItemStackImage(item, data.getPlayer(), InteractiveChatDiscordSrvAddon.plugin.itemAltAir);
                    byte[] imageData = ImageUtils.toArray(image);
                    DiscordDescription description = DiscordItemStackUtils.getDiscordDescription(item, player);
                    DiscordMessageContent content = new DiscordMessageContent(description.getName(), "attachment://Item.png", color);
                    content.addAttachment("Item.png", imageData);
                    contents.add(content);
                    if (InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImage) {
                        DiscordToolTip discordToolTip = DiscordItemStackUtils.getToolTip(item, player);
                        if (!discordToolTip.isBaseItem() || InteractiveChatDiscordSrvAddon.plugin.itemUseTooltipImageOnBaseItem) {
                            BufferedImage tooltip = ImageGeneration.getToolTipImage(discordToolTip.getComponents());
                            byte[] tooltipData = ImageUtils.toArray(tooltip);
                            content.addAttachment("ToolTip.png", tooltipData);
                            content.addImageUrl("attachment://ToolTip.png");
                        } else {
                            content.addDescription(description.getDescription().orElse(null));
                        }
                    } else {
                        content.addDescription(description.getDescription().orElse(null));
                    }
                    if (type.equals(ImageDisplayType.ITEM_CONTAINER)) {
                        if (!description.getDescription().isPresent()) {
                            content.getImageUrls().remove("attachment://ToolTip.png");
                            content.getAttachments().remove("ToolTip.png");
                        }
                        TitledInventoryWrapper inv = iData.getInventory().get();
                        BufferedImage container = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
                        byte[] containerData = ImageUtils.toArray(container);
                        content.addAttachment("Container.png", containerData);
                        content.addImageUrl("attachment://Container.png");
                    } else {
                        if (iData.isFilledMap() && iData.getPlayer().isLocal()) {
                            if (!description.getDescription().isPresent()) {
                                content.getImageUrls().remove("attachment://ToolTip.png");
                                content.getAttachments().remove("ToolTip.png");
                            }
                            BufferedImage map = ImageGeneration.getMapImage(item, iData.getPlayer().getLocalPlayer());
                            byte[] mapData = ImageUtils.toArray(map);
                            content.addAttachment("Map.png", mapData);
                            content.addImageUrl("attachment://Map.png");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (iData.getInventory().isPresent()) {
                Debug.debug("createContents creating inventory discord content");
                TitledInventoryWrapper inv = iData.getInventory().get();
                try {
                    BufferedImage image;
                    if (iData.isPlayerInventory()) {
                        if (InteractiveChatDiscordSrvAddon.plugin.usePlayerInvView) {
                            image = ImageGeneration.getPlayerInventoryImage(inv.getInventory(), iData.getPlayer());
                        } else {
                            image = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
                        }
                    } else {
                        image = ImageGeneration.getInventoryImage(inv.getInventory(), inv.getTitle(), data.getPlayer());
                    }
                    Color color;
                    switch(type) {
                        case ENDERCHEST:
                            color = InteractiveChatDiscordSrvAddon.plugin.enderColor;
                            break;
                        case INVENTORY:
                            color = InteractiveChatDiscordSrvAddon.plugin.invColor;
                            break;
                        default:
                            color = Color.black;
                            break;
                    }
                    byte[] imageData = ImageUtils.toArray(image);
                    DiscordMessageContent content = new DiscordMessageContent(title, null, null, "attachment://Inventory.png", color);
                    content.addAttachment("Inventory.png", imageData);
                    if (type.equals(ImageDisplayType.INVENTORY) && InteractiveChatDiscordSrvAddon.plugin.invShowLevel) {
                        int level = iData.getPlayer().getExperienceLevel();
                        byte[] bottleData = ImageUtils.toArray(InteractiveChatDiscordSrvAddon.plugin.modelRenderer.render(32, 32, InteractiveChatDiscordSrvAddon.plugin.resourceManager, InteractiveChat.version.isOld(), "minecraft:item/experience_bottle", ModelDisplayPosition.GUI, false).getImage());
                        content.addAttachment("Level.png", bottleData);
                        content.setFooter(LanguageUtils.getTranslation(TranslationKeyUtils.getLevelTranslation(level), InteractiveChatDiscordSrvAddon.plugin.language).replaceFirst("%s", level + ""));
                        content.setFooterImageUrl("attachment://Level.png");
                    }
                    contents.add(content);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (data instanceof HoverClickDisplayData) {
            Debug.debug("createContents creating hover event discord content");
            try {
                HoverClickDisplayData hData = (HoverClickDisplayData) data;
                String title = hData.getDisplayText();
                Color color = hData.getColor();
                DiscordMessageContent content = new DiscordMessageContent(title, null, color);
                String body = "";
                String preview = null;
                if (hData.hasHover()) {
                    if (InteractiveChatDiscordSrvAddon.plugin.hoverUseTooltipImage) {
                        Component print = hData.getHoverText();
                        BufferedImage tooltip = ImageGeneration.getToolTipImage(print, true);
                        byte[] tooltipData = ImageUtils.toArray(tooltip);
                        content.addAttachment("ToolTip.png", tooltipData);
                        content.addImageUrl("attachment://ToolTip.png");
                        content.addDescription(null);
                    } else {
                        body += ComponentStringUtils.stripColorAndConvertMagic(InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(hData.getHoverText()));
                    }
                }
                if (hData.hasClick()) {
                    switch(hData.getClickAction()) {
                        case COPY_TO_CLIPBOARD:
                            if (body.length() > 0) {
                                body += "\n\n";
                            }
                            body += LanguageUtils.getTranslation(TranslationKeyUtils.getCopyToClipboard(), InteractiveChatDiscordSrvAddon.plugin.language) + ": __" + hData.getClickValue() + "__";
                            break;
                        case OPEN_URL:
                            if (body.length() > 0) {
                                body += "\n\n";
                            }
                            String url = hData.getClickValue();
                            body += LanguageUtils.getTranslation(TranslationKeyUtils.getOpenUrl(), InteractiveChatDiscordSrvAddon.plugin.language) + ": __" + url + "__";
                            if (URLRequestUtils.IMAGE_URL_PATTERN.matcher(url).matches() && URLRequestUtils.isAllowed(url)) {
                                preview = url;
                            }
                            break;
                        default:
                            break;
                    }
                }
                if (!body.isEmpty()) {
                    content.addDescription(body);
                }
                if (InteractiveChatDiscordSrvAddon.plugin.hoverImage) {
                    BufferedImage image = InteractiveChatDiscordSrvAddon.plugin.resourceManager.getTextureManager().getTexture(ResourceRegistry.IC_MISC_TEXTURE_LOCATION + "hover_cursor").getTexture();
                    byte[] imageData = ImageUtils.toArray(image);
                    content.setAuthorIconUrl("attachment://Hover.png");
                    content.addAttachment("Hover.png", imageData);
                }
                if (preview != null) {
                    content.addImageUrl(preview);
                }
                contents.add(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return contents;
}
Also used : DiscordDescription(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordDescription) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) DiscordDisplayData(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData) Color(java.awt.Color) DiscordToolTip(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) TitledInventoryWrapper(com.loohp.interactivechatdiscordsrvaddon.wrappers.TitledInventoryWrapper) ImageDisplayType(com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayType) ItemStack(org.bukkit.inventory.ItemStack) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ImageDisplayData(com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayData) HoverClickDisplayData(com.loohp.interactivechatdiscordsrvaddon.objectholders.HoverClickDisplayData)

Aggregations

DiscordMessageContent (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent)4 Subscribe (github.scarsz.discordsrv.api.Subscribe)3 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)3 WebhookClient (club.minnced.discord.webhook.WebhookClient)2 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)2 DiscordDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData)2 DiscordDescription (com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordDescription)2 DiscordToolTip (com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip)2 Message (github.scarsz.discordsrv.dependencies.jda.api.entities.Message)2 Color (java.awt.Color)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ItemStack (org.bukkit.inventory.ItemStack)2 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 DiscordImageEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent)1 AttachmentData (com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData)1 HoverClickDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.HoverClickDisplayData)1 ImageDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayData)1