Search in sources :

Example 1 with AttachmentData

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

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

the class OutboundToDiscordEvents method onAdvancement.

// ===== Advancement
@Subscribe(priority = ListenerPriority.HIGHEST)
public void onAdvancement(AchievementMessagePreProcessEvent event) {
    if (event.isCancelled()) {
        return;
    }
    Debug.debug("Triggered onAdvancement");
    MessageFormat messageFormat = event.getMessageFormat();
    if (messageFormat == null) {
        return;
    }
    String title = null;
    String description = null;
    ItemStack item = null;
    AdvancementType advancementType = null;
    boolean isMinecraft = true;
    Event bukkitEvent = event.getTriggeringBukkitEvent();
    if (bukkitEvent.getClass().getSimpleName().equals("PlayerAdvancementDoneEvent")) {
        Debug.debug("onAdvancement getting advancement");
        Object bukkitAdvancement = AdvancementUtils.getAdvancementFromEvent(bukkitEvent);
        AdvancementData data = AdvancementUtils.getAdvancementData(bukkitAdvancement);
        if (data == null) {
            return;
        }
        title = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getTitle(), InteractiveChatDiscordSrvAddon.plugin.language);
        description = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getDescription(), InteractiveChatDiscordSrvAddon.plugin.language);
        item = data.getItem();
        advancementType = data.getAdvancementType();
        isMinecraft = data.isMinecraft();
    } else if (bukkitEvent.getClass().getSimpleName().equals("PlayerAchievementAwardedEvent")) {
        Debug.debug("onAdvancement getting achievement");
        Object bukkitAchievement = AchievementUtils.getAdvancementFromEvent(bukkitEvent);
        if (bukkitAchievement == null) {
            return;
        }
        AdvancementData data = AchievementUtils.getAdvancementData(bukkitAchievement);
        if (data == null) {
            return;
        }
        title = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getTitle(), InteractiveChatDiscordSrvAddon.plugin.language);
        description = InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(data.getDescription(), InteractiveChatDiscordSrvAddon.plugin.language);
        item = data.getItem();
        advancementType = data.getAdvancementType();
        isMinecraft = data.isMinecraft();
    } else {
        return;
    }
    Debug.debug("onAdvancement processing advancement");
    if (InteractiveChatDiscordSrvAddon.plugin.advancementItem && item != null && advancementType != null) {
        String content = messageFormat.getContent();
        if (content == null) {
            content = "";
        }
        try {
            int id = DATA_ID_PROVIDER.getNext();
            BufferedImage thumbnail = ImageGeneration.getAdvancementIcon(item, advancementType, true, ICPlayerFactory.getICPlayer(event.getPlayer()));
            byte[] thumbnailData = ImageUtils.toArray(thumbnail);
            content += "<ICA=" + id + ">";
            messageFormat.setContent(content);
            RESEND_WITH_ATTACHMENT.put(id, new AttachmentData("Thumbnail.png", thumbnailData));
            messageFormat.setThumbnailUrl("attachment://Thumbnail.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (InteractiveChatDiscordSrvAddon.plugin.advancementName && title != null) {
        event.setAchievementName(ChatColorUtils.stripColor(title));
        messageFormat.setAuthorName(LanguageUtils.getTranslation(advancementType.getTranslationKey(), InteractiveChatDiscordSrvAddon.plugin.language).replaceFirst("%s", event.getPlayer().getName()).replaceFirst("%s", ChatColorUtils.stripColor(title)));
        Color color;
        if (isMinecraft) {
            color = ColorUtils.getColor(advancementType.getColor());
        } else {
            String colorStr = ChatColorUtils.getFirstColors(title);
            color = ColorUtils.getColor(ColorUtils.toChatColor(colorStr));
        }
        if (color.equals(Color.white)) {
            color = new Color(0xFFFFFE);
        }
        messageFormat.setColorRaw(color.getRGB());
    }
    if (InteractiveChatDiscordSrvAddon.plugin.advancementDescription && description != null) {
        messageFormat.setDescription(ChatColorUtils.stripColor(description));
    }
    event.setMessageFormat(messageFormat);
}
Also used : MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Color(java.awt.Color) IOException(java.io.IOException) AdvancementData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData) BufferedImage(java.awt.image.BufferedImage) AdvancementType(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType) GameMessageProcessPlayerInventoryEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessPlayerInventoryEvent) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) Event(org.bukkit.event.Event) DeathMessagePostProcessEvent(github.scarsz.discordsrv.api.events.DeathMessagePostProcessEvent) GameMessageProcessItemEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessItemEvent) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent) VentureChatMessagePreProcessEvent(github.scarsz.discordsrv.api.events.VentureChatMessagePreProcessEvent) GameChatMessagePreProcessEvent(github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) GameMessagePostProcessEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessagePostProcessEvent) DiscordGuildMessagePostProcessEvent(github.scarsz.discordsrv.api.events.DiscordGuildMessagePostProcessEvent) GameMessagePreProcessEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessagePreProcessEvent) DiscordImageEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent) DiscordGuildMessageSentEvent(github.scarsz.discordsrv.api.events.DiscordGuildMessageSentEvent) MessageReceivedEvent(github.scarsz.discordsrv.dependencies.jda.api.events.message.MessageReceivedEvent) GameMessageProcessInventoryEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessInventoryEvent) AttachmentData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData) ItemStack(org.bukkit.inventory.ItemStack) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Aggregations

AttachmentData (com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData)2 Subscribe (github.scarsz.discordsrv.api.Subscribe)2 WebhookClient (club.minnced.discord.webhook.WebhookClient)1 DiscordImageEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent)1 GameMessagePostProcessEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessagePostProcessEvent)1 GameMessagePreProcessEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessagePreProcessEvent)1 GameMessageProcessInventoryEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessInventoryEvent)1 GameMessageProcessItemEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessItemEvent)1 GameMessageProcessPlayerInventoryEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.GameMessageProcessPlayerInventoryEvent)1 AdvancementData (com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData)1 AdvancementType (com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType)1 DiscordMessageContent (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent)1 AchievementMessagePostProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent)1 AchievementMessagePreProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent)1 DeathMessagePostProcessEvent (github.scarsz.discordsrv.api.events.DeathMessagePostProcessEvent)1 DiscordGuildMessagePostProcessEvent (github.scarsz.discordsrv.api.events.DiscordGuildMessagePostProcessEvent)1 DiscordGuildMessageSentEvent (github.scarsz.discordsrv.api.events.DiscordGuildMessageSentEvent)1 GameChatMessagePreProcessEvent (github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent)1 VentureChatMessagePreProcessEvent (github.scarsz.discordsrv.api.events.VentureChatMessagePreProcessEvent)1 Message (github.scarsz.discordsrv.dependencies.jda.api.entities.Message)1