Skip to content

Commit d5f53c0

Browse files
authored
Fix multiple username transformation (#683)
1 parent 0bbd467 commit d5f53c0

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/main/java/codes/biscuit/skyblockaddons/listeners/PlayerListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,13 @@ private void playerSymbolsDisplay(ClientChatReceivedEvent e, String unformattedT
511511
if(!suffix.equals(" ")) {
512512
String finalSuffix = suffix;
513513
String finalUsername = username;
514-
TextUtils.recursiveTransformChatComponent(oldMessage, component -> {
514+
TextUtils.transformAnyChatComponent(oldMessage, component -> {
515515
if (component instanceof ChatComponentText & ((ChatComponentText)component).text.contains(finalUsername)) {
516516
ChatComponentText textComponent = (ChatComponentText) component;
517517
textComponent.text = textComponent.text.replace(finalUsername, finalUsername + finalSuffix);
518+
return true;
518519
}
520+
return false;
519521
}
520522
);
521523
}

src/main/java/codes/biscuit/skyblockaddons/utils/TextUtils.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.text.ParseException;
1010
import java.util.*;
1111
import java.util.function.Consumer;
12+
import java.util.function.Predicate;
1213
import java.util.regex.Matcher;
1314
import java.util.regex.Pattern;
1415

@@ -453,10 +454,28 @@ private static String mergeFormats(String firstFormat, String secondFormat) {
453454
* @param action action to be performed
454455
* @author Sychic
455456
*/
456-
public static void recursiveTransformChatComponent(IChatComponent chatComponent, Consumer<IChatComponent> action) {
457+
public static void transformAllChatComponents(IChatComponent chatComponent, Consumer<IChatComponent> action) {
457458
action.accept(chatComponent);
458459
for (IChatComponent sibling : chatComponent.getSiblings()) {
459-
recursiveTransformChatComponent(sibling, action);
460+
transformAllChatComponents(sibling, action);
460461
}
461462
}
463+
464+
/**
465+
* Recursively searches for a chat component to transform based on a given Predicate.
466+
*
467+
* Important to note that this function will stop on the first successful transformation, unlike {@link #transformAllChatComponents(IChatComponent, Consumer)}
468+
* @param chatComponent root chat component
469+
* @param action predicate that transforms a component and reports a successful transformation
470+
* @return Whether any transformation occurred
471+
*/
472+
public static boolean transformAnyChatComponent(IChatComponent chatComponent, Predicate<IChatComponent> action) {
473+
if(action.test(chatComponent))
474+
return true;
475+
for (IChatComponent sibling : chatComponent.getSiblings()) {
476+
if(transformAnyChatComponent(sibling, action))
477+
return true;
478+
}
479+
return false;
480+
}
462481
}

0 commit comments

Comments
 (0)