Skip to content

Commit e0a89b3

Browse files
Merge branch 'dev/feature' into dev/DebugFix
2 parents 6f99896 + 4569e9d commit e0a89b3

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

src/main/java/org/skriptlang/skript/bukkit/tags/elements/ExprTag.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import ch.njol.skript.lang.ExpressionType;
1212
import ch.njol.skript.lang.Literal;
1313
import ch.njol.skript.lang.SkriptParser.ParseResult;
14+
import ch.njol.skript.lang.util.ContextlessEvent;
1415
import ch.njol.skript.lang.util.SimpleExpression;
1516
import ch.njol.skript.lang.util.SimpleLiteral;
1617
import ch.njol.util.Kleenean;
@@ -130,8 +131,8 @@ public String toString(@Nullable Event event, boolean debug) {
130131
@Override
131132
public Expression<? extends Tag> simplify() {
132133
if (names instanceof Literal<String>)
133-
return new SimpleLiteral<>(getArray(null), Tag.class, true);
134-
return this;
134+
return new SimpleLiteral<>(getArray(ContextlessEvent.get()), Tag.class, true);
135+
return super.simplify();
135136
}
136137

137138
}

src/main/java/org/skriptlang/skript/bukkit/tags/elements/ExprTagContents.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
import ch.njol.skript.doc.Keywords;
99
import ch.njol.skript.doc.Name;
1010
import ch.njol.skript.doc.Since;
11+
import ch.njol.skript.entity.EntityData;
1112
import ch.njol.skript.lang.Expression;
1213
import ch.njol.skript.lang.ExpressionType;
14+
import ch.njol.skript.lang.Literal;
1315
import ch.njol.skript.lang.SkriptParser.ParseResult;
16+
import ch.njol.skript.lang.util.ContextlessEvent;
1417
import ch.njol.skript.lang.util.SimpleExpression;
15-
import ch.njol.skript.util.Utils;
18+
import ch.njol.skript.lang.util.SimpleLiteral;
19+
import ch.njol.skript.registrations.Classes;
1620
import ch.njol.util.Kleenean;
1721
import org.bukkit.Material;
1822
import org.bukkit.Tag;
@@ -22,7 +26,9 @@
2226
import org.jetbrains.annotations.Nullable;
2327
import org.skriptlang.skript.bukkit.tags.TagType;
2428

29+
import java.lang.reflect.Array;
2530
import java.util.Objects;
31+
import java.util.stream.Stream;
2632

2733
@Name("Tags Contents")
2834
@Description({
@@ -45,27 +51,55 @@ public class ExprTagContents extends SimpleExpression<Object> {
4551
}
4652

4753
private Expression<Tag<?>> tag;
48-
private TagType<?> @Nullable [] tagTypes;
54+
55+
private Class<?> returnType;
56+
private Class<?>[] possibleReturnTypes;
4957

5058
@Override
5159
public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
5260
//noinspection unchecked
5361
tag = (Expression<Tag<?>>) expressions[0];
62+
63+
TagType<?>[] tagTypes = null;
5464
if (expressions[0] instanceof ExprTag exprTag) {
5565
tagTypes = exprTag.types;
5666
} else if (expressions[0] instanceof ExprTagsOf exprTagsOf) {
5767
tagTypes = exprTagsOf.types;
5868
} else if (expressions[0] instanceof ExprTagsOfType exprTagsOfType) {
5969
tagTypes = exprTagsOfType.types;
6070
}
71+
if (tagTypes != null) { // try to determine the return type
72+
possibleReturnTypes = new Class<?>[tagTypes.length];
73+
for (int i = 0; i < tagTypes.length; i++) {
74+
Class<?> type = tagTypes[i].type();
75+
// map types
76+
if (type == Material.class) {
77+
type = ItemType.class;
78+
} else if (type == EntityType.class) {
79+
type = EntityData.class;
80+
}
81+
possibleReturnTypes[i] = type;
82+
}
83+
returnType = Classes.getSuperClassInfo(possibleReturnTypes).getC();
84+
} else {
85+
returnType = Object.class;
86+
possibleReturnTypes = new Class<?>[]{returnType};
87+
}
88+
6189
return true;
6290
}
6391

6492
@Override
93+
@SuppressWarnings({"unchecked", "rawtypes", "RedundantCast"}) // cast to avoid type issues
6594
protected Object @Nullable [] get(Event event) {
95+
return ((Stream) stream(event)).toArray(length -> Array.newInstance(getReturnType(), length));
96+
}
97+
98+
@Override
99+
public Stream<?> stream(Event event) {
66100
Tag<?> tag = this.tag.getSingle(event);
67101
if (tag == null)
68-
return null;
102+
return Stream.empty();
69103
return tag.getValues().stream()
70104
.map(value -> {
71105
if (value instanceof Material material) {
@@ -75,8 +109,7 @@ public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, K
75109
}
76110
return null;
77111
})
78-
.filter(Objects::nonNull)
79-
.toArray();
112+
.filter(Objects::nonNull);
80113
}
81114

82115
@Override
@@ -86,19 +119,26 @@ public boolean isSingle() {
86119

87120
@Override
88121
public Class<?> getReturnType() {
89-
if (tagTypes != null) {
90-
Class<?>[] possibleTypes = new Class<?>[tagTypes.length];
91-
for (int i = 0; i < tagTypes.length; i++) {
92-
possibleTypes[i] = tagTypes[i].type();
93-
}
94-
return Utils.getSuperType(possibleTypes);
95-
}
96-
return Object.class;
122+
return returnType;
123+
}
124+
125+
@Override
126+
public Class<?>[] possibleReturnTypes() {
127+
return possibleReturnTypes;
97128
}
98129

99130
@Override
100131
public String toString(@Nullable Event event, boolean debug) {
101132
return "the tag contents of " + tag.toString(event, debug);
102133
}
103134

135+
@Override
136+
public Expression<?> simplify() {
137+
if (tag instanceof Literal<Tag<?>>) {
138+
Object[] values = getArray(ContextlessEvent.get());
139+
return new SimpleLiteral(values, values.getClass().getComponentType(), true);
140+
}
141+
return super.simplify();
142+
}
143+
104144
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ test "tags contents":
55
assert tag contents of item tag "slabs" contains oak slab with "oak slab is not a slab"
66

77
assert tag contents of entity tag "minecraft:skeletons" contains a skeleton with "skeleton is not a skeleton"
8+
9+
parse:
10+
loop tag contents of minecraft item tag "logs":
11+
add loop-item to {_list::*}
12+
assert last parse logs is not set with "failed to parse tag looping (%last parse logs%)"

0 commit comments

Comments
 (0)