mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-04 15:41:38 +00:00
添加条件lore
This commit is contained in:
@@ -8,10 +8,10 @@ import net.momirealms.craftengine.core.util.TriFunction;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
// todo 可以考虑未来添加条件系统
|
public record LoreModification(Operation operation, boolean split, FormattedLine[] content, Predicate<ItemBuildContext> predicate) {
|
||||||
public record LoreModification(Operation operation, boolean split, FormattedLine[] content) {
|
|
||||||
|
|
||||||
public Stream<Component> apply(Stream<Component> lore, ItemBuildContext context) {
|
public Stream<Component> apply(Stream<Component> lore, ItemBuildContext context) {
|
||||||
return this.operation.function.apply(lore, context, this);
|
return this.operation.function.apply(lore, context, this);
|
||||||
@@ -27,8 +27,18 @@ public record LoreModification(Operation operation, boolean split, FormattedLine
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Operation {
|
public enum Operation {
|
||||||
APPEND((s, c, modification) -> Stream.concat(s, modification.parseAsStream(c))),
|
APPEND((s, c, modification) -> {
|
||||||
PREPEND((s, c, modification) -> Stream.concat(modification.parseAsStream(c), s));
|
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;
|
private final TriFunction<Stream<Component>, ItemBuildContext, LoreModification, Stream<Component>> function;
|
||||||
|
|
||||||
|
|||||||
@@ -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.ItemProcessor;
|
||||||
import net.momirealms.craftengine.core.item.processor.SimpleNetworkItemProcessor;
|
import net.momirealms.craftengine.core.item.processor.SimpleNetworkItemProcessor;
|
||||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
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.plugin.text.minimessage.FormattedLine;
|
||||||
import net.momirealms.craftengine.core.util.Key;
|
import net.momirealms.craftengine.core.util.Key;
|
||||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
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,
|
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))
|
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);
|
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);
|
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);
|
lastPriority = Optional.ofNullable(complexLore.get("priority")).map(it -> ResourceConfigUtils.getAsInt(it, "priority")).orElse(lastPriority);
|
||||||
boolean split = ResourceConfigUtils.getAsBoolean(complexLore.get("split-lines"), "split-lines");
|
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,
|
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))
|
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);
|
modifications.sort(LoreModificationHolder::compareTo);
|
||||||
|
|||||||
Reference in New Issue
Block a user