9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 09:59:20 +00:00

fix: style loss

This commit is contained in:
ᑕᖇEEᑭYᑕᖇEEᑭEᖇ
2025-07-20 00:07:32 +08:00
parent f68432b761
commit 70925ee2c9

View File

@@ -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
* <ul>
* <li>Can be used individually without style loss</li>
* <li>Can be concatenated to form the original component, given that children are dropped</li>
* </ul>
* Any {@link net.kyori.adventure.text.ComponentIteratorFlag}s are ignored.
*/
private static final ComponentIteratorType SLICER = (component, deque, flags) -> {
final List<Component> 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<Component> splitLines(Component component) {
List<Component> result = new ArrayList<>(1);
Component line = Component.empty();
for (Iterator<Component> it = component.replaceText(REPLACE_LF).iterator(ComponentIteratorType.DEPTH_FIRST); it.hasNext(); ) {
for (Iterator<Component> 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;
}