From d72ad289ec8384cd52f5251b5ddd8886cb95d1e7 Mon Sep 17 00:00:00 2001 From: AlexDev_ <56083016+alexdev03@users.noreply.github.com> Date: Thu, 14 Dec 2023 23:41:00 +0100 Subject: [PATCH] feat: Improve placeholder system, add `luck_perms_meta` placeholder (#125) --- .../velocitab/config/Placeholder.java | 39 ++++++++++++++++--- .../velocitab/hook/LuckPermsHook.java | 5 +++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/william278/velocitab/config/Placeholder.java b/src/main/java/net/william278/velocitab/config/Placeholder.java index dede531..f22c31c 100644 --- a/src/main/java/net/william278/velocitab/config/Placeholder.java +++ b/src/main/java/net/william278/velocitab/config/Placeholder.java @@ -23,6 +23,8 @@ import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; import net.william278.velocitab.Velocitab; import net.william278.velocitab.player.TabPlayer; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.function.TriFunction; import org.jetbrains.annotations.NotNull; import org.slf4j.event.Level; @@ -30,6 +32,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.concurrent.CompletableFuture; import java.util.function.BiFunction; +import java.util.regex.Matcher; import java.util.regex.Pattern; public enum Placeholder { @@ -53,26 +56,50 @@ public enum Placeholder { ROLE_WEIGHT((plugin, player) -> player.getRoleWeightString()), SERVER_GROUP((plugin, player) -> player.getServerGroup(plugin)), SERVER_GROUP_INDEX((plugin, player) -> Integer.toString(player.getServerGroupPosition(plugin))), - DEBUG_TEAM_NAME((plugin, player) -> plugin.getFormatter().escape(player.getLastTeamName().orElse(""))); + DEBUG_TEAM_NAME((plugin, player) -> plugin.getFormatter().escape(player.getLastTeamName().orElse(""))), + LUCK_PERMS_META_((param, plugin, player) -> plugin.getLuckPermsHook().map(hook -> hook.getMeta(player.getPlayer(), param)) + .orElse("")), + ; /** * Function to replace placeholders with a real value */ - private final BiFunction replacer; - private final static Pattern pattern = Pattern.compile("%.*?%"); + private final TriFunction replacer; + private final boolean parameterised; + private final Pattern pattern; + private final static Pattern checkPlaceholders = Pattern.compile("%.*?%"); Placeholder(@NotNull BiFunction replacer) { - this.replacer = replacer; + this.parameterised = false; + this.replacer = (text, player, plugin) -> replacer.apply(player, plugin); + this.pattern = Pattern.compile("%" + this.name().toLowerCase() + "%"); + } + + Placeholder(@NotNull TriFunction parameterisedReplacer) { + this.parameterised = true; + this.replacer = parameterisedReplacer; + this.pattern = Pattern.compile("%" + this.name().toLowerCase() + "[^%]+%", Pattern.CASE_INSENSITIVE); } public static CompletableFuture replace(@NotNull String format, @NotNull Velocitab plugin, @NotNull TabPlayer player) { + for (Placeholder placeholder : values()) { - format = format.replace("%" + placeholder.name().toLowerCase() + "%", placeholder.replacer.apply(plugin, player)); + Matcher matcher = placeholder.pattern.matcher(format); + if (placeholder.parameterised) { + // Replace the placeholder with the result of the replacer function with the parameter + format = matcher.replaceAll(matchResult -> + placeholder.replacer.apply(StringUtils.chop(matchResult.group().replace("%" + placeholder.name().toLowerCase(), "")) + , plugin, player)); + } else { + // Replace the placeholder with the result of the replacer function + format = matcher.replaceAll(matchResult -> placeholder.replacer.apply(null, plugin, player)); + } + } final String replaced = format; - if (!pattern.matcher(replaced).find()) { + if (!checkPlaceholders.matcher(replaced).find()) { return CompletableFuture.completedFuture(replaced); } diff --git a/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java b/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java index 1cf74ba..145d84e 100644 --- a/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java +++ b/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java @@ -80,6 +80,11 @@ public class LuckPermsHook extends Hook { ); } + @Nullable + public String getMeta(@NotNull Player player, @NotNull String key) { + return getUser(player.getUniqueId()).getCachedData().getMetaData().getMetaValue(key); + } + public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) { // Prevent duplicate events if (lastUpdate.getOrDefault(event.getUser().getUniqueId(), 0L) > System.currentTimeMillis() - 100) {