Skip to content

Commit

Permalink
Undo backwards iteration; help #218
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbuilder1961 committed Feb 4, 2025
1 parent f498165 commit 20784a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/obro1961/chatpatches/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/obro1961/chatpatches/util/ChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ public static Text modifyMessage(@NotNull Text m) {
* corresponds to the most recent message
* only.</li>
* </ol>
* <li>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.</li>
* <li>Iterate through the last {@code attemptDistance}
* messages to find and condense (remove) any duplicates.</li>
* <ol>
* <li>If the incoming message is different from the
* iterated message, in terms of text or style data
Expand All @@ -314,6 +312,10 @@ public static Text modifyMessage(@NotNull Text m) {
* <li>Remove the message being condensed.</li>
* <li>Remove visible message(s), starting at the iterated
* index, until the next message (EoE) is reached.</li>
* <li>Decrement the index to prevent skipping the next
* message.</li>
* <li>Decrement the attempt distance to prevent checking
* extra messages.</li>
* </ol>
* <li>Update the incoming message with the new dupe counter,
* if the total dupe count is greater than 1.</li>
Expand All @@ -327,11 +329,11 @@ private static Text tryCondenseDupes(Text incoming) {
ChatHud chathud = MinecraftClient.getInstance().inGameHud.getChatHud();
ChatHudAccessor chat = (ChatHudAccessor) chathud;
List<ChatHudLine> messages = chat.chatpatches$getMessages();
List<Text> siblings = new ArrayList<>( incoming.getSiblings() ); // prevents UOEs on 1.20.3+ (#199)

if(!config.counter || messages.isEmpty())
return incoming;

List<Text> siblings = new ArrayList<>( incoming.getSiblings() ); // prevents UOEs on 1.20.3+ (#199)
List<ChatHudLine.Visible> visibles = chat.chatpatches$getVisibleMessages();
int attemptDistance =
switch(config.counterCompact ? config.counterCompactDistance : 1) {
Expand All @@ -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()) )
Expand All @@ -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
Expand Down

0 comments on commit 20784a6

Please sign in to comment.