Use String.replace for Velocity server placeholder

This commit is contained in:
2025-10-26 14:12:46 +01:00
parent 89e26a8958
commit 274f5bdad9
2 changed files with 34 additions and 2 deletions
@@ -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);
@@ -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<String, String> colorize,
Supplier<String> serverSupplier,
Supplier<String> nameSupplier,
String message) {
string =
string.replace(
"{SERVER}", StringUtils.sanitize(colorize.apply(serverSupplier.get())));
return FormatUtils.replacePlaceholders(string, colorize, nameSupplier, message);
}
}