Skip to content

Commit

Permalink
Better Chat optimisations (MeteorDevelopment#3942)
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored Aug 12, 2023
1 parent 96ba7da commit 17f634c
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public class BetterChat extends Module {
.build()
);

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

private final Char2CharMap SMALL_CAPS = new Char2CharOpenHashMap();
Expand All @@ -229,8 +229,9 @@ private void onMessageReceive(ReceiveMessageEvent event) {
Text message = event.getMessage();

if (filterRegex.get()) {
String messageString = message.getString();
for (Pattern pattern : filterRegexList) {
if (pattern.matcher(message.getString()).find()) {
if (pattern.matcher(messageString).find()) {
event.cancel();
return;
}
Expand All @@ -250,19 +251,17 @@ private void onMessageReceive(ReceiveMessageEvent event) {
}

if (timestamps.get()) {
Matcher matcher = timestampRegex.matcher(message.getString());
if (matcher.matches()) message.getSiblings().subList(0, 8).clear();

Text timestamp = Text.literal("<" + dateFormat.format(new Date()) + "> ").formatted(Formatting.GRAY);

message = Text.literal("").append(timestamp).append(message);
message = Text.empty().append(timestamp).append(message);
}

event.setMessage(message);
}


private Text appendAntiSpam(Text text) {
String textString = text.getString();
Text returnText = null;
int messageIndex = -1;

Expand All @@ -277,21 +276,18 @@ private Text appendAntiSpam(Text text) {
stringToCheck = stringToCheck.substring(8);
}

if (text.getString().equals(stringToCheck)) {
if (textString.equals(stringToCheck)) {
messageIndex = i;
returnText = text.copy().append(Text.literal(" (2)").formatted(Formatting.GRAY));
break;
}
else {
} else {
Matcher matcher = antiSpamRegex.matcher(stringToCheck);
if (!matcher.matches() && !matcher.find()) continue;
if (!matcher.find()) continue;

String group = matcher.group(matcher.groupCount());
int number = Integer.parseInt(group.substring(1, group.length() - 1));

String counter = " (" + number + ")";
int number = Integer.parseInt(group);

if (stringToCheck.substring(0, stringToCheck.length() - counter.length()).equals(text.getString())) {
if (stringToCheck.substring(0, matcher.start()).equals(textString)) {
messageIndex = i;
returnText = text.copy().append(Text.literal(" (" + (number + 1) + ")").formatted(Formatting.GRAY));
break;
Expand Down

0 comments on commit 17f634c

Please sign in to comment.