Skip to content

Commit

Permalink
Add name parameter for Discord avatar URL (EssentialsX#4369)
Browse files Browse the repository at this point in the history
Allows offline mode/Geyser users to configure their own avatar URL using a service that accepts usernames.
  • Loading branch information
JRoy authored Aug 9, 2021
1 parent d56ecaa commit ac8a13f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void sendChatMessage(final Player player, final String chatMessage) {
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getPrefix(player))),
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))));

final String avatarUrl = getSettings().isShowAvatar() ? getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()) : null;
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ public void sendJoinQuitMessage(final Player player, final String message, boole
MessageUtil.formatMessage(join ? jda.getSettings().getJoinFormat(player) : jda.getSettings().getQuitFormat(player),
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(message),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? player.getName() : (jda.getSettings().isShowDisplayName() ? player.getDisplayName() : null),
player.getUniqueId()));
MessageUtil.sanitizeDiscordMarkdown(message)),
player);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -154,6 +151,7 @@ public void onDeath(PlayerDeathEvent event) {
if (!event.getEntity().getWorld().isGameRule("showDeathMessages")) {
showDeathMessages = null;
} else {
//noinspection deprecation
showDeathMessages = event.getEntity().getWorld().getGameRuleValue("showDeathMessages").equals("true");
}
}
Expand All @@ -166,10 +164,7 @@ public void onDeath(PlayerDeathEvent event) {
MessageUtil.sanitizeDiscordMarkdown(event.getEntity().getName()),
MessageUtil.sanitizeDiscordMarkdown(event.getEntity().getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(event.getDeathMessage())),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", event.getEntity().getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? event.getEntity().getName() : (jda.getSettings().isShowDisplayName() ? event.getEntity().getDisplayName() : null),
event.getEntity().getUniqueId());
event.getEntity());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -189,10 +184,7 @@ public void onAfk(AfkStatusChangeEvent event) {
MessageUtil.formatMessage(format,
MessageUtil.sanitizeDiscordMarkdown(event.getAffected().getName()),
MessageUtil.sanitizeDiscordMarkdown(event.getAffected().getDisplayName())),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", event.getAffected().getBase().getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? event.getAffected().getName() : (jda.getSettings().isShowDisplayName() ? event.getAffected().getDisplayName() : null),
event.getAffected().getBase().getUniqueId());
event.getAffected().getBase());
}

@EventHandler(priority = EventPriority.MONITOR)
Expand All @@ -206,10 +198,7 @@ public void onAdvancement(AbstractAchievementEvent event) {
MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getName()),
MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getDisplayName()),
event.getName()),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", event.getPlayer().getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? event.getPlayer().getName() : (jda.getSettings().isShowDisplayName() ? event.getPlayer().getDisplayName() : null),
event.getPlayer().getUniqueId());
event.getPlayer());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -233,10 +222,27 @@ private boolean isVanishHide(final IUser user) {
}

private void sendDiscordMessage(final MessageType messageType, final String message) {
sendDiscordMessage(messageType, message, false, null, null, null);
sendDiscordMessage(messageType, message, null);
}

private void sendDiscordMessage(final MessageType messageType, final String message, final boolean allowPing, final String avatarUrl, final String name, final UUID uuid) {
DiscordUtil.dispatchDiscordMessage(jda, messageType, message, allowPing, avatarUrl, name, uuid);
private void sendDiscordMessage(final MessageType messageType, final String message, final Player player) {
String avatarUrl = null;
String name = null;
UUID uuid = null;
if (player != null) {
if (jda.getSettings().isShowAvatar()) {
avatarUrl = DiscordUtil.getAvatarUrl(jda, player);
}

if (jda.getSettings().isShowName()) {
name = player.getName();
} else if (jda.getSettings().isShowDisplayName()) {
name = player.getDisplayName();
}

uuid = player.getUniqueId();
}

DiscordUtil.dispatchDiscordMessage(jda, messageType, message, false, avatarUrl, name, uuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.essentialsx.discord.JDADiscordService;
import okhttp3.OkHttpClient;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -194,6 +195,10 @@ public static boolean hasRoles(Member member, List<String> roleDefinitions) {
return false;
}

public static String getAvatarUrl(final JDADiscordService jda, final Player player) {
return jda.getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()).replace("{name}", player.getName());
}

public static void dispatchDiscordMessage(final JDADiscordService jda, final MessageType messageType, final String message, final boolean allowPing, final String avatarUrl, final String name, final UUID uuid) {
if (jda.getPlugin().getSettings().getMessageChannel(messageType.getKey()).equalsIgnoreCase("none")) {
return;
Expand Down
1 change: 1 addition & 0 deletions EssentialsDiscord/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ show-avatar: false
# The URL which should be used to get the avatars of users when "show-avatar" is set to true.
# Any URL in here should only return a proper JPEG/PNG image and nothing else.
# 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
Expand Down

0 comments on commit ac8a13f

Please # to comment.