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);
}
}
Aggregations