Search in sources :

Example 1 with PlayerAdvancementDoneEvent

use of org.bukkit.event.player.PlayerAdvancementDoneEvent 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)

Aggregations

Debug (github.scarsz.discordsrv.Debug)1 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 AchievementMessagePostProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePostProcessEvent)1 AchievementMessagePreProcessEvent (github.scarsz.discordsrv.api.events.AchievementMessagePreProcessEvent)1 MessageFormat (github.scarsz.discordsrv.objects.MessageFormat)1 github.scarsz.discordsrv.util (github.scarsz.discordsrv.util)1 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1 Message (net.dv8tion.jda.api.entities.Message)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Bukkit (org.bukkit.Bukkit)1 Advancement (org.bukkit.advancement.Advancement)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1