Paper
Paper copied to clipboard
Can't remove entities during ChunkUnloadEvent
Expected behavior
When ChunkUnloadEvent is called, iterating through the entities and removing specific ones by calling that entity's .remove() method should delete it from the world and that would not be saved in the chunk.
Observed/Actual behavior
Entities are not removed, and appear again when the chunk loads.
Steps/models to reproduce
I created a testPlugin that had this listener to check the behaviour
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
for (Entity e:event.getChunk().getEntities()) {
this.getLogger().info("Removing Entity " + e.getUniqueId());
e.remove();
}
}
Spawn & name some entities, let the chunk unload, reload the chunk, see entities haven't been removed.
Plugin and Datapack List
Only a test plugin to check this behaviour
Paper version
This server is running Paper version git-Paper-200 (MC: 1.19.2) (Implementing API version 1.19.2-R0.1-SNAPSHOT) (Git: c389b1c) You are running the latest version Previous version: git-Paper-173 (MC: 1.19.2)
Other
This seems to have started being an issue since the new chunk system, as it works as expected on build #173
Pretty much known. Generally removal or addition during the ChunkLoad/UnloadEvent and EntitesLoad/UnloadEvents is having issues after the chunk rewrite.
There is #6451 which tracks something presumably similar now (after it was expanded, see owens comment).
Have same Issue.
Update from paper-1.19.2-134 to paper-1.19.2-211
Just wanted to put my two cents in here. The onChunkUnload event gets called at seemingly random times. I was testing it and the event gets fired even when a player is still standing in the chunk.
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk();
if (chunk.getX() == 50 && chunk.getY() == 50) {
System.out.println("The chunk has been unloaded");
}
}
I have another account sitting in that exact chunk, and my primary account exits through a Nether Portal in the same chunk. My System.out.println
message gets logged while the second account is still in the chunk.
I have another account sitting in that exact chunk, and my primary account exits through a Nether Portal in the same chunk. My
System.out.println
message gets logged while the second account is still in the chunk.
Sounds like the event is fired from entity tracking to me, which would not be unexpected due to many circumstances that would make such an event tricky to implement safely with the Bukkit API as I would assume at this point true chunk unloads are an async operation.
(Why this whole thing sounds eerily like a really nasty dupe?)
~~The core problem is that the chunk is not added to the full loaded chunk map before the entity callbacks are made.~~ Wrong issue.
What is the correct way to implement this for Paper servers? I need to despawn my entities in a chunk when it gets unloaded so that they do not get invalidated instead.
I seem to have the same problem with vehicles (invisible armorstands) they bugg when despawned in unloaded chunks and using this would be a nice way to always despawn vehicles in unloaded chunks which also helps on restarts and to just spawn then back on chunkload event (if player or admin didn't despawn the vehicle in the meantime but since this method also doesn't work I am running a little out of options