Skip to content

Commit 2918a1f

Browse files
Merge branch 'dev/patch' into dev/feature
2 parents e2e2e76 + feba26e commit 2918a1f

File tree

3 files changed

+68
-51
lines changed

3 files changed

+68
-51
lines changed

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@
2020
import org.bukkit.Location;
2121
import org.bukkit.World;
2222
import org.bukkit.entity.Entity;
23-
import org.bukkit.entity.Player;
2423
import org.bukkit.event.Event;
2524
import org.bukkit.util.BoundingBox;
2625
import org.jetbrains.annotations.Nullable;
2726
import org.jetbrains.annotations.UnknownNullability;
2827

2928
import 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

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
package ch.njol.skript.expressions;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
6-
import org.bukkit.entity.Player;
7-
import org.bukkit.event.Event;
8-
import org.bukkit.inventory.Inventory;
9-
import org.bukkit.inventory.InventoryHolder;
10-
import org.jetbrains.annotations.Nullable;
11-
123
import ch.njol.skript.Skript;
134
import ch.njol.skript.doc.Description;
145
import ch.njol.skript.doc.Examples;
@@ -19,6 +10,16 @@
1910
import ch.njol.skript.lang.SkriptParser.ParseResult;
2011
import ch.njol.skript.lang.util.SimpleExpression;
2112
import ch.njol.util.Kleenean;
13+
import org.bukkit.entity.HumanEntity;
14+
import org.bukkit.entity.Player;
15+
import org.bukkit.event.Event;
16+
import org.bukkit.inventory.Inventory;
17+
import org.bukkit.inventory.InventoryHolder;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
import java.lang.reflect.Array;
21+
import java.util.ArrayList;
22+
import java.util.List;
2223

2324
@Name("Inventory Holder/Viewers/Rows/Slots")
2425
@Description({"Gets the amount of rows/slots, viewers and holder of an inventory.",
@@ -50,41 +51,42 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
5051
}
5152

5253
@Override
53-
protected Object[] get(Event e) {
54-
Inventory[] inventories = this.inventories.getArray(e);
55-
List<Object> objects = new ArrayList<>();
54+
protected Object[] get(Event event) {
55+
Inventory[] inventories = this.inventories.getArray(event);
5656
switch (type) {
5757
case HOLDER:
58+
List<InventoryHolder> holders = new ArrayList<>();
5859
for (Inventory inventory : inventories) {
5960
InventoryHolder holder = inventory.getHolder();
6061
if (holder != null)
61-
objects.add(holder);
62+
holders.add(holder);
6263
}
63-
break;
64+
return holders.toArray(new InventoryHolder[0]);
6465
case ROWS:
66+
List<Number> rows = new ArrayList<>();
6567
for (Inventory inventory : inventories) {
6668
int size = inventory.getSize();
6769
if (size < 9) // Hoppers have a size of 5, we don't want to return 0
68-
objects.add(1);
70+
rows.add(1);
6971
else
70-
objects.add(size / 9);
72+
rows.add(size / 9);
7173
}
72-
break;
74+
return rows.toArray(new Number[0]);
7375
case SLOTS:
76+
List<Number> sizes = new ArrayList<>();
7477
for (Inventory inventory : inventories) {
75-
objects.add(inventory.getSize());
78+
sizes.add(inventory.getSize());
7679
}
77-
break;
80+
return sizes.toArray(new Number[0]);
7881
case VIEWERS:
82+
List<HumanEntity> viewers = new ArrayList<>();
7983
for (Inventory inventory : inventories) {
80-
objects.addAll(inventory.getViewers());
84+
viewers.addAll(inventory.getViewers());
8185
}
82-
break;
86+
return viewers.stream().filter(viewer -> viewer instanceof Player).toArray(Player[]::new);
8387
default:
84-
return new Object[0];
88+
return (Object[]) Array.newInstance(getReturnType(), 0);
8589
}
86-
return objects.toArray(new Object[0]);
87-
8890
}
8991

9092
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
parse:
3+
results: {5829::parse results}
4+
code:
5+
on chunk load:
6+
broadcast all armor stands in event-chunk
7+
8+
test "entities in chunk parsing":
9+
assert {5829::parse results} is not set with "Failed to parse all armor stands in event-chunk"

0 commit comments

Comments
 (0)