Search in sources :

Example 1 with AchievementMessagePostProcessEvent

use of github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent in project DiscordSRV by Scarsz.

the class PlayerAdvancementDoneListener method runAsync.

private void runAsync(PlayerAdvancementDoneEvent event) {
    try {
        Object craftAdvancement = ((Object) event.getAdvancement()).getClass().getMethod("getHandle").invoke(event.getAdvancement());
        Object advancementDisplay = craftAdvancement.getClass().getMethod("c").invoke(craftAdvancement);
        boolean display = (boolean) advancementDisplay.getClass().getMethod("i").invoke(advancementDisplay);
        if (!display)
            return;
    } catch (NullPointerException e) {
        return;
    } catch (Exception e) {
        DiscordSRV.error(e);
        return;
    }
    String channelName = DiscordSRV.getPlugin().getOptionalChannel("awards");
    Player player = event.getPlayer();
    Advancement advancement = event.getAdvancement();
    MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerAchievementMessage");
    if (messageFormat == null)
        return;
    // turn "story/advancement_name" into "Advancement Name"
    String advancementTitle = getTitle(advancement);
    AchievementMessagePreProcessEvent preEvent = DiscordSRV.api.callEvent(new AchievementMessagePreProcessEvent(channelName, messageFormat, player, advancementTitle, event));
    if (preEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePreProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    advancementTitle = preEvent.getAchievementName();
    channelName = preEvent.getChannel();
    messageFormat = preEvent.getMessageFormat();
    if (messageFormat == null)
        return;
    String finalAchievementName = StringUtils.isNotBlank(advancementTitle) ? advancementTitle : "";
    String avatarUrl = DiscordSRV.getAvatarUrl(player);
    String botAvatarUrl = DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
    String botName = DiscordSRV.getPlugin().getMainGuild() != null ? DiscordSRV.getPlugin().getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
    String displayName = StringUtils.isNotBlank(player.getDisplayName()) ? MessageUtil.strip(player.getDisplayName()) : "";
    TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
        if (content == null)
            return null;
        content = content.replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%username%", needsEscape ? DiscordUtil.escapeMarkdown(player.getName()) : player.getName()).replace("%displayname%", needsEscape ? DiscordUtil.escapeMarkdown(displayName) : displayName).replace("%usernamenoescapes%", player.getName()).replace("%displaynamenoescapes%", displayName).replace("%world%", player.getWorld().getName()).replace("%achievement%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalAchievementName) : finalAchievementName)).replace("%embedavatarurl%", avatarUrl).replace("%botavatarurl%", botAvatarUrl).replace("%botname%", botName);
        if (destinationChannel != null)
            content = DiscordUtil.translateEmotes(content, destinationChannel.getGuild());
        content = PlaceholderUtil.replacePlaceholdersToDiscord(content, player);
        return content;
    };
    Message discordMessage = DiscordSRV.translateMessage(messageFormat, translator);
    if (discordMessage == null)
        return;
    String webhookName = translator.apply(messageFormat.getWebhookName(), false);
    String webhookAvatarUrl = translator.apply(messageFormat.getWebhookAvatarUrl(), false);
    AchievementMessagePostProcessEvent postEvent = DiscordSRV.api.callEvent(new AchievementMessagePostProcessEvent(channelName, discordMessage, player, advancementTitle, event, messageFormat.isUseWebhooks(), webhookName, webhookAvatarUrl, preEvent.isCancelled()));
    if (postEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePostProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    channelName = postEvent.getChannel();
    discordMessage = postEvent.getDiscordMessage();
    TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    if (postEvent.isUsingWebhooks()) {
        WebhookUtil.deliverMessage(textChannel, postEvent.getWebhookName(), postEvent.getWebhookAvatarUrl(), discordMessage.getContentRaw(), discordMessage.getEmbeds().stream().findFirst().orElse(null));
    } else {
        DiscordUtil.queueMessage(textChannel, discordMessage, true);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) PlayerAdvancementDoneEvent(org.bukkit.event.player.PlayerAdvancementDoneEvent) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Arrays(java.util.Arrays) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) BiFunction(java.util.function.BiFunction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) Field(java.lang.reflect.Field) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Advancement(org.bukkit.advancement.Advancement) Debug(github.scarsz.discordsrv.Debug) EventHandler(org.bukkit.event.EventHandler) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) EventPriority(org.bukkit.event.EventPriority) Map(java.util.Map) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) Player(org.bukkit.entity.Player) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Message(net.dv8tion.jda.api.entities.Message) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent) Advancement(org.bukkit.advancement.Advancement)

Example 2 with AchievementMessagePostProcessEvent

use of github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent in project DiscordSRV by Scarsz.

the class PlayerAchievementsListener method runAsync.

