From 3869300a1506f647ed8f785c56c9aed51d79a3e7 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Tue, 8 Aug 2023 21:25:46 -0400 Subject: [PATCH] changes --- .../essentialsx/discord/DiscordSettings.java | 13 ++- .../essentialsx/discord/util/DiscordUtil.java | 88 ++++++++----------- .../src/main/resources/config.yml | 16 ++-- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java index 43900effd96..a7c4570dfb7 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.utils.FormatUtil; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Role; import net.essentialsx.api.v2.ChatType; import org.apache.logging.log4j.Level; import org.bukkit.entity.Player; @@ -33,6 +34,7 @@ public class DiscordSettings implements IConf { private Activity statusActivity; private List discordFilter; + private Map roleAliases; private MessageFormat consoleFormat; private Level consoleLogLevel; @@ -100,8 +102,8 @@ public Boolean getInvertDiscordRoleBlacklist() { return config.getBoolean("invert-discord-role-blacklist", false); } - public String getDiscordRoleAlias(String key) { - return config.getString("discord-roles-aliases." + key, ""); + public String getRoleAlias(final Role role) { + return roleAliases.getOrDefault(role.getId(), roleAliases.getOrDefault(role.getName(), role.getName())); } public List getPermittedFormattingRoles() { @@ -520,6 +522,13 @@ public void reloadConfig() { } } + final Map roleAliases = new HashMap<>(); + + for (Map.Entry entry : config.getStringMap("chat.world-aliases").entrySet()) { + roleAliases.put(entry.getKey(), FormatUtil.replaceFormat(entry.getValue())); + } + this.roleAliases = roleAliases; + consoleLogLevel = Level.toLevel(config.getString("console.log-level", null), Level.INFO); if (config.isList("console.console-filter")) { diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java index 261dcddcf51..01f82a51115 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java @@ -4,7 +4,6 @@ import club.minnced.discord.webhook.send.AllowedMentions; import com.earth2me.essentials.utils.DownsampleUtil; import com.earth2me.essentials.utils.FormatUtil; -import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.VersionUtil; import com.google.common.collect.ImmutableList; import net.dv8tion.jda.api.Permission; @@ -21,10 +20,12 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Predicate; public final class DiscordUtil { public final static String ADVANCED_RELAY_NAME = "EssX Advanced Relay"; @@ -126,6 +127,27 @@ public static CompletableFuture createWebhook(TextChannel channel, Stri return future; } + public static Role getHighestRole(final JDADiscordService jda, final Member member, final Predicate fail) { + final List roles = member == null ? Collections.emptyList() : member.getRoles(); + final List blacklist = jda.getPlugin().getSettings().getDiscordRolesBlacklist(); + final boolean invert = jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist(); + + for (final Role role : roles) { + final boolean blacklisted = blacklist.contains(role.getName()) || blacklist.contains(role.getId()); + if ((blacklisted && !invert) || (!blacklisted && invert)) { + continue; + } + + if (fail != null && fail.test(role)) { + continue; + } + + return role; + } + + return null; + } + /** * Gets the highest role of a given member or an empty string if the member has no roles. * @@ -133,27 +155,8 @@ public static CompletableFuture createWebhook(TextChannel channel, Stri * @return The highest role or blank string. */ public static String getRoleFormat(final JDADiscordService jda, Member member) { - final List roles = member == null ? null : member.getRoles(); - - for (final Role role : roles) { - final Boolean contains = jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getName()) || - jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getId()); - if ((!jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && !contains) - || (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && contains)) { - String alias = jda.getPlugin().getSettings().getDiscordRoleAlias(role.getName()); - if (alias == "") { - alias = jda.getPlugin().getSettings().getDiscordRoleAlias(role.getId()); - } - if (alias != "") { - return alias; - } else { - return role.getName(); - } - } - } - - return ""; - + final Role role = getHighestRole(jda, member, null); + return role != null ? jda.getSettings().getRoleAlias(role) : ""; } /** @@ -163,35 +166,12 @@ public static String getRoleFormat(final JDADiscordService jda, Member member) { * @return The bukkit color code or blank string. */ public static String getRoleColorFormat(final JDADiscordService jda, Member member) { - if (member == null || member.getColorRaw() == Role.DEFAULT_COLOR_RAW) { + final Role topRole = getHighestRole(jda, member, role -> role.getColorRaw() == Role.DEFAULT_COLOR_RAW); + if (topRole == null || topRole.getColorRaw() == Role.DEFAULT_COLOR_RAW) { return ""; } - final List roles = member == null ? null : member.getRoles(); - - int color = Role.DEFAULT_COLOR_RAW; - for (final Role role : roles) { - final Boolean contains = jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getName()) || - jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getId()); - if ((!jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && !contains) - || (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && contains) - && role.getColorRaw() != Role.DEFAULT_COLOR_RAW) { - - color = role.getColorRaw(); - break; - - } - } - - if (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && jda.getPlugin().getSettings().getDiscordRolesBlacklist().isEmpty()) { - color = member.getColorRaw(); - } - - if (color == Role.DEFAULT_COLOR_RAW) { - return ""; - } - - final int rawColor = 0xff000000 | color; + final int rawColor = 0xff000000 | topRole.getColorRaw(); if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_1_R01)) { // Essentials' FormatUtil allows us to not have to use bungee's chatcolor since bukkit's own one doesn't support rgb @@ -215,14 +195,13 @@ public static boolean hasRoles(Member member, List roleDefinitions) { final List roles = member.getRoles(); for (String roleDefinition : roleDefinitions) { roleDefinition = roleDefinition.trim(); - final boolean id = NumberUtil.isNumeric(roleDefinition); if (roleDefinition.equals("*") || member.getId().equals(roleDefinition)) { return true; } for (final Role role : roles) { - if (role.getId().equals(roleDefinition) || (!id && role.getName().equalsIgnoreCase(roleDefinition))) { + if (matchesRole(role, roleDefinition)) { return true; } } @@ -230,6 +209,15 @@ public static boolean hasRoles(Member member, List roleDefinitions) { return false; } + /** + * Checks if the provided role matches the provided role definition (string representation of id or the role's name) + * + * @return true if the provided definition matches the provided role. + */ + public static boolean matchesRole(Role role, String roleDefinition) { + return role.getId().equals(roleDefinition) || role.getName().equalsIgnoreCase(roleDefinition); + } + public static String getAvatarUrl(final JDADiscordService jda, final Player player) { return jda.getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()).replace("{name}", player.getName()); } diff --git a/EssentialsDiscord/src/main/resources/config.yml b/EssentialsDiscord/src/main/resources/config.yml index 25cfbee6622..331be18df2c 100644 --- a/EssentialsDiscord/src/main/resources/config.yml +++ b/EssentialsDiscord/src/main/resources/config.yml @@ -224,17 +224,17 @@ commands: # If this is set to false and a message from Discord only contains an image/file and not any text, nothing will be sent. show-discord-attachments: true -# List of roles which are used for the top role placeholders. If this is empty, all roles will be allowed. +# A list of roles which should be ignored by the {color} and {role} placeholders. +# for the Discord->MC chat format. discord-role-blacklist: - - "1234123412341234123" + - "123456789012345678" - "Members" -invert-discord-role-blacklist: false - -# Aliases for Discord roles before they are sent to Minecraft. -discord-roles-aliases: [] -# Admins: "Admin" -# Members: "Member" +# Role aliases allow you to replace the role names with something different in the Discord->MC chat relay format. +# If you are using role aliases, make sure to remove the '#' at the start to allow the setting to be read. +discord-roles-aliases: +# "123456789012345678": "&c&lAdmin" +# "Members": "Member" # A list of roles allowed to send Minecraft color/formatting codes from Discord to MC. # This applies to all aspects such as that Discord->MC chat relay as well as commands.