From 20784a66382c011eb2877ea5ff5f5003e5a976f8 Mon Sep 17 00:00:00 2001 From: OBro1961 Date: Mon, 3 Feb 2025 21:59:16 -0600 Subject: [PATCH] Undo backwards iteration; help #218 --- .../obro1961/chatpatches/config/Config.java | 2 +- .../obro1961/chatpatches/util/ChatUtils.java | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/obro1961/chatpatches/config/Config.java b/src/main/java/obro1961/chatpatches/config/Config.java index 0b67913..8065e23 100644 --- a/src/main/java/obro1961/chatpatches/config/Config.java +++ b/src/main/java/obro1961/chatpatches/config/Config.java @@ -246,7 +246,7 @@ public void sendBoundaryLine() { * config. */ public static void read() { - // warning: modified recently, watch out for bugs! + // modified recently, watch out for bugs! if(Files.exists(PATH)) { try { String rawData = Files.readString(PATH); diff --git a/src/main/java/obro1961/chatpatches/util/ChatUtils.java b/src/main/java/obro1961/chatpatches/util/ChatUtils.java index 7711474..8abc216 100644 --- a/src/main/java/obro1961/chatpatches/util/ChatUtils.java +++ b/src/main/java/obro1961/chatpatches/util/ChatUtils.java @@ -298,10 +298,8 @@ public static Text modifyMessage(@NotNull Text m) { * corresponds to the most recent message * only. * - *
  • Iterate backwards through the last {@code attemptDistance} - * messages to find and condense (remove) any duplicates. Goes - * backwards to avoid skipping messages and needing to decrement - * the attempt distance after every match.
  • + *
  • Iterate through the last {@code attemptDistance} + * messages to find and condense (remove) any duplicates.
  • *
      *
    1. If the incoming message is different from the * iterated message, in terms of text or style data @@ -314,6 +312,10 @@ public static Text modifyMessage(@NotNull Text m) { *
    2. Remove the message being condensed.
    3. *
    4. Remove visible message(s), starting at the iterated * index, until the next message (EoE) is reached.
    5. + *
    6. Decrement the index to prevent skipping the next + * message.
    7. + *
    8. Decrement the attempt distance to prevent checking + * extra messages.
    9. *
    *
  • Update the incoming message with the new dupe counter, * if the total dupe count is greater than 1.
  • @@ -327,11 +329,11 @@ private static Text tryCondenseDupes(Text incoming) { ChatHud chathud = MinecraftClient.getInstance().inGameHud.getChatHud(); ChatHudAccessor chat = (ChatHudAccessor) chathud; List messages = chat.chatpatches$getMessages(); - List siblings = new ArrayList<>( incoming.getSiblings() ); // prevents UOEs on 1.20.3+ (#199) if(!config.counter || messages.isEmpty()) return incoming; + List siblings = new ArrayList<>( incoming.getSiblings() ); // prevents UOEs on 1.20.3+ (#199) List visibles = chat.chatpatches$getVisibleMessages(); int attemptDistance = switch(config.counterCompact ? config.counterCompactDistance : 1) { @@ -343,9 +345,8 @@ private static Text tryCondenseDupes(Text incoming) { // iterate through the last `attemptDistance` messages to find and condense (remove) any duplicates - // goes backwards to avoid skipping messages and needing to decrement the attempt distance int dupeCount = 1; - for(int i = attemptDistance - 1; i >= 0 && i < messages.size(); i--) { + for(int i = 0; i < attemptDistance && i < messages.size(); i++) { Text msg = messages.get(i).content(); if( !getPart(incoming, MESSAGE_INDEX).getString().equalsIgnoreCase(getPart(msg, MESSAGE_INDEX).getString()) ) @@ -362,7 +363,10 @@ else if( config.counterCheckStyle && !copyWithoutContent(incoming).equals(copyWi // remove the visible message(s) of the message being condensed do visibles.remove(i); - while(!visibles.isEmpty() && !visibles.get(i).endOfEntry()); // continue removing them until the next message (EoE) is reached + while(i < visibles.size() && !visibles.get(i).endOfEntry()); // continue removing them until the next message (EoE) is reached + + i--; // we removed the first message, but we don't want to skip the next one + attemptDistance--; // but we also don't want to check messages we shouldn't be checking } // update the incoming message with the new dupe counter