use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.
the class PlayerJoinLeaveListener method PlayerQuitEvent.
// priority needs to be different to MONITOR to avoid problems with permissions check when PEX is used, it is at lowest so that it executes before VanishNoPacket's player leave listener and is able to see whether the player is vanished
@EventHandler(priority = EventPriority.LOWEST)
public void PlayerQuitEvent(PlayerQuitEvent event) {
final Player player = event.getPlayer();
if (PlayerUtil.isVanished(player)) {
DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Not sending a quit message for " + event.getPlayer().getName() + " because a vanish plugin reported them as vanished");
return;
}
MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerLeaveMessage");
// make sure quit messages enabled
if (messageFormat == null)
return;
final String name = player.getName();
// no quit message, user shouldn't have one from permission
if (GamePermissionUtil.hasPermission(event.getPlayer(), "discordsrv.silentquit")) {
DiscordSRV.info(LangUtil.InternalMessage.SILENT_QUIT.toString().replace("{player}", name));
return;
}
// player doesn't have silent quit, show quit message
Bukkit.getScheduler().runTaskAsynchronously(DiscordSRV.getPlugin(), () -> DiscordSRV.getPlugin().sendLeaveMessage(event.getPlayer(), event.getQuitMessage()));
}
use of github.scarsz.discordsrv.objects.MessageFormat 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);
}
}
use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.
the class SuperVanishHook method onPlayerHide.
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerHide(PostPlayerHideEvent event) {
SuperVanish plugin = (SuperVanish) getPlugin();
if (!plugin.getSettings().getBoolean("MessageOptions.FakeJoinQuitMessages.BroadcastFakeQuitOnVanish") || event.isSilent()) {
return;
}
final Player player = event.getPlayer();
MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerLeaveMessage");
// make sure quit messages enabled
if (messageFormat == null)
return;
final String name = player.getName();
String joinMessage = plugin.replacePlaceholders("ReappearMessage", player);
// no quit message, user shouldn't have one from permission
if (GamePermissionUtil.hasPermission(event.getPlayer(), "discordsrv.silentquit")) {
DiscordSRV.info(LangUtil.InternalMessage.SILENT_QUIT.toString().replace("{player}", name));
return;
}
// player doesn't have silent quit, show quit message
Bukkit.getScheduler().runTaskAsynchronously(DiscordSRV.getPlugin(), () -> DiscordSRV.getPlugin().sendLeaveMessage(event.getPlayer(), joinMessage));
}
use of github.scarsz.discordsrv.objects.MessageFormat in project DiscordSRV by Scarsz.
the class SuperVanishHook method onPlayerShow.
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerShow(PostPlayerShowEvent event) {
SuperVanish plugin = (SuperVanish) getPlugin();
if (!plugin.getSettings().getBoolean("MessageOptions.FakeJoinQuitMessages.BroadcastFakeJoinOnReappear") || event.isSilent()) {
return;
}
final Player player = event.getPlayer();
String joinMessage = plugin.replacePlaceholders("VanishMessage", player);
MessageFormat messageFormat = DiscordSRV.getPlugin().getMessageFromConfiguration("MinecraftPlayerJoinMessage");
// make sure join messages enabled
if (messageFormat == null)
return;
final String name = player.getName();
// check if player has permission to not have join messages
if (GamePermissionUtil.hasPermission(event.getPlayer(), "discordsrv.silentjoin")) {
DiscordSRV.info(LangUtil.InternalMessage.SILENT_JOIN.toString().replace("{player}", name));
return;
}
// player doesn't have silent join permission, send join message
// schedule command to run in a second to be able to capture display name
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordSRV.getPlugin(), () -> DiscordSRV.getPlugin().sendJoinMessage(event.getPlayer(), joinMessage), 20);
}
use of github.scarsz.discordsrv.objects.MessageFormat 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