Search in sources :

Example 1 with ChatHudLine

use of net.minecraft.client.gui.hud.ChatHudLine in project meteor-client by MeteorDevelopment.

the class BetterChat method appendAntiSpam.

private Text appendAntiSpam(Text text, int index) {
    List<ChatHudLine<OrderedText>> visibleMessages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getVisibleMessages();
    if (visibleMessages.isEmpty() || index < 0 || index > visibleMessages.size() - 1)
        return null;
    ChatHudLine<OrderedText> visibleMessage = visibleMessages.get(index);
    LiteralText parsed = new LiteralText("");
    visibleMessage.getText().accept((i, style, codePoint) -> {
        parsed.append(new LiteralText(new String(Character.toChars(codePoint))).setStyle(style));
        return true;
    });
    String oldMessage = parsed.getString();
    String newMessage = text.getString();
    if (oldMessage.equals(newMessage)) {
        return parsed.append(new LiteralText(" (2)").formatted(Formatting.GRAY));
    } else {
        Matcher matcher = Pattern.compile(".*(\\([0-9]+\\)$)").matcher(oldMessage);
        if (!matcher.matches())
            return null;
        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)) {
            for (int i = 0; i < counter.length(); i++) parsed.getSiblings().remove(parsed.getSiblings().size() - 1);
            return parsed.append(new LiteralText(" (" + (number + 1) + ")").formatted(Formatting.GRAY));
        }
    }
    return null;
}
Also used : ChatHudAccessor(meteordevelopment.meteorclient.mixin.ChatHudAccessor) Matcher(java.util.regex.Matcher) ChatHudLine(net.minecraft.client.gui.hud.ChatHudLine)

Example 2 with ChatHudLine

use of net.minecraft.client.gui.hud.ChatHudLine in project EdenClient by HahaOO7.

the class AntiSpam method onChat.

private void onChat(AddChatMessageCallback.ChatAddEvent event) {
    if (!enabled)
        return;
    List<ChatHudLine<OrderedText>> chatLines = event.getChatLines();
    var chatText = event.getChatText();
    if (chatText == null)
        return;
    if (chatLines.isEmpty())
        return;
    class JustGiveMeTheStringVisitor implements CharacterVisitor {

        final StringBuilder sb = new StringBuilder();

        @Override
        public boolean accept(int index, Style style, int codePoint) {
            sb.appendCodePoint(codePoint);
            return true;
        }

        @Override
        public String toString() {
            return sb.toString();
        }
    }
    ChatHud chat = MC.inGameHud.getChatHud();
    int maxTextLength = MathHelper.floor(chat.getWidth() / chat.getChatScale());
    List<OrderedText> newLines = ChatMessages.breakRenderedChatMessageLines(chatText, maxTextLength, MC.textRenderer);
    int spamCounter = 1;
    int matchingLines = 0;
    for (int i = chatLines.size() - 1; i >= 0; i--) {
        JustGiveMeTheStringVisitor oldLineVS = new JustGiveMeTheStringVisitor();
        chatLines.get(i).getText().accept(oldLineVS);
        String oldLine = oldLineVS.toString();
        if (matchingLines <= newLines.size() - 1) {
            JustGiveMeTheStringVisitor newLineVS = new JustGiveMeTheStringVisitor();
            newLines.get(matchingLines).accept(newLineVS);
            String newLine = newLineVS.toString();
            if (matchingLines < newLines.size() - 1) {
                if (oldLine.equals(newLine))
                    matchingLines++;
                else
                    matchingLines = 0;
                continue;
            }
            if (!oldLine.startsWith(newLine)) {
                matchingLines = 0;
                continue;
            }
            if (i > 0 && matchingLines == newLines.size() - 1) {
                JustGiveMeTheStringVisitor nextOldLineVS = new JustGiveMeTheStringVisitor();
                chatLines.get(i - 1).getText().accept(nextOldLineVS);
                String nextOldLine = nextOldLineVS.toString();
                String twoLines = oldLine + nextOldLine;
                String addedText = twoLines.substring(newLine.length());
                if (addedText.startsWith(" [x") && addedText.endsWith("]")) {
                    String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                    if (MathUtils.isInteger(oldSpamCounter)) {
                        spamCounter += Integer.parseInt(oldSpamCounter);
                        matchingLines++;
                        continue;
                    }
                }
            }
            if (oldLine.length() == newLine.length())
                spamCounter++;
            else {
                String addedText = oldLine.substring(newLine.length());
                if (!addedText.startsWith(" [x") || !addedText.endsWith("]")) {
                    matchingLines = 0;
                    continue;
                }
                String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                if (!MathUtils.isInteger(oldSpamCounter)) {
                    matchingLines = 0;
                    continue;
                }
                spamCounter += Integer.parseInt(oldSpamCounter);
            }
        }
        if (i + matchingLines >= i) {
            chatLines.subList(i, i + matchingLines + 1).clear();
        }
        matchingLines = 0;
    }
    if (spamCounter > 1) {
        chatText = new LiteralText("").append(chatText).append(new LiteralText(" [x" + spamCounter + "]"));
    }
    event.setChatText(chatText);
}
Also used : ChatHudLine(net.minecraft.client.gui.hud.ChatHudLine) CharacterVisitor(net.minecraft.text.CharacterVisitor) OrderedText(net.minecraft.text.OrderedText) Style(net.minecraft.text.Style) ChatHud(net.minecraft.client.gui.hud.ChatHud) LiteralText(net.minecraft.text.LiteralText)

Aggregations

ChatHudLine (net.minecraft.client.gui.hud.ChatHudLine)2 Matcher (java.util.regex.Matcher)1 ChatHudAccessor (meteordevelopment.meteorclient.mixin.ChatHudAccessor)1 ChatHud (net.minecraft.client.gui.hud.ChatHud)1 CharacterVisitor (net.minecraft.text.CharacterVisitor)1 LiteralText (net.minecraft.text.LiteralText)1 OrderedText (net.minecraft.text.OrderedText)1 Style (net.minecraft.text.Style)1