diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java index 192f70a88..e95e3ffcd 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java @@ -226,6 +226,36 @@ public class DiscordSettings implements IConf { "username", "displayname", "message", "world", "prefix", "suffix"); } + public String getLegacyNameFormat() { + // For compatibility with old configs + if (isShowDisplayName()) { + return "{displayname}"; + } else if (isShowName()) { + return "{username}"; + } else { + // Will default to "{botname}" in the format + return null; + } + } + + public MessageFormat getMcToDiscordNameFormat(Player player) { + String format = getFormatString("mc-to-discord-name-format"); + if (format == null) { + format = getLegacyNameFormat(); + } + final String filled; + + if (plugin.isPAPI() && format != null) { + filled = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, format); + } else { + filled = format; + } + + return generateMessageFormat(filled, "{botname}", false, + "username", "displayname", "world", "prefix", "suffix", "botname"); + + } + public MessageFormat getTempMuteFormat() { return tempMuteFormat; } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java index 9ce07b6fe..a381a0ddd 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java @@ -278,9 +278,16 @@ public class JDADiscordService implements DiscordService, IEssentialsModule { MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player)))); final String avatarUrl = DiscordUtil.getAvatarUrl(this, player); - final String name = getSettings().isShowName() ? player.getName() : (getSettings().isShowDisplayName() ? player.getDisplayName() : null); - DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, name, player.getUniqueId()); + final String formattedName = MessageUtil.formatMessage(getSettings().getMcToDiscordNameFormat(player), + MessageUtil.sanitizeDiscordMarkdown(player.getName()), + MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()), + MessageUtil.sanitizeDiscordMarkdown(getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())), + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getPrefix(player))), + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))), + guild.getMember(jda.getSelfUser()).getEffectiveName()); + + DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, formattedName, player.getUniqueId()); } @Override diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java index 79d3130ea..6f4bd31c9 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java @@ -2,6 +2,7 @@ package net.essentialsx.discord.listeners; import com.earth2me.essentials.Console; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IUser; import net.ess3.api.events.AfkStatusChangeEvent; @@ -271,11 +272,13 @@ public class BukkitListener implements Listener { avatarUrl = DiscordUtil.getAvatarUrl(jda, player); } - if (jda.getSettings().isShowName()) { - name = player.getName(); - } else if (jda.getSettings().isShowDisplayName()) { - name = player.getDisplayName(); - } + name = MessageUtil.formatMessage(jda.getSettings().getMcToDiscordNameFormat(player), + MessageUtil.sanitizeDiscordMarkdown(player.getName()), + MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()), + MessageUtil.sanitizeDiscordMarkdown(jda.getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())), + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getPrefix(player))), + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getSuffix(player))), + jda.getGuild().getMember(jda.getJda().getSelfUser()).getEffectiveName()); uuid = player.getUniqueId(); } diff --git a/EssentialsDiscord/src/main/resources/config.yml b/EssentialsDiscord/src/main/resources/config.yml index e87149d0e..145df92ff 100644 --- a/EssentialsDiscord/src/main/resources/config.yml +++ b/EssentialsDiscord/src/main/resources/config.yml @@ -147,11 +147,6 @@ show-avatar: false # To include the UUID of the player in this URL, use "{uuid}". # To include the name of the player in this URL, use "{name}". avatar-url: "https://crafthead.net/helm/{uuid}" -# Whether or not player messages should show their name as the bot name in Discord. -show-name: false -# Whether or not player messages should show their display-name/nickname as the bot name in Discord. -# You must disable show-name for this option to work. -show-displayname: false # Whether or not fake join and leave messages should be sent to Discord when a player toggles vanish in Minecraft. # Fake join/leave messages will be sent the same as real join and leave messages (and to the same channel). @@ -263,6 +258,16 @@ messages: # - {suffix}: The suffix of the player sending the message # ... PlaceholderAPI placeholders are also supported here too! mc-to-discord: "{displayname}: {message}" + # This is the bot's name which appears in Discord when sending player-specific messages. + # The following placeholders can be used here: + # - {username}: The username of the player sending the message + # - {displayname}: The display name of the player sending the message (This would be their nickname) + # - {world}: The name of the world the player sending the message is in + # - {prefix}: The prefix of the player sending the message + # - {suffix}: The suffix of the player sending the message + # - {botname}: Name of the Discord bot + # ... PlaceholderAPI placeholders are also supported here too! + mc-to-discord-name-format: "{botname}" # This is the message sent to Discord when a player is temporarily muted in minecraft. # The following placeholders can be used here: # - {username}: The username of the player being muted