From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 28 Jul 2018 12:18:27 -0400 Subject: [PATCH] Ignore Dead Entities in entityList iteration A spigot change delays removal of entities from the entity list. This causes a change in behavior from Vanilla where getEntities type methods will return dead entities that they shouldn't otherwise be doing. This will ensure that dead entities are skipped from iteration since they shouldn't of been in the list in the first place. diff --git a/src/main/java/com/destroystokyo/paper/PaperCommand.java b/src/main/java/com/destroystokyo/paper/PaperCommand.java index b839769ceae8932bb121a0b96fde1e7d129a1f63..5acad8e44f024d3ddf5ef4fd320460ac516e0fb8 100644 --- a/src/main/java/com/destroystokyo/paper/PaperCommand.java +++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java @@ -179,6 +179,7 @@ public class PaperCommand extends Command { Collection entities = world.entitiesById.values(); entities.forEach(e -> { MinecraftKey key = e.getMinecraftKey(); + if (e.shouldBeRemoved) return; // Paper MutablePair> info = list.computeIfAbsent(key, k -> MutablePair.of(0, Maps.newHashMap())); ChunkCoordIntPair chunk = new ChunkCoordIntPair(e.getChunkX(), e.getChunkZ()); diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java index 3ba19f75d24a1539c94f4fed9b8f90aecc918cdc..f7be161c8557ae3848227b1a61e27374770ad243 100644 --- a/src/main/java/net/minecraft/server/Chunk.java +++ b/src/main/java/net/minecraft/server/Chunk.java @@ -818,6 +818,7 @@ public class Chunk implements IChunkAccess { for (int i1 = 0; i1 < l; ++i1) { Entity entity1 = (Entity) list1.get(i1); + if (entity1.shouldBeRemoved) continue; // Paper if (entity1.getBoundingBox().c(axisalignedbb) && entity1 != entity) { if (predicate == null || predicate.test(entity1)) { @@ -854,6 +855,7 @@ public class Chunk implements IChunkAccess { while (iterator.hasNext()) { T entity = (T) iterator.next(); // CraftBukkit - decompile error + if (entity.shouldBeRemoved) continue; // Paper if ((entitytypes == null || entity.getEntityType() == entitytypes) && entity.getBoundingBox().c(axisalignedbb) && predicate.test(entity)) { list.add(entity); @@ -875,6 +877,7 @@ public class Chunk implements IChunkAccess { while (iterator.hasNext()) { T t0 = (T) iterator.next(); // CraftBukkit - decompile error + if (t0.shouldBeRemoved) continue; // Paper if (oclass.isInstance(t0) && t0.getBoundingBox().c(axisalignedbb) && (predicate == null || predicate.test(t0))) { // Spigot - instance check list.add(t0); diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java index 6012891971265323603cb05ae444cca1bb058c89..9163916e6f600f2efa3e05f3df1d07ef9b60cd3a 100644 --- a/src/main/java/net/minecraft/server/Entity.java +++ b/src/main/java/net/minecraft/server/Entity.java @@ -196,6 +196,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke protected int numCollisions = 0; // Paper public void inactiveTick() { } // Spigot end + public boolean shouldBeRemoved; // Paper public float getBukkitYaw() { return this.yaw; diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java index d81d6f7b984dc3587baadc96fc33f087b3594dcc..ba0bb12dd5c5068d31c8a925e31804ddf2cec5da 100644 --- a/src/main/java/net/minecraft/server/WorldServer.java +++ b/src/main/java/net/minecraft/server/WorldServer.java @@ -1153,6 +1153,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { entity.origin = entity.getBukkitEntity().getLocation(); } // Paper end + entity.shouldBeRemoved = false; // Paper - shouldn't be removed after being re-added new com.destroystokyo.paper.event.entity.EntityAddToWorldEvent(entity.getBukkitEntity()).callEvent(); // Paper - fire while valid } @@ -1165,6 +1166,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { this.removeEntityFromChunk(entity); this.entitiesById.remove(entity.getId()); this.unregisterEntity(entity); + entity.shouldBeRemoved = true; // Paper } } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 66c89831796db1bbdd6c83cba786b27ef339ec9b..411d7a3f7d4d54fbf748888e24f2032c4d091250 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -1031,6 +1031,7 @@ public class CraftWorld implements World { for (Object o : world.entitiesById.values()) { if (o instanceof net.minecraft.server.Entity) { net.minecraft.server.Entity mcEnt = (net.minecraft.server.Entity) o; + if (mcEnt.shouldBeRemoved) continue; // Paper Entity bukkitEntity = mcEnt.getBukkitEntity(); // Assuming that bukkitEntity isn't null @@ -1050,6 +1051,7 @@ public class CraftWorld implements World { for (Object o : world.entitiesById.values()) { if (o instanceof net.minecraft.server.Entity) { net.minecraft.server.Entity mcEnt = (net.minecraft.server.Entity) o; + if (mcEnt.shouldBeRemoved) continue; // Paper Entity bukkitEntity = mcEnt.getBukkitEntity(); // Assuming that bukkitEntity isn't null @@ -1076,6 +1078,7 @@ public class CraftWorld implements World { for (Object entity: world.entitiesById.values()) { if (entity instanceof net.minecraft.server.Entity) { + if (((net.minecraft.server.Entity) entity).shouldBeRemoved) continue; // Paper Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity(); if (bukkitEntity == null) { @@ -1099,6 +1102,7 @@ public class CraftWorld implements World { for (Object entity: world.entitiesById.values()) { if (entity instanceof net.minecraft.server.Entity) { + if (((net.minecraft.server.Entity) entity).shouldBeRemoved) continue; // Paper Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity(); if (bukkitEntity == null) {