mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-25 09:59:20 +00:00
feat: split lore components
This commit is contained in:
@@ -90,11 +90,7 @@ public class ComponentItemFactory1_21_5 extends ComponentItemFactory1_21_4 {
|
||||
if (lore == null || lore.isEmpty()) {
|
||||
item.resetComponent(ComponentTypes.LORE);
|
||||
} else {
|
||||
List<Tag> loreTags = new ArrayList<>();
|
||||
for (Component component : lore) {
|
||||
loreTags.add(AdventureHelper.componentToTag(component));
|
||||
}
|
||||
item.setSparrowNBTComponent(ComponentTypes.LORE, new ListTag(loreTags));
|
||||
item.setSparrowNBTComponent(ComponentTypes.LORE, new ListTag(lore.stream().map(AdventureHelper::split).flatMap(List::stream).map(AdventureHelper::componentToTag).toList()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import net.momirealms.sparrow.nbt.Tag;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class ItemFactory<W extends ItemWrapper<I>, I> {
|
||||
protected final CraftEngine plugin;
|
||||
@@ -112,7 +111,7 @@ public abstract class ItemFactory<W extends ItemWrapper<I>, I> {
|
||||
|
||||
protected void loreComponent(W item, List<Component> component) {
|
||||
if (component != null && !component.isEmpty()) {
|
||||
loreJson(item, component.stream().map(AdventureHelper::componentToJson).collect(Collectors.toList()));
|
||||
loreJson(item, component.stream().map(AdventureHelper::split).flatMap(List::stream).map(AdventureHelper::componentToJson).toList());
|
||||
} else {
|
||||
loreJson(item, null);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package net.momirealms.craftengine.core.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.ComponentIteratorType;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
@@ -29,6 +35,7 @@ public class AdventureHelper {
|
||||
private final MiniMessage miniMessageCustom;
|
||||
private final GsonComponentSerializer gsonComponentSerializer;
|
||||
private final NBTComponentSerializer nbtComponentSerializer;
|
||||
private static final TextReplacementConfig REPLACE_LF = TextReplacementConfig.builder().matchLiteral("\n").replacement(Component.newline()).build();
|
||||
|
||||
private AdventureHelper() {
|
||||
this.miniMessage = MiniMessage.builder().build();
|
||||
@@ -209,6 +216,24 @@ public class AdventureHelper {
|
||||
return getNBT().deserialize(tag);
|
||||
}
|
||||
|
||||
public static List<Component> split(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(); ) {
|
||||
Component child = it.next().children(Collections.emptyList());
|
||||
if (Component.EQUALS.test(child, Component.newline())) {
|
||||
result.add(line);
|
||||
line = Component.empty();
|
||||
} else {
|
||||
line = line.append(child);
|
||||
}
|
||||
}
|
||||
if (Component.IS_NOT_EMPTY.test(line)) {
|
||||
result.add(line);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a character is a legacy color code.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user