Skip to content

Commit

Permalink
Fix anti spam (MeteorDevelopment#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wide-Cat authored Jun 30, 2023
1 parent 12b4b41 commit 66e0b88
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.client.option.ChatVisibility;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.network.message.MessageSignatureData;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
Expand All @@ -38,6 +39,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -83,7 +85,13 @@ private void onAddMessage(Text message, @Nullable MessageSignatureData signature
if (event.isCancelled()) info.cancel();
else {
visibleMessages.removeIf(msg -> ((IChatHudLine) (Object) msg).getId() == nextId && nextId != 0);
messages.removeIf(msg -> ((IChatHudLine) (Object) msg).getId() == nextId && nextId != 0);

for (int i = messages.size() - 1; i > -1 ; i--) {
if (((IChatHudLine) (Object) messages.get(i)).getId() == nextId && nextId != 0) {
messages.remove(i);
Modules.get().get(BetterChat.class).lines.remove(i);
}
}

if (event.isModified()) {
info.cancel();
Expand All @@ -95,7 +103,8 @@ private void onAddMessage(Text message, @Nullable MessageSignatureData signature
}
}

@ModifyExpressionValue(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V", slice = @Slice(from = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/hud/ChatHud;visibleMessages:Ljava/util/List;")), at = @At(value = "INVOKE", target = "Ljava/util/List;size()I"))
@ModifyExpressionValue(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
slice = @Slice(from = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/hud/ChatHud;visibleMessages:Ljava/util/List;")), at = @At(value = "INVOKE", target = "Ljava/util/List;size()I"))
private int addMessageListSizeProxy(int size) {
BetterChat betterChat = Modules.get().get(BetterChat.class);
if (betterChat.isLongerChat() && betterChat.getChatLength() >= 100) return size - betterChat.getChatLength();
Expand Down Expand Up @@ -210,4 +219,34 @@ private static Identifier getMessageTexture(String message) {
private MessageIndicator onRender_modifyIndicator(MessageIndicator indicator) {
return Modules.get().get(NoRender.class).noMessageSignatureIndicator() ? null : indicator;
}

// Anti spam

@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/ChatHud;isChatFocused()Z"), locals = LocalCapture.CAPTURE_FAILSOFT)
private void onBreakChatMessageLines(Text message, MessageSignatureData signature, int ticks, MessageIndicator indicator, boolean refresh, CallbackInfo ci, int i, List<OrderedText> list) {
Modules.get().get(BetterChat.class).lines.add(0, list.size());
}

@Inject(method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
slice = @Slice(from = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/hud/ChatHud;messages:Ljava/util/List;")), at = @At(value = "INVOKE", target = "Ljava/util/List;remove(I)Ljava/lang/Object;"))
private void onRemoveMessage(Text message, MessageSignatureData signature, int ticks, MessageIndicator indicator, boolean refresh, CallbackInfo ci) {
BetterChat betterChat = Modules.get().get(BetterChat.class);
int size = betterChat.lines.size() - (betterChat.isLongerChat() && betterChat.getChatLength() >= 100 ? betterChat.getChatLength() : 0);

while (size > 100) {
betterChat.lines.remove(size - 1);
size--;
}
}

@Inject(method = "clear", at = @At("HEAD"))
private void onClear(boolean clearHistory, CallbackInfo ci) {
Modules.get().get(BetterChat.class).lines.clear();
}

@Inject(method = "refresh", at = @At("HEAD"))
private void onRefresh(CallbackInfo ci) {
Modules.get().get(BetterChat.class).lines.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import it.unimi.dsi.fastutil.chars.Char2CharMap;
import it.unimi.dsi.fastutil.chars.Char2CharOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import meteordevelopment.meteorclient.commands.Commands;
import meteordevelopment.meteorclient.events.game.ReceiveMessageEvent;
import meteordevelopment.meteorclient.events.game.SendMessageEvent;
Expand All @@ -18,10 +20,11 @@
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.client.gui.hud.ChatHudLine;
import net.minecraft.client.util.ChatMessages;
import net.minecraft.text.*;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -210,6 +213,7 @@ public class BetterChat extends Module {

private final Char2CharMap SMALL_CAPS = new Char2CharOpenHashMap();
private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
public final IntList lines = new IntArrayList();

public BetterChat() {
super(Categories.Misc, "better-chat", "Improves your chat experience in various ways.");
Expand All @@ -233,15 +237,6 @@ 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);
}

if (playerHeads.get()) {
message = Text.literal(" ").append(message);
}
Expand All @@ -254,62 +249,72 @@ 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);
}

event.setMessage(message);
}

/**
* @author Crosby
* Adding author tag because this is spaghetti code
*/

private Text appendAntiSpam(Text text) {
Text returnText = null;
int messageIndex = -1;
MutableText originalMessage = null;

for (int i = 0; i < antiSpamDepth.get(); i++) {
List<ChatHudLine> messages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getMessages();
if (messages.isEmpty() || i > messages.size() - 1) return null;
List<ChatHudLine> messages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getMessages();
if (messages.isEmpty()) return null;

for (int i = 0; i < Math.min(antiSpamDepth.get(), messages.size()); i++) {
String stringToCheck = messages.get(i).content().copy().getString();

MutableText message = messages.get(i).content().copy();
String oldMessage = message.getString();
String newMessage = text.getString();
Matcher timestampMatcher = timestampRegex.matcher(stringToCheck);
if (timestampMatcher.find()) {
stringToCheck = stringToCheck.substring(8);
}

if (oldMessage.equals(newMessage)) {
originalMessage = message.copy();
if (text.getString().equals(stringToCheck)) {
messageIndex = i;
returnText = message.append(Text.literal(" (2)").formatted(Formatting.GRAY));
returnText = text.copy().append(Text.literal(" (2)").formatted(Formatting.GRAY));
break;
}
else {
Matcher matcher = antiSpamRegex.matcher(oldMessage);

if (!matcher.matches()) continue;
Matcher matcher = antiSpamRegex.matcher(stringToCheck);
if (!matcher.matches() && !matcher.find()) continue;

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

String counter = " (" + number + ")";

if (oldMessage.substring(0, oldMessage.length() - counter.length()).equals(newMessage)) {
message.getSiblings().remove(message.getSiblings().size() - 1);
originalMessage = message.copy();
if (stringToCheck.substring(0, stringToCheck.length() - counter.length()).equals(text.getString())) {
messageIndex = i;
returnText = message.append(Text.literal(" (" + (number + 1) + ")").formatted(Formatting.GRAY));
returnText = text.copy().append(Text.literal(" (" + (number + 1) + ")").formatted(Formatting.GRAY));
break;
}
}
}

if (returnText != null) {
((ChatHudAccessor) mc.inGameHud.getChatHud()).getMessages().remove(messageIndex);
List<ChatHudLine.Visible> visible = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getVisibleMessages();

List<OrderedText> list = ChatMessages.breakRenderedChatMessageLines(originalMessage, MathHelper.floor((double)mc.inGameHud.getChatHud().getWidth() / mc.inGameHud.getChatHud().getChatScale()), mc.textRenderer);
List<ChatHudLine.Visible> visibleMessages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getVisibleMessages();
int lines = Math.min(list.size(), visibleMessages.size());
int start = -1;
for (int i = 0; i < messageIndex; i++) {
start += lines.get(i);
}

for (int i = 0; i < lines; i++) {
visibleMessages.remove(messageIndex);
int i = lines.get(messageIndex);
while (i > 0) {
visible.remove(start + 1);
i--;
}

messages.remove(messageIndex);
lines.remove(messageIndex);
}

return returnText;
Expand Down

0 comments on commit 66e0b88

Please sign in to comment.