Search in sources :

Example 1 with ChatHud

use of net.minecraft.client.gui.hud.ChatHud 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

ChatHud (net.minecraft.client.gui.hud.ChatHud)1 ChatHudLine (net.minecraft.client.gui.hud.ChatHudLine)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