[SPIGOT-2644] Removing hanging entity and respawning new one in same location within same tick causes NPE Created: 31/Aug/16  Updated: 31/Aug/16  Resolved: 31/Aug/16

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Maxim Roncace Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: entities
Environment:

Java 8u102 on Windows 10 1607



 Description   

The following steps will produce a `NullPointerException` on the latest CraftBukkit/Spigot versions:

  1. Place a hanging entity on a block such that no blocks surround it bar the one it's attached to.
  2. Attempt to remove it via `Entity#remove`.
  3. Within the same tick, attempt to spawn a new hanging entity in the same location.

The last step will fail with a `NullPointerException`, because the server cannot find a suitable direction for the new entity since it believes the space is occupied by an entity currently marked for removal. Therefore local variable `face` will be `null` and [this line](https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/CraftWorld.java?at=43ab266#1088) will fail.

The obvious solution is to modify [this line](https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/CraftWorld.java?at=43ab266#1072) to include the condition `!e.isDead()`. However, I am not knowledgeable enough to say whether this would present a technical conflict, so alternatively I propose that in the case that a suitable `BlockFace` cannot be determined, an `IllegalArgumentException` is thrown to signify that and why it is not possible to spawn an entity with the given parameters, which would be much better UX than a relatively mysterious NPE.



 Comments   
Comment by md_5 [ 31/Aug/16 ]

It's not as simple as isDead because the hanging entity will not survive the tick anyway.
I've simply added a better exception.

Generated at Thu Mar 27 04:31:27 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.