Search in sources :

Example 1 with ImageDisplayType

use of com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayType 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

Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 DiscordDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData)1 DiscordMessageContent (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent)1 HoverClickDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.HoverClickDisplayData)1 ImageDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayData)1 ImageDisplayType (com.loohp.interactivechatdiscordsrvaddon.objectholders.ImageDisplayType)1 DiscordDescription (com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordDescription)1 DiscordToolTip (com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip)1 TitledInventoryWrapper (com.loohp.interactivechatdiscordsrvaddon.wrappers.TitledInventoryWrapper)1 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ItemStack (org.bukkit.inventory.ItemStack)1