Skip to content

Commit 0d3183e

Browse files
committed
ExpressionList specificity
1 parent 7fc9262 commit 0d3183e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/main/java/ch/njol/skript/expressions/ExprExcept.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package ch.njol.skript.expressions;
22

33
import ch.njol.skript.Skript;
4-
import ch.njol.skript.doc.*;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Example;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
58
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.ExpressionList;
610
import ch.njol.skript.lang.ExpressionType;
711
import ch.njol.skript.lang.SkriptParser.ParseResult;
812
import ch.njol.skript.lang.SyntaxStringBuilder;
@@ -14,6 +18,8 @@
1418
import org.skriptlang.skript.lang.comparator.Comparators;
1519
import org.skriptlang.skript.lang.comparator.Relation;
1620

21+
import java.util.Iterator;
22+
1723
@Name("Except")
1824
@Description("Filter a list by providing objects to be excluded.")
1925
@Example("""
@@ -29,18 +35,22 @@ public class ExprExcept extends SimpleExpression<Object> {
2935

3036
static {
3137
Skript.registerExpression(ExprExcept.class, Object.class, ExpressionType.COMBINED,
32-
"%~objects% (except|excluding|not including) %objects%");
38+
"%objects% (except|excluding|not including) %objects%");
3339
}
3440

3541
private Expression<?> source;
3642
private Expression<?> exclude;
43+
private boolean isOrList = false;
3744

3845
@Override
3946
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
4047
source = LiteralUtils.defendExpression(exprs[0]);
4148
if (source.isSingle()) {
42-
Skript.error("Must provide a list containing more than one object to exclude objects from.");
43-
return false;
49+
if (!(source instanceof ExpressionList<?>)) {
50+
Skript.error("Must provide a list containing more than one object to exclude objects from.");
51+
return false;
52+
}
53+
isOrList = true;
4454
}
4555
exclude = LiteralUtils.defendExpression(exprs[1]);
4656
return LiteralUtils.canInitSafely(source, exclude);
@@ -50,7 +60,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5060
protected Object @Nullable [] get(Event event) {
5161
Object[] exclude = this.exclude.getArray(event);
5262
if (exclude == null || exclude.length == 0)
53-
return source.getArray(event);
63+
return isOrList ? source.getArray(event) : source.getAll(event);
5464

5565
return source.streamAll(event)
5666
.filter(sourceObject -> {
@@ -62,9 +72,24 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
6272
.toArray();
6373
}
6474

75+
@Override
76+
public @Nullable Iterator<?> iterator(Event event) {
77+
Object[] exclude = this.exclude.getArray(event);
78+
if (exclude == null || exclude.length == 0)
79+
return source.iterator(event);
80+
81+
return source.streamAll(event)
82+
.filter(sourceObject -> {
83+
for (Object excludeObject : exclude)
84+
if (sourceObject.equals(excludeObject) || Comparators.compare(sourceObject, excludeObject) == Relation.EQUAL)
85+
return false;
86+
return true;
87+
}).iterator();
88+
}
89+
6590
@Override
6691
public boolean isSingle() {
67-
return false;
92+
return isOrList;
6893
}
6994

7095
@Override

src/test/skript/tests/syntaxes/expressions/ExprExcept.sk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ test "except entities":
3535
assert size of all entities not including {_exclude::*} is 12 with "Failed to exclude list (zombie, skeleton, villager)"
3636

3737
clear all entities
38+
39+
test "except 'or' objects":
40+
assert (1 or 2) except 2 is 1 with "Failed to exclude '2' from an 'or' list"
41+
42+
set {_item} to (a diamond or an iron ingot or an emerald) excluding a diamond
43+
assert {_item} is (an iron ingot or an emerald) with "Failed to exclude 'diamond' from an 'or' item list"

0 commit comments

Comments
 (0)