Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/obro1961/chatpatches/util/ChatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public static Component modifyMessage(@NotNull Component m) {
} else { // reconstructs the message if it matches the vanilla format '<%s> %s' but isn't translatable
MutableComponent realContent = Component.empty();
// collect all message parts into one list, including the root TextContent
List<Component> parts = Lists.asList( m.plainCopy().setStyle(style), m.getSiblings().toArray(new Component[0]) );
List<Component> parts = TextUtil.linearize(m);

// find the first index of a '>' in the '<%s> %s'-formatted message
Component firstPart = parts.stream()
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/obro1961/chatpatches/util/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -342,4 +343,34 @@ else if(style.equals(Style.EMPTY) && !last.equals(Style.EMPTY))

return joiner.toString();
}

/**
* Walk the component-sibling tree in depth first order (aka render order).
* Similar to {@code Component.visit()} but without using {@code Component.getContents().visit()},
* i.e. it will not decompose translatables into parts.
*/
public static void walkTree(Component component, Style currentStyle, BiConsumer<Component, Style> consumer) {
currentStyle = component.getStyle().applyTo(currentStyle);

consumer.accept(component, currentStyle);

for (Component sibling : component.getSiblings()) {
walkTree(sibling, currentStyle, consumer);
}
}

/**
* Turns the component-sibling tree into a flat list.
* Similar to {@code Component.toFlatList()} but using {@link #walkTree} instead of {@code Component.visit()}.
*/
public static List<Component> linearize(Component component) {
List<Component> components = new ArrayList<>();

walkTree(component, Style.EMPTY, (c, cStyle) -> {
var copy = c.plainCopy().setStyle(cStyle);
components.add(copy);
});

return components;
}
}
Loading