diff --git a/src/main/java/meteordevelopment/meteorclient/mixin/ChatHudMixin.java b/src/main/java/meteordevelopment/meteorclient/mixin/ChatHudMixin.java index d9680f183c..f8268e978a 100644 --- a/src/main/java/meteordevelopment/meteorclient/mixin/ChatHudMixin.java +++ b/src/main/java/meteordevelopment/meteorclient/mixin/ChatHudMixin.java @@ -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; @@ -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; @@ -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(); @@ -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(); @@ -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 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(); + } } diff --git a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java index 4fb3db1673..16ad357c1f 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java @@ -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; @@ -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; @@ -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."); @@ -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); } @@ -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 messages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getMessages(); - if (messages.isEmpty() || i > messages.size() - 1) return null; + List 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 visible = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getVisibleMessages(); - List list = ChatMessages.breakRenderedChatMessageLines(originalMessage, MathHelper.floor((double)mc.inGameHud.getChatHud().getWidth() / mc.inGameHud.getChatHud().getChatScale()), mc.textRenderer); - List 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;