8
8
import ch .njol .skript .doc .Keywords ;
9
9
import ch .njol .skript .doc .Name ;
10
10
import ch .njol .skript .doc .Since ;
11
+ import ch .njol .skript .entity .EntityData ;
11
12
import ch .njol .skript .lang .Expression ;
12
13
import ch .njol .skript .lang .ExpressionType ;
14
+ import ch .njol .skript .lang .Literal ;
13
15
import ch .njol .skript .lang .SkriptParser .ParseResult ;
16
+ import ch .njol .skript .lang .util .ContextlessEvent ;
14
17
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 ;
16
20
import ch .njol .util .Kleenean ;
17
21
import org .bukkit .Material ;
18
22
import org .bukkit .Tag ;
22
26
import org .jetbrains .annotations .Nullable ;
23
27
import org .skriptlang .skript .bukkit .tags .TagType ;
24
28
29
+ import java .lang .reflect .Array ;
25
30
import java .util .Objects ;
31
+ import java .util .stream .Stream ;
26
32
27
33
@ Name ("Tags Contents" )
28
34
@ Description ({
@@ -45,27 +51,55 @@ public class ExprTagContents extends SimpleExpression<Object> {
45
51
}
46
52
47
53
private Expression <Tag <?>> tag ;
48
- private TagType <?> @ Nullable [] tagTypes ;
54
+
55
+ private Class <?> returnType ;
56
+ private Class <?>[] possibleReturnTypes ;
49
57
50
58
@ Override
51
59
public boolean init (Expression <?> @ NotNull [] expressions , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
52
60
//noinspection unchecked
53
61
tag = (Expression <Tag <?>>) expressions [0 ];
62
+
63
+ TagType <?>[] tagTypes = null ;
54
64
if (expressions [0 ] instanceof ExprTag exprTag ) {
55
65
tagTypes = exprTag .types ;
56
66
} else if (expressions [0 ] instanceof ExprTagsOf exprTagsOf ) {
57
67
tagTypes = exprTagsOf .types ;
58
68
} else if (expressions [0 ] instanceof ExprTagsOfType exprTagsOfType ) {
59
69
tagTypes = exprTagsOfType .types ;
60
70
}
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
+
61
89
return true ;
62
90
}
63
91
64
92
@ Override
93
+ @ SuppressWarnings ({"unchecked" , "rawtypes" , "RedundantCast" }) // cast to avoid type issues
65
94
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 ) {
66
100
Tag <?> tag = this .tag .getSingle (event );
67
101
if (tag == null )
68
- return null ;
102
+ return Stream . empty () ;
69
103
return tag .getValues ().stream ()
70
104
.map (value -> {
71
105
if (value instanceof Material material ) {
@@ -75,8 +109,7 @@ public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, K
75
109
}
76
110
return null ;
77
111
})
78
- .filter (Objects ::nonNull )
79
- .toArray ();
112
+ .filter (Objects ::nonNull );
80
113
}
81
114
82
115
@ Override
@@ -86,19 +119,26 @@ public boolean isSingle() {
86
119
87
120
@ Override
88
121
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 ;
97
128
}
98
129
99
130
@ Override
100
131
public String toString (@ Nullable Event event , boolean debug ) {
101
132
return "the tag contents of " + tag .toString (event , debug );
102
133
}
103
134
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
+
104
144
}
0 commit comments