Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprAnyOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
import ch.njol.skript.doc.Name;
import ch.njol.skript.expressions.base.WrapperExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Any Of")
@Description({
"Returns an 'or list' composed of the given objects. For example, `any of (1, 2, and 3)` is equivalent to `1, 2, or 3`",
"Useful when doing comparisons with variable lists."
})
@Example("if any of {_numbers::*} are 1:")
@Example("if any of {teamA::*} are within location(0, 0, 0) and location(10, 10, 10):")
public class ExprAnyOf extends WrapperExpression<Object> {

static {
Skript.registerExpression(ExprAnyOf.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "(any [one]|one) of [the] %objects%");
}

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expression<?> expr = LiteralUtils.defendExpression(expressions[0]);
setExpr(expr);
return LiteralUtils.canInitSafely(expr);
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
return new Class[0];
}

@Override
public boolean getAnd() {
return false;
}

@Override
public boolean isSingle() {
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "any of " + getExpr().toString(event, debug);
}

}
10 changes: 8 additions & 2 deletions src/main/java/ch/njol/skript/lang/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -200,8 +201,13 @@ public void invertAnd() {

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) throws UnsupportedOperationException {
for (Expression<?> expr : expressions) {
expr.change(event, delta, mode);
if (and) {
for (Expression<?> expr : expressions) {
expr.change(event, delta, mode);
}
} else {
int i = ThreadLocalRandom.current().nextInt(expressions.length);
expressions[i].change(event, delta, mode);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test "changing or list changes all":
set {_a::1} or {_a::2} to 1
assert size of {_a::*} is 1
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprAnyOf.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "any of":
set {_a} to any of 1, 2, and 3
assert {_a} is 1, 2, or 3 with error "any of 1, 2, and 3 was not 1, 2, or 3"
set {_a} to any of 1, 2, or 3
assert {_a} is 1, 2, or 3 with error "any of 1, 2, or 3 was not 1, 2, or 3"
assert 1 is any of 1, 2, and 3 with error "1 was not any of 1, 2, and 3"
set {_a::*} to 1, 2, and 3
assert 1 is any of {_a::*} with error "1 was not any of 1, 2, and 3 as a variable"

assert 1 is any of the integers from 1 to 3 with error "1 was not any of integers from 1 to 3"
assert any of the integers from 1 to 3 are evenly divisible by 2 with error "none of integers from 1 to 3 were divisible by 2"