From 485cc40d70fcb82a5538f2dac8713c253c943aed 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 ecd1c65a98..1898ab897c 100644 --- a/src/main/java/com/destroystokyo/paper/PaperCommand.java +++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java @@ -128,6 +128,7 @@ public class PaperCommand extends Command { List entities = world.entityList; entities.forEach(e -> { MinecraftKey key = EntityTypes.getKey(e); + 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/Entity.java b/src/main/java/net/minecraft/server/Entity.java index 1e64d5fcd6..45e149f4a3 100644 --- a/src/main/java/net/minecraft/server/Entity.java +++ b/src/main/java/net/minecraft/server/Entity.java @@ -124,6 +124,7 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper protected boolean E; private boolean aw; public boolean dead; + public boolean shouldBeRemoved; // Paper public float width; public float length; public float I; diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java index 8822d17745..3ce6f9778b 100644 --- a/src/main/java/net/minecraft/server/World.java +++ b/src/main/java/net/minecraft/server/World.java @@ -1215,6 +1215,7 @@ public abstract class World implements IBlockAccess { } entity.valid = true; // CraftBukkit + 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 } @@ -1280,6 +1281,7 @@ public abstract class World implements IBlockAccess { if (entity.aa && this.isChunkLoaded(i, j, true)) { this.getChunkAt(i, j).b(entity); } + entity.shouldBeRemoved = true; // Paper if (!guardEntityList) { // Spigot - It will get removed after the tick if we are ticking // Paper - always remove from current chunk above // CraftBukkit start - Decrement loop variable field if we've already ticked this entity @@ -2634,6 +2636,7 @@ public abstract class World implements IBlockAccess { while (iterator.hasNext()) { Entity entity = (Entity) iterator.next(); + if (entity.shouldBeRemoved) continue; // Paper if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply((T) entity)) { arraylist.add(entity); @@ -2720,6 +2723,7 @@ public abstract class World implements IBlockAccess { while (iterator.hasNext()) { Entity entity = (Entity) iterator.next(); + if (entity.shouldBeRemoved) continue; // Paper // CraftBukkit start - Split out persistent check, don't apply it to special persistent mobs if (entity instanceof EntityInsentient) { EntityInsentient entityinsentient = (EntityInsentient) entity; diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 210e3bc4e6..e6ecd1796c 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -678,6 +678,7 @@ public class CraftWorld implements World { for (Object o : world.entityList) { 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 @@ -696,6 +697,7 @@ public class CraftWorld implements World { for (Object o : world.entityList) { 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 @@ -720,6 +722,7 @@ public class CraftWorld implements World { for (Object entity: world.entityList) { 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) { @@ -742,6 +745,7 @@ public class CraftWorld implements World { for (Object entity: world.entityList) { 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) { -- 2.18.0