private void runAsync(Event event, Player player) {
    Enum<?> achievement;
    try {
        achievement = (Enum<?>) event.getClass().getMethod("getAchievement").invoke(event);
        if (achievement == null)
            return;
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        DiscordSRV.error("Failed to get achievement name from " + event.getEventName() + ": " + e.getMessage());
        handlerList.unregister(registeredListener);
        return;
    }
    // turn "ACHIEVEMENT_NAME" into "Achievement Name"
    String channelName = DiscordSRV.getPlugin().getOptionalChannel("awards");
    String achievementName = PrettyUtil.beautify(achievement);
    MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerAchievementMessage");
    if (messageFormat == null)
        return;
    AchievementMessagePreProcessEvent preEvent = DiscordSRV.api.callEvent(new AchievementMessagePreProcessEvent(channelName, messageFormat, player, achievementName, event));
    if (preEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePreProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    achievementName = preEvent.getAchievementName();
    channelName = preEvent.getChannel();
    messageFormat = preEvent.getMessageFormat();
    if (messageFormat == null)
        return;
    String finalAchievementName = StringUtils.isNotBlank(achievementName) ? achievementName : "";
    String avatarUrl = DiscordSRV.getAvatarUrl(player);
    String botAvatarUrl = DiscordUtil.getJda().getSelfUser().getEffectiveAvatarUrl();
    String botName = DiscordSRV.getPlugin().getMainGuild() != null ? DiscordSRV.getPlugin().getMainGuild().getSelfMember().getEffectiveName() : DiscordUtil.getJda().getSelfUser().getName();
    String displayName = StringUtils.isNotBlank(player.getDisplayName()) ? MessageUtil.strip(player.getDisplayName()) : "";
    TextChannel destinationChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    BiFunction<String, Boolean, String> translator = (content, needsEscape) -> {
        if (content == null)
            return null;
        content = content.replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%username%", needsEscape ? DiscordUtil.escapeMarkdown(player.getName()) : player.getName()).replace("%displayname%", needsEscape ? DiscordUtil.escapeMarkdown(displayName) : displayName).replace("%usernamenoescapes%", player.getName()).replace("%displaynamenoescapes%", displayName).replace("%world%", player.getWorld().getName()).replace("%achievement%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalAchievementName) : finalAchievementName)).replace("%embedavatarurl%", avatarUrl).replace("%botavatarurl%", botAvatarUrl).replace("%botname%", botName);
        if (destinationChannel != null)
            content = DiscordUtil.translateEmotes(content, destinationChannel.getGuild());
        content = PlaceholderUtil.replacePlaceholdersToDiscord(content, player);
        return content;
    };
    Message discordMessage = DiscordSRV.translateMessage(messageFormat, translator);
    if (discordMessage == null)
        return;
    String webhookName = translator.apply(messageFormat.getWebhookName(), false);
    String webhookAvatarUrl = translator.apply(messageFormat.getWebhookAvatarUrl(), false);
    AchievementMessagePostProcessEvent postEvent = DiscordSRV.api.callEvent(new AchievementMessagePostProcessEvent(channelName, discordMessage, player, achievementName, event, messageFormat.isUseWebhooks(), webhookName, webhookAvatarUrl, preEvent.isCancelled()));
    if (postEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "AchievementMessagePostProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    channelName = postEvent.getChannel();
    discordMessage = postEvent.getDiscordMessage();
    TextChannel textChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(channelName);
    if (postEvent.isUsingWebhooks()) {
        WebhookUtil.deliverMessage(textChannel, postEvent.getWebhookName(), postEvent.getWebhookAvatarUrl(), discordMessage.getContentRaw(), discordMessage.getEmbeds().stream().findFirst().orElse(null));
    } else {
        DiscordUtil.queueMessage(textChannel, discordMessage, true);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) HandlerList(org.bukkit.event.HandlerList) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) BiFunction(java.util.function.BiFunction) Event(org.bukkit.event.Event) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) StringUtils(org.apache.commons.lang3.StringUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) Debug(github.scarsz.discordsrv.Debug) RegisteredListener(org.bukkit.plugin.RegisteredListener) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) EventPriority(org.bukkit.event.EventPriority) EventExecutor(org.bukkit.plugin.EventExecutor) PlayerEvent(org.bukkit.event.player.PlayerEvent) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Message(net.dv8tion.jda.api.entities.Message) InvocationTargetException(java.lang.reflect.InvocationTargetException) AchievementMessagePreProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AchievementMessagePostProcessEvent(github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent)

Aggregations

Debug (github.scarsz.discordsrv.Debug)2 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)2 AchievementMessagePostProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent)2 AchievementMessagePreProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent)2 MessageFormat (github.scarsz.discordsrv.objects.MessageFormat)2 github.scarsz.discordsrv.util (github.scarsz.discordsrv.util)2 BiFunction (java.util.function.BiFunction)2 Message (net.dv8tion.jda.api.entities.Message)2 TextChannel (net.dv8tion.jda.api.entities.TextChannel)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Bukkit (org.bukkit.Bukkit)2 Player (org.bukkit.entity.Player)2 EventPriority (org.bukkit.event.EventPriority)2 Listener (org.bukkit.event.Listener)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1