From 70925ee2c98af2acab9acf29ccc51f003bafb8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=91=95=E1=96=87EE=E1=91=ADY=E1=91=95=E1=96=87EE?= =?UTF-8?q?=E1=91=ADE=E1=96=87?= <3404705272@qq.com> Date: Sun, 20 Jul 2025 00:07:32 +0800 Subject: [PATCH] fix: style loss --- .../core/util/AdventureHelper.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) 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; }