Skip to content

Commit

Permalink
Fix ChatUtils formatting in log files (MeteorDevelopment#4061)
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored Sep 10, 2023
1 parent e5b64cc commit 52595d0
Showing 1 changed file with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()))))
Expand Down Expand Up @@ -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);
Expand All @@ -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("[");
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 52595d0

Please sign in to comment.