Search in sources :

Example 1 with DeathMessagePostProcessEvent

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

the class PlayerDeathListener method runAsync.

private void runAsync(PlayerDeathEvent event, Player player) {
    String deathMessage = event.getDeathMessage();
    if (StringUtils.isBlank(deathMessage)) {
        DiscordSRV.debug("Not sending death message for " + player.getName() + ", the death message is null");
        return;
    }
    String channelName = DiscordSRV.getPlugin().getOptionalChannel("deaths");
    MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerDeathMessage");
    if (messageFormat == null)
        return;
    DeathMessagePreProcessEvent preEvent = DiscordSRV.api.callEvent(new DeathMessagePreProcessEvent(channelName, messageFormat, player, deathMessage, event));
    if (preEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "DeathMessagePreProcessEvent was cancelled, message send aborted");
        return;
    }
    // Update from event in case any listeners modified parameters
    channelName = preEvent.getChannel();
    messageFormat = preEvent.getMessageFormat();
    deathMessage = preEvent.getDeathMessage();
    if (messageFormat == null)
        return;
    String finalDeathMessage = StringUtils.isNotBlank(deathMessage) ? deathMessage : "";
    String avatarUrl = DiscordSRV.getAvatarUrl(event.getEntity());
    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("%deathmessage%", MessageUtil.strip(needsEscape ? DiscordUtil.escapeMarkdown(finalDeathMessage) : finalDeathMessage)).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);
    if (DiscordSRV.getLength(discordMessage) < 3) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Not sending death message, because it's less than three characters long. Message: " + messageFormat);
        return;
    }
    DeathMessagePostProcessEvent postEvent = DiscordSRV.api.callEvent(new DeathMessagePostProcessEvent(channelName, discordMessage, player, deathMessage, event, messageFormat.isUseWebhooks(), webhookName, webhookAvatarUrl, preEvent.isCancelled()));
    if (postEvent.isCancelled()) {
        DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "DeathMessagePostProcessEvent 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) DeathMessagePreProcessEvent(github.scarsz.discordsrv.api.events.DeathMessagePreProcessEvent) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) BiFunction(java.util.function.BiFunction) DeathMessagePostProcessEvent(github.scarsz.discordsrv.api.events.DeathMessagePostProcessEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) EntityType(org.bukkit.entity.EntityType) StringUtils(org.apache.commons.lang3.StringUtils) Debug(github.scarsz.discordsrv.Debug) EventHandler(org.bukkit.event.EventHandler) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) EventPriority(org.bukkit.event.EventPriority) github.scarsz.discordsrv.util(github.scarsz.discordsrv.util) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageFormat(github.scarsz.discordsrv.objects.MessageFormat) Message(net.dv8tion.jda.api.entities.Message) DeathMessagePreProcessEvent(github.scarsz.discordsrv.api.events.DeathMessagePreProcessEvent) DeathMessagePostProcessEvent(github.scarsz.discordsrv.api.events.DeathMessagePostProcessEvent)

Aggregations

Debug (github.scarsz.discordsrv.Debug)1 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 DeathMessagePostProcessEvent (github.scarsz.discordsrv.api.events.DeathMessagePostProcessEvent)1 DeathMessagePreProcessEvent (github.scarsz.discordsrv.api.events.DeathMessagePreProcessEvent)1 MessageFormat (github.scarsz.discordsrv.objects.MessageFormat)1 github.scarsz.discordsrv.util (github.scarsz.discordsrv.util)1 BiFunction (java.util.function.BiFunction)1 Message (net.dv8tion.jda.api.entities.Message)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Bukkit (org.bukkit.Bukkit)1 EntityType (org.bukkit.entity.EntityType)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 EventPriority (org.bukkit.event.EventPriority)1 Listener (org.bukkit.event.Listener)1 PlayerDeathEvent (org.bukkit.event.entity.PlayerDeathEvent)1