Skip to content

Commit c470008

Browse files
Wide-CatRacoonDog
andcommitted
Fix issue with better chat and clientside messages
Co-authored-by: RacoonDog <[email protected]>
1 parent 546d0b9 commit c470008

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,22 @@
2323
import meteordevelopment.meteorclient.utils.Utils;
2424
import meteordevelopment.meteorclient.utils.misc.MeteorIdentifier;
2525
import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent;
26+
import meteordevelopment.meteorclient.utils.misc.text.TextVisitor;
2627
import meteordevelopment.meteorclient.utils.player.ChatUtils;
2728
import meteordevelopment.meteorclient.utils.render.color.Color;
2829
import meteordevelopment.orbit.EventHandler;
2930
import net.minecraft.client.gui.DrawContext;
3031
import net.minecraft.client.gui.hud.ChatHudLine;
3132
import net.minecraft.client.network.PlayerListEntry;
32-
import net.minecraft.text.ClickEvent;
33-
import net.minecraft.text.HoverEvent;
34-
import net.minecraft.text.MutableText;
35-
import net.minecraft.text.Text;
33+
import net.minecraft.text.*;
3634
import net.minecraft.util.Formatting;
3735
import net.minecraft.util.Identifier;
3836

3937
import java.text.SimpleDateFormat;
4038
import java.util.ArrayList;
4139
import java.util.Date;
4240
import java.util.List;
41+
import java.util.Optional;
4342
import java.util.regex.Matcher;
4443
import java.util.regex.Pattern;
4544
import java.util.regex.PatternSyntaxException;
@@ -226,7 +225,7 @@ public class BetterChat extends Module {
226225
);
227226

228227
private static final Pattern antiSpamRegex = Pattern.compile(" \\(([0-9]+)\\)$");
229-
private static final Pattern antiClearRegex = Pattern.compile("\\\\n(\\\\n|\\s)*\\\\n");
228+
private static final Pattern antiClearRegex = Pattern.compile("\\\\n(\\\\n|\\s)+\\\\n");
230229
private static final Pattern timestampRegex = Pattern.compile("^(<[0-9]{2}:[0-9]{2}>\\s)");
231230
private static final Pattern usernameRegex = Pattern.compile("^(?:<[0-9]{2}:[0-9]{2}>\\s)?<(.*?)>.*");
232231

@@ -258,13 +257,22 @@ private void onMessageReceive(ReceiveMessageEvent event) {
258257
}
259258

260259
if (antiClear.get()) {
261-
// more than two \n behind each other will be reduced to only two \n
262-
String jsonString = Text.Serialization.toJsonString(message, mc.player.getRegistryManager());
263-
264-
Matcher antiClearMatcher = antiClearRegex.matcher(jsonString);
265-
String replacedString = antiClearMatcher.replaceAll("\n\n");
266-
267-
message = (Text) Text.Serialization.fromJson(replacedString, mc.player.getRegistryManager());
260+
String messageString = message.getString();
261+
if (antiClearRegex.matcher(messageString).matches()) {
262+
MutableText newMessage = Text.empty();
263+
TextVisitor.visit(message, (text, style, string) -> {
264+
Matcher antiClearMatcher = antiClearRegex.matcher(string);
265+
if (antiClearMatcher.matches()) {
266+
// assume literal text content
267+
newMessage.append(Text.literal(antiClearMatcher.replaceAll("\n\n")).setStyle(style));
268+
} else {
269+
newMessage.append(text.copyContentOnly().setStyle(style));
270+
}
271+
272+
return Optional.empty();
273+
}, Style.EMPTY);
274+
message = newMessage;
275+
}
268276
}
269277

270278
if (antiSpam.get()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.utils.misc.text;
7+
8+
import net.minecraft.text.Style;
9+
import net.minecraft.text.Text;
10+
11+
import java.util.ArrayDeque;
12+
import java.util.Optional;
13+
import java.util.Queue;
14+
15+
/**
16+
* An extension of {@link net.minecraft.text.StringVisitable.StyledVisitor} with access to the underlying {@link Text} objects.
17+
* @param <T> the optional short circuit return type, to match the semantics of {@link net.minecraft.text.StringVisitable.Visitor} and {@link net.minecraft.text.StringVisitable.StyledVisitor}.
18+
* @author Crosby
19+
*/
20+
@FunctionalInterface
21+
public interface TextVisitor<T> {
22+
Optional<T> accept(Text text, Style style, String string);
23+
24+
static <T> Optional<T> visit(Text text, TextVisitor<T> visitor, Style baseStyle) {
25+
Queue<Text> queue = collectSiblings(text);
26+
return text.visit((style, string) -> visitor.accept(queue.remove(), style, string), baseStyle);
27+
}
28+
29+
/**
30+
* Collapses the tree of {@link Text} siblings into a one dimensional LIFO {@link Queue}
31+
* @param text the text
32+
* @return the text and its siblings in the order they appear when rendered.
33+
*/
34+
static Queue<Text> collectSiblings(Text text) {
35+
Queue<Text> queue = new ArrayDeque<>();
36+
collectSiblings(text, queue);
37+
return queue;
38+
}
39+
40+
private static void collectSiblings(Text text, Queue<Text> queue) {
41+
queue.add(text);
42+
for (Text sibling : text.getSiblings()) {
43+
collectSiblings(sibling, queue);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)