9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 19:09:08 +00:00

添加条件lore

This commit is contained in:
XiaoMoMi
2025-12-26 22:33:38 +08:00
parent 6b76a0d456
commit 201df49643
2 changed files with 19 additions and 6 deletions

View File

@@ -8,10 +8,10 @@ import net.momirealms.craftengine.core.util.TriFunction;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
// todo 可以考虑未来添加条件系统
public record LoreModification(Operation operation, boolean split, FormattedLine[] content) {
public record LoreModification(Operation operation, boolean split, FormattedLine[] content, Predicate<ItemBuildContext> predicate) {
public Stream<Component> apply(Stream<Component> lore, ItemBuildContext context) {
return this.operation.function.apply(lore, context, this);
@@ -27,8 +27,18 @@ public record LoreModification(Operation operation, boolean split, FormattedLine
}
public enum Operation {
APPEND((s, c, modification) -> Stream.concat(s, modification.parseAsStream(c))),
PREPEND((s, c, modification) -> Stream.concat(modification.parseAsStream(c), s));
APPEND((s, c, modification) -> {
if (modification.predicate.test(c)) {
return Stream.concat(s, modification.parseAsStream(c));
}
return s;
}),
PREPEND((s, c, modification) -> {
if (modification.predicate.test(c)) {
return Stream.concat(modification.parseAsStream(c), s);
}
return s;
});
private final TriFunction<Stream<Component>, ItemBuildContext, LoreModification, Stream<Component>> function;

View File

@@ -8,6 +8,8 @@ import net.momirealms.craftengine.core.item.ItemProcessorFactory;
import net.momirealms.craftengine.core.item.processor.ItemProcessor;
import net.momirealms.craftengine.core.item.processor.SimpleNetworkItemProcessor;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.event.EventConditions;
import net.momirealms.craftengine.core.plugin.text.minimessage.FormattedLine;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -62,7 +64,7 @@ public sealed interface LoreProcessor<I> extends SimpleNetworkItemProcessor<I>
}
return new SingleLoreProcessor<>(new LoreModification(LoreModification.Operation.APPEND, false,
Arrays.stream(rawLore).map(line -> Config.addNonItalicTag() && !line.startsWith("<!i>") ? FormattedLine.create("<!i>" + line) : FormattedLine.create(line))
.toArray(FormattedLine[]::new)));
.toArray(FormattedLine[]::new), c -> true));
}
List<LoreModificationHolder> modifications = new ArrayList<>(rawLoreData.size() + 1);
@@ -73,9 +75,10 @@ public sealed interface LoreProcessor<I> extends SimpleNetworkItemProcessor<I>
LoreModification.Operation operation = ResourceConfigUtils.getAsEnum(Optional.ofNullable(complexLore.get("operation")).map(String::valueOf).orElse(null), LoreModification.Operation.class, LoreModification.Operation.APPEND);
lastPriority = Optional.ofNullable(complexLore.get("priority")).map(it -> ResourceConfigUtils.getAsInt(it, "priority")).orElse(lastPriority);
boolean split = ResourceConfigUtils.getAsBoolean(complexLore.get("split-lines"), "split-lines");
List<Condition<ItemBuildContext>> conditions = ResourceConfigUtils.parseConfigAsList(complexLore.get("conditions"), EventConditions::fromMap);
modifications.add(new LoreModificationHolder(new LoreModification(operation, split,
Arrays.stream(content).map(line -> Config.addNonItalicTag() && !line.startsWith("<!i>") ? FormattedLine.create("<!i>" + line) : FormattedLine.create(line))
.toArray(FormattedLine[]::new)), lastPriority));
.toArray(FormattedLine[]::new), MiscUtils.allOf(conditions)), lastPriority));
}
}
modifications.sort(LoreModificationHolder::compareTo);