Search in sources :

Example 1 with AdvancementData

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

the class AchievementUtils method getAdvancementData.

public static AdvancementData getAdvancementData(Object achievementObject) {
    try {
        Object nmsAchievement = craftStatisticClassGetNMSAchievementMethod.invoke(null, achievementObject);
        nmsAdvancementClassNameField.setAccessible(true);
        String name = nmsAdvancementClassNameField.get(nmsAchievement).toString();
        Component title = Component.translatable(name).color(NamedTextColor.GREEN);
        Component description = Component.translatable(name + ".desc");
        AdvancementType advancementType = AdvancementType.LEGACY;
        nmsAdvancementClassItemStackField.setAccessible(true);
        Object nmsItemStack = nmsAdvancementClassItemStackField.get(nmsAchievement);
        ItemStack itemStack = ItemStackUtils.toBukkitCopy(nmsItemStack);
        return new AdvancementData(title, description, itemStack, advancementType, true);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AdvancementType(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ItemStack(org.bukkit.inventory.ItemStack) AdvancementData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AdvancementData

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

Example 3 with AdvancementData

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

the class AdvancementUtils method getAdvancementData.

public static AdvancementData getAdvancementData(Object advancementObject) {
    try {
        Advancement advancement = (Advancement) advancementObject;
        boolean isMinecraft = advancement.getKey().getNamespace().equals(MINECRAFT_NAMESPACE);
        Object craftAdvancement = craftAdvancementClass.cast(advancement);
        Object nmsAdvancement = craftAdvancementClassGetHandleMethod.invoke(craftAdvancement);
        Object nmsAdvancementDisplay = nmsAdvancementClassGetDisplayMethod.invoke(nmsAdvancement);
        Object nmsTitle = nmsAdvancementDisplayClassGetTitleMethod.invoke(nmsAdvancementDisplay);
        Component title = ChatComponentType.IChatBaseComponent.convertFrom(nmsTitle);
        Object nmsDescription = nmsAdvancementDisplayClassGetDescriptionMethod.invoke(nmsAdvancementDisplay);
        Component description = ChatComponentType.IChatBaseComponent.convertFrom(nmsDescription);
        Object nmsAdvancementType = nmsAdvancementDisplayClassGetTypeMethod.invoke(nmsAdvancementDisplay);
        AdvancementType advancementType = AdvancementType.fromHandle(nmsAdvancementType);
        nmsAdvancementDisplayClassItemStackField.setAccessible(true);
        Object nmsItemStack = nmsAdvancementDisplayClassItemStackField.get(nmsAdvancementDisplay);
        ItemStack itemStack = ItemStackUtils.toBukkitCopy(nmsItemStack);
        return new AdvancementData(title, description, itemStack, advancementType, isMinecraft);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AdvancementType(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) ItemStack(org.bukkit.inventory.ItemStack) AdvancementData(com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData) Advancement(org.bukkit.advancement.Advancement) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AdvancementData (com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementData)3 AdvancementType (com.loohp.interactivechatdiscordsrvaddon.objectholders.AdvancementType)3 ItemStack (org.bukkit.inventory.ItemStack)3 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 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 AttachmentData (com.loohp.interactivechatdiscordsrvaddon.objectholders.AttachmentData)1 Subscribe (github.scarsz.discordsrv.api.Subscribe)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