mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-03 22:26:16 +00:00
添加玩家选择器
This commit is contained in:
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.plugin.context.Condition;
|
||||
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
|
||||
@@ -11,22 +12,18 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AllOfCondition<CTX extends Context> implements Condition<CTX> {
|
||||
protected final List<? extends Condition<CTX>> conditions;
|
||||
protected final Predicate<CTX> condition;
|
||||
|
||||
public AllOfCondition(List<? extends Condition<CTX>> conditions) {
|
||||
this.conditions = conditions;
|
||||
this.condition = MCUtils.allOf(conditions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(CTX ctx) {
|
||||
for (Condition<CTX> condition : conditions) {
|
||||
if (!condition.test(ctx)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return this.condition.test(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.plugin.context.Condition;
|
||||
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
|
||||
@@ -11,22 +12,18 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AnyOfCondition<CTX extends Context> implements Condition<CTX> {
|
||||
protected final List<? extends Condition<CTX>> conditions;
|
||||
protected final Predicate<CTX> condition;
|
||||
|
||||
public AnyOfCondition(List<? extends Condition<CTX>> conditions) {
|
||||
this.conditions = conditions;
|
||||
this.condition = MCUtils.anyOf(conditions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(CTX ctx) {
|
||||
for (Condition<CTX> condition : conditions) {
|
||||
if (condition.test(ctx)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return this.condition.test(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -35,6 +35,10 @@ public abstract class AbstractConditionalFunction<CTX extends Context> implement
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public java.util.function.Function<Map<String, Object>, Condition<CTX>> conditionFactory() {
|
||||
return factory;
|
||||
}
|
||||
|
||||
protected List<Condition<CTX>> getPredicates(Map<String, Object> arguments) {
|
||||
Object predicates = arguments.get("conditions");
|
||||
if (predicates == null) return List.of();
|
||||
|
||||
@@ -72,7 +72,7 @@ public class CommandFunction<CTX extends Context> extends AbstractConditionalFun
|
||||
Object command = ResourceConfigUtils.requireNonNullOrThrow(ResourceConfigUtils.get(arguments, "command", "commands"), "warning.config.function.command.missing_command");
|
||||
List<TextProvider> commands = MiscUtils.getAsStringList(command).stream().map(TextProviders::fromString).toList();
|
||||
boolean asPlayer = (boolean) arguments.getOrDefault("as-player", false);
|
||||
return new CommandFunction<>(getPredicates(arguments), commands, asPlayer, PlayerSelectors.fromObject(arguments.get("target")));
|
||||
return new CommandFunction<>(getPredicates(arguments), commands, asPlayer, PlayerSelectors.fromObject(arguments.get("target"), conditionFactory()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,42 +8,38 @@ import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AllPlayerSelector<CTX extends Context> implements PlayerSelector<CTX> {
|
||||
private final List<Condition<CTX>> predicates;
|
||||
private final Predicate<CTX> predicate;
|
||||
|
||||
public AllPlayerSelector(List<Condition<CTX>> predicates) {
|
||||
this.predicates = predicates;
|
||||
this.predicate = MCUtils.allOf(predicates);
|
||||
}
|
||||
|
||||
public AllPlayerSelector() {
|
||||
this.predicates = List.of();
|
||||
}
|
||||
|
||||
public List<Condition<CTX>> predicates() {
|
||||
return predicates;
|
||||
this.predicate = null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Player> get(CTX context) {
|
||||
if (this.predicates.isEmpty()) {
|
||||
if (this.predicate == null) {
|
||||
return Arrays.asList(CraftEngine.instance().networkManager().onlineUsers());
|
||||
} else {
|
||||
List<Player> players = new ArrayList<>();
|
||||
outer: for (Player player : CraftEngine.instance().networkManager().onlineUsers()) {
|
||||
for (Player player : CraftEngine.instance().networkManager().onlineUsers()) {
|
||||
PlayerOptionalContext newContext = PlayerOptionalContext.of(player, ContextHolder.builder()
|
||||
.withOptionalParameter(CommonParameters.WORLD, context.getOptionalParameter(CommonParameters.WORLD).orElse(null))
|
||||
.withOptionalParameter(CommonParameters.LOCATION, context.getOptionalParameter(CommonParameters.LOCATION).orElse(null))
|
||||
);
|
||||
for (Condition<CTX> predicate : this.predicates) {
|
||||
if (!predicate.test((CTX) newContext)) {
|
||||
continue outer;
|
||||
}
|
||||
if (!this.predicate.test((CTX) newContext)) {
|
||||
continue;
|
||||
}
|
||||
players.add(player);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
package net.momirealms.craftengine.core.plugin.context.selector;
|
||||
|
||||
import net.momirealms.craftengine.core.plugin.context.Condition;
|
||||
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PlayerSelectors {
|
||||
public static final Key ALL = Key.of("craftengine:all");
|
||||
public static final Key SELF = Key.of("craftengine:self");
|
||||
|
||||
public static <CTX extends Context> PlayerSelector<CTX> fromObject(Object object) {
|
||||
public static <CTX extends Context> PlayerSelector<CTX> fromObject(Object object, Function<Map<String, Object>, Condition<CTX>> conditionFactory) {
|
||||
if (object == null) return null;
|
||||
if (object instanceof String string) {
|
||||
if (string.equals("self") || string.equals("@self") || string.equals("@s")) {
|
||||
@@ -18,6 +24,27 @@ public class PlayerSelectors {
|
||||
return new AllPlayerSelector<>();
|
||||
}
|
||||
} else if (object instanceof Map<?,?> map) {
|
||||
Map<String, Object> selectorMap = MiscUtils.castToMap(map, false);
|
||||
Object typeObj = selectorMap.get("type");
|
||||
Object conditionObj = ResourceConfigUtils.get(selectorMap, "conditions");
|
||||
if (!(typeObj instanceof String typeString)) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
if (typeString.equals("all") || typeString.equals("@all") || typeString.equals("@a")) {
|
||||
List<Condition<CTX>> conditions = new ArrayList<>();
|
||||
if (conditionObj instanceof List<?> list) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> conditionList = (List<Map<String, Object>>) list;
|
||||
for (Map<String, Object> condition : conditionList) {
|
||||
conditions.add(conditionFactory.apply(condition));
|
||||
}
|
||||
} else if (conditionObj instanceof Map<?,?>) {
|
||||
conditions.add(conditionFactory.apply(MiscUtils.castToMap(conditionObj, false)));
|
||||
} else {
|
||||
return new AllPlayerSelector<>();
|
||||
}
|
||||
return new AllPlayerSelector<>(conditions);
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@@ -116,7 +116,6 @@ public class MCUtils {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -148,6 +147,70 @@ public class MCUtils {
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf() {
|
||||
return o -> false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T> a) {
|
||||
return (Predicate<T>) a;
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T> a, Predicate<? super T> b) {
|
||||
return o -> a.test(o) || b.test(o);
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T> a, Predicate<? super T> b, Predicate<? super T> c) {
|
||||
return o -> a.test(o) || b.test(o) || c.test(o);
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T> a, Predicate<? super T> b, Predicate<? super T> c, Predicate<? super T> d) {
|
||||
return o -> a.test(o) || b.test(o) || c.test(o) || d.test(o);
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T> a, Predicate<? super T> b, Predicate<? super T> c, Predicate<? super T> d, Predicate<? super T> e) {
|
||||
return o -> a.test(o) || b.test(o) || c.test(o) || d.test(o) || e.test(o);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> Predicate<T> anyOf(Predicate<? super T>... predicates) {
|
||||
return o -> {
|
||||
for (Predicate<? super T> predicate : predicates) {
|
||||
if (predicate.test(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> anyOf(List<? extends Predicate<? super T>> predicates) {
|
||||
return switch (predicates.size()) {
|
||||
case 0 -> anyOf();
|
||||
case 1 -> anyOf((Predicate<? super T>) predicates.get(0));
|
||||
case 2 -> anyOf((Predicate<? super T>) predicates.get(0), (Predicate<? super T>) predicates.get(1));
|
||||
case 3 -> anyOf((Predicate<? super T>) predicates.get(0), (Predicate<? super T>) predicates.get(1), (Predicate<? super T>) predicates.get(2));
|
||||
case 4 -> anyOf(
|
||||
(Predicate<? super T>) predicates.get(0),
|
||||
(Predicate<? super T>) predicates.get(1),
|
||||
(Predicate<? super T>) predicates.get(2),
|
||||
(Predicate<? super T>) predicates.get(3)
|
||||
);
|
||||
case 5 -> anyOf(
|
||||
(Predicate<? super T>) predicates.get(0),
|
||||
(Predicate<? super T>) predicates.get(1),
|
||||
(Predicate<? super T>) predicates.get(2),
|
||||
(Predicate<? super T>) predicates.get(3),
|
||||
(Predicate<? super T>) predicates.get(4)
|
||||
);
|
||||
default -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
Predicate<? super T>[] predicates2 = predicates.toArray(Predicate[]::new);
|
||||
yield anyOf(predicates2);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> T findPreviousInIterable(Iterable<T> iterable, @Nullable T object) {
|
||||
Iterator<T> iterator = iterable.iterator();
|
||||
T previous = null;
|
||||
|
||||
Reference in New Issue
Block a user