diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/AdventureHelper.java b/core/src/main/java/net/momirealms/craftengine/core/util/AdventureHelper.java index f58769a92..f3af38fc8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/util/AdventureHelper.java +++ b/core/src/main/java/net/momirealms/craftengine/core/util/AdventureHelper.java @@ -36,6 +36,20 @@ public class AdventureHelper { private final GsonComponentSerializer gsonComponentSerializer; private final NBTComponentSerializer nbtComponentSerializer; private static final TextReplacementConfig REPLACE_LF = TextReplacementConfig.builder().matchLiteral("\n").replacement(Component.newline()).build(); + /** + * This iterator slices a component into individual parts that + * + * Any {@link net.kyori.adventure.text.ComponentIteratorFlag}s are ignored. + */ + private static final ComponentIteratorType SLICER = (component, deque, flags) -> { + final List children = component.children(); + for (int i = children.size() - 1; i >= 0; i--) { + deque.addFirst(children.get(i).applyFallbackStyle(component.style())); + } + }; private AdventureHelper() { this.miniMessage = MiniMessage.builder().build(); @@ -219,17 +233,17 @@ public class AdventureHelper { public static List splitLines(Component component) { List result = new ArrayList<>(1); Component line = Component.empty(); - for (Iterator it = component.replaceText(REPLACE_LF).iterator(ComponentIteratorType.DEPTH_FIRST); it.hasNext(); ) { + for (Iterator it = component.replaceText(REPLACE_LF).iterator(SLICER); it.hasNext(); ) { Component child = it.next().children(Collections.emptyList()); - if (Component.EQUALS.test(child, Component.newline())) { - result.add(line); + if (child instanceof TextComponent text && text.content().equals(Component.newline().content())) { + result.add(line.compact()); line = Component.empty(); } else { line = line.append(child); } } if (Component.IS_NOT_EMPTY.test(line)) { - result.add(line); + result.add(line.compact()); } return result; }