Skip to content

Commit

Permalink
add anti chat clear option to BetterChat
Browse files Browse the repository at this point in the history
  • Loading branch information
RealMuffinTime authored and Wide-Cat committed Jun 12, 2024
1 parent f08df5c commit c82f0c0
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public class BetterChat extends Module {
.build()
);

private final Setting<Boolean> antiClear = sgFilter.add(new BoolSetting.Builder()
.name("anti-clear")
.description("Prevents servers from clearing chat.")
.defaultValue(true)
.build()
);

private final Setting<Boolean> filterRegex = sgFilter.add(new BoolSetting.Builder()
.name("filter-regex")
.description("Filter out chat messages that match the regex filter.")
Expand Down Expand Up @@ -219,6 +226,7 @@ public class BetterChat extends Module {
);

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

Expand Down Expand Up @@ -249,6 +257,16 @@ private void onMessageReceive(ReceiveMessageEvent event) {
}
}

if (antiClear.get()) {
// more than two \n behind each other will be reduced to only two \n
String jsonString = Text.Serialization.toJsonString(message, mc.player.getRegistryManager());

Matcher antiClearMatcher = antiClearRegex.matcher(jsonString);
String replacedString = antiClearMatcher.replaceAll("\n\n");

message = (Text) Text.Serialization.fromJson(replacedString, mc.player.getRegistryManager());
}

if (antiSpam.get()) {
Text antiSpammed = appendAntiSpam(message);

Expand Down

0 comments on commit c82f0c0

Please sign in to comment.