From 274f5bdad911a426187669b2e669fa05f1c2b053 Mon Sep 17 00:00:00 2001 From: Oskar Nordling Date: Sun, 26 Oct 2025 14:12:46 +0100 Subject: [PATCH] Use String.replace for Velocity server placeholder --- .../handler/VelocityStaffChatHandler.java | 13 +++++++++-- .../velocity/util/VelocityFormatUtils.java | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/velocity/java/me/oskar3123/staffchat/velocity/util/VelocityFormatUtils.java diff --git a/src/velocity/java/me/oskar3123/staffchat/velocity/handler/VelocityStaffChatHandler.java b/src/velocity/java/me/oskar3123/staffchat/velocity/handler/VelocityStaffChatHandler.java index c425594..95d3aaa 100644 --- a/src/velocity/java/me/oskar3123/staffchat/velocity/handler/VelocityStaffChatHandler.java +++ b/src/velocity/java/me/oskar3123/staffchat/velocity/handler/VelocityStaffChatHandler.java @@ -5,11 +5,11 @@ import java.util.HashSet; import java.util.Set; import java.util.UUID; import java.util.function.Function; -import me.oskar3123.staffchat.util.FormatUtils; import me.oskar3123.staffchat.util.Permissions; import me.oskar3123.staffchat.velocity.VelocityStaffChat; import me.oskar3123.staffchat.velocity.event.VelocityStaffChatEvent; import me.oskar3123.staffchat.velocity.event.VelocityStaffChatEvent.StaffChatResult; +import me.oskar3123.staffchat.velocity.util.VelocityFormatUtils; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -61,7 +61,16 @@ public class VelocityStaffChatHandler { private void broadcastStaffMessage(Player player, String format, String message) { String formattedMessage = - FormatUtils.replacePlaceholders(format, Function.identity(), player::getUsername, message); + VelocityFormatUtils.replacePlaceholders( + format, + Function.identity(), + () -> + player + .getCurrentServer() + .map(connection -> connection.getServerInfo().getName()) + .orElse(""), + player::getUsername, + message); TextComponent component = LegacyComponentSerializer.legacyAmpersand().deserialize(formattedMessage); diff --git a/src/velocity/java/me/oskar3123/staffchat/velocity/util/VelocityFormatUtils.java b/src/velocity/java/me/oskar3123/staffchat/velocity/util/VelocityFormatUtils.java new file mode 100644 index 0000000..075d5eb --- /dev/null +++ b/src/velocity/java/me/oskar3123/staffchat/velocity/util/VelocityFormatUtils.java @@ -0,0 +1,23 @@ +package me.oskar3123.staffchat.velocity.util; + +import java.util.function.Function; +import java.util.function.Supplier; +import me.oskar3123.staffchat.util.FormatUtils; +import me.oskar3123.staffchat.util.StringUtils; + +public final class VelocityFormatUtils { + + private VelocityFormatUtils() {} + + public static String replacePlaceholders( + String string, + Function colorize, + Supplier serverSupplier, + Supplier nameSupplier, + String message) { + string = + string.replace( + "{SERVER}", StringUtils.sanitize(colorize.apply(serverSupplier.get()))); + return FormatUtils.replacePlaceholders(string, colorize, nameSupplier, message); + } +}