2020import org .bukkit .Location ;
2121import org .bukkit .World ;
2222import org .bukkit .entity .Entity ;
23- import org .bukkit .entity .Player ;
2423import org .bukkit .event .Event ;
2524import org .bukkit .util .BoundingBox ;
2625import org .jetbrains .annotations .Nullable ;
2726import org .jetbrains .annotations .UnknownNullability ;
2827
2928import java .lang .reflect .Array ;
30- import java .util .ArrayList ;
31- import java .util .Arrays ;
32- import java .util .Collection ;
33- import java .util .Iterator ;
34- import java .util .List ;
29+ import java .util .*;
3530
3631@ Name ("Entities" )
3732@ Description ("All entities in all worlds, in a specific world, in a chunk, in a radius around a certain location or within two locations. " +
@@ -47,8 +42,8 @@ public class ExprEntities extends SimpleExpression<Entity> {
4742
4843 static {
4944 Skript .registerExpression (ExprEntities .class , Entity .class , ExpressionType .PATTERN_MATCHES_EVERYTHING ,
50- "[(all [[of] the]|the)] %*entitydatas% [(in|of) ([ world[s]] %-worlds%|1¦%- chunks%)]" ,
51- "[(all [[of] the]|the)] entities of type[s] %entitydatas% [(in|of) ([ world[s]] %-worlds%|1¦%- chunks%)]" ,
45+ "[(all [[of] the]|the)] %*entitydatas% [(in|of) (world[s] %-worlds%|1:%-worlds/ chunks%)]" ,
46+ "[(all [[of] the]|the)] entities of type[s] %entitydatas% [(in|of) (world[s] %-worlds%|1:%-worlds/ chunks%)]" ,
5247 "[(all [[of] the]|the)] %*entitydatas% (within|[with]in radius) %number% [(block[s]|met(er|re)[s])] (of|around) %location%" ,
5348 "[(all [[of] the]|the)] entities of type[s] %entitydatas% in radius %number% (of|around) %location%" ,
5449 "[(all [[of] the]|the)] %*entitydatas% within %location% and %location%" ,
@@ -59,9 +54,7 @@ public class ExprEntities extends SimpleExpression<Entity> {
5954 Expression <? extends EntityData <?>> types ;
6055
6156 @ UnknownNullability
62- private Expression <World > worlds ;
63- @ UnknownNullability
64- private Expression <Chunk > chunks ;
57+ private Expression <?> worldsOrChunks ;
6558 @ UnknownNullability
6659 private Expression <Number > radius ;
6760 @ UnknownNullability
@@ -95,9 +88,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
9588 to = (Expression <Location >) exprs [2 ];
9689 } else {
9790 if (parseResult .mark == 1 ) {
98- chunks = ( Expression < Chunk >) exprs [2 ];
91+ worldsOrChunks = exprs [2 ];
9992 } else {
100- worlds = ( Expression < World >) exprs [1 ];
93+ worldsOrChunks = exprs [1 ];
10194 }
10295 }
10396 if (types instanceof Literal && ((Literal <EntityData <?>>) types ).getAll ().length == 1 )
@@ -137,11 +130,27 @@ public boolean isLoopOf(String s) {
137130 list .add (iter .next ());
138131 return list .toArray ((Entity []) Array .newInstance (returnType , list .size ()));
139132 } else {
140- if (chunks != null ) {
141- return EntityData .getAll (types .getArray (event ), returnType , chunks .getArray (event ));
142- } else {
143- return EntityData .getAll (types .getAll (event ), returnType , worlds != null ? worlds .getArray (event ) : null );
133+ EntityData <?>[] types = this .types .getAll (event );
134+ if (worldsOrChunks == null ) {
135+ return EntityData .getAll (types , returnType , (World []) null );
136+ }
137+ List <Chunk > chunks = new ArrayList <>();
138+ List <World > worlds = new ArrayList <>();
139+ for (Object obj : worldsOrChunks .getArray (event )) {
140+ if (obj instanceof Chunk chunk ) {
141+ chunks .add (chunk );
142+ } else if (obj instanceof World world ) {
143+ worlds .add (world );
144+ }
144145 }
146+ Set <Entity > entities = new HashSet <>();
147+ if (!chunks .isEmpty ()) {
148+ entities .addAll (Arrays .asList (EntityData .getAll (types , returnType , chunks .toArray (new Chunk [0 ]))));
149+ }
150+ if (!worlds .isEmpty ()) {
151+ entities .addAll (Arrays .asList (EntityData .getAll (types , returnType , worlds .toArray (new World [0 ]))));
152+ }
153+ return entities .toArray ((Entity []) Array .newInstance (returnType , entities .size ()));
145154 }
146155 }
147156
@@ -197,10 +206,7 @@ public Iterator<? extends Entity> iterator(Event event) {
197206 return false ;
198207 });
199208 } else {
200- if (chunks == null || returnType == Player .class )
201- return super .iterator (event );
202-
203- return Arrays .stream (EntityData .getAll (types .getArray (event ), returnType , chunks .getArray (event ))).iterator ();
209+ return super .iterator (event );
204210 }
205211 }
206212
@@ -216,14 +222,14 @@ public Class<? extends Entity> getReturnType() {
216222
217223 @ Override
218224 @ SuppressWarnings ("null" )
219- public String toString (@ Nullable Event e , boolean debug ) {
220- String message = "all entities of type " + types .toString (e , debug );
221- if (worlds != null )
222- message += " in " + worlds .toString (e , debug );
225+ public String toString (@ Nullable Event event , boolean debug ) {
226+ String message = "all entities of type " + types .toString (event , debug );
227+ if (worldsOrChunks != null )
228+ message += " in " + worldsOrChunks .toString (event , debug );
223229 else if (radius != null && center != null )
224- message += " in radius " + radius .toString (e , debug ) + " around " + center .toString (e , debug );
230+ message += " in radius " + radius .toString (event , debug ) + " around " + center .toString (event , debug );
225231 else if (from != null && to != null )
226- message += " within " + from .toString (e , debug ) + " and " + to .toString (e , debug );
232+ message += " within " + from .toString (event , debug ) + " and " + to .toString (event , debug );
227233 return message ;
228234 }
229235
0 commit comments