diff --git a/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java b/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java index b46da53a06..bf2283e4f6 100644 --- a/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java +++ b/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java @@ -6,6 +6,7 @@ package meteordevelopment.meteorclient.utils.player; import baritone.api.BaritoneAPI; +import com.mojang.brigadier.StringReader; import meteordevelopment.meteorclient.MeteorClient; import meteordevelopment.meteorclient.mixininterface.IChatHud; import meteordevelopment.meteorclient.systems.config.Config; @@ -30,7 +31,7 @@ public class ChatUtils { @PostInit public static void init() { - PREFIX = Text.literal("") + PREFIX = Text.empty() .setStyle(Style.EMPTY.withFormatting(Formatting.GRAY)) .append("[") .append(Text.literal("Meteor").setStyle(Style.EMPTY.withColor(TextColor.fromRgb(MeteorClient.ADDON.color.getPacked())))) @@ -121,19 +122,19 @@ public static void sendMsg(int id, Formatting color, String message, Object... a } public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable Formatting prefixColor, Formatting messageColor, String messageContent, Object... args) { - sendMsg(id, prefixTitle, prefixColor, formatMsg(messageContent, messageColor, args), messageColor); + MutableText message = formatMsg(String.format(messageContent, args), messageColor); + sendMsg(id, prefixTitle, prefixColor, message); } public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable Formatting prefixColor, String messageContent, Formatting messageColor) { - MutableText message = Text.literal(messageContent); - message.setStyle(message.getStyle().withFormatting(messageColor)); + MutableText message = formatMsg(messageContent, messageColor); sendMsg(id, prefixTitle, prefixColor, message); } public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable Formatting prefixColor, Text msg) { if (mc.world == null) return; - MutableText message = Text.literal(""); + MutableText message = Text.empty(); message.append(getPrefix()); if (prefixTitle != null) message.append(getCustomPrefix(prefixTitle, prefixColor)); message.append(msg); @@ -144,7 +145,7 @@ public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable Forma } private static MutableText getCustomPrefix(String prefixTitle, Formatting prefixColor) { - MutableText prefix = Text.literal(""); + MutableText prefix = Text.empty(); prefix.setStyle(prefix.getStyle().withFormatting(Formatting.GRAY)); prefix.append("["); @@ -196,19 +197,50 @@ private static Text getPrefix() { return PREFIX; } - private static String formatMsg(String format, Formatting defaultColor, Object... args) { - String msg = String.format(format, args); - msg = msg.replace("(default)", defaultColor.toString()); - msg = msg.replace("(highlight)", Formatting.WHITE.toString()); - msg = msg.replace("(underline)", Formatting.UNDERLINE.toString()); + private static MutableText formatMsg(String message, Formatting defaultColor) { + StringReader reader = new StringReader(message); + MutableText text = Text.empty(); + Style style = Style.EMPTY.withFormatting(defaultColor); + StringBuilder result = new StringBuilder(); + boolean formatting = false; + while (reader.canRead()) { + char c = reader.read(); + if (c == '(') { + text.append(Text.literal(result.toString()).setStyle(style)); + result.setLength(0); + result.append(c); + formatting = true; + } else { + result.append(c); + + if (formatting && c == ')') { + switch (result.toString()) { + case "(default)" -> { + style = style.withFormatting(defaultColor); + result.setLength(0); + } + case "(highlight)" -> { + style = style.withFormatting(Formatting.WHITE); + result.setLength(0); + } + case "(underline)" -> { + style = style.withFormatting(Formatting.UNDERLINE); + result.setLength(0); + } + } + formatting = false; + } + } + } + + if (!result.isEmpty()) text.append(Text.literal(result.toString()).setStyle(style)); - return msg; + return text; } public static MutableText formatCoords(Vec3d pos) { String coordsString = String.format("(highlight)(underline)%.0f, %.0f, %.0f(default)", pos.x, pos.y, pos.z); - coordsString = formatMsg(coordsString, Formatting.GRAY); - MutableText coordsText = Text.literal(coordsString); + MutableText coordsText = formatMsg(coordsString, Formatting.GRAY); coordsText.setStyle(coordsText.getStyle() .withFormatting(Formatting.BOLD) .withClickEvent(new ClickEvent(