Paper icon indicating copy to clipboard operation
Paper copied to clipboard

Can't remove entities during ChunkUnloadEvent

Open TheClowner opened this issue 2 years ago • 6 comments

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

TheClowner avatar Oct 09 '22 20:10 TheClowner

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).

lynxplay avatar Oct 09 '22 21:10 lynxplay

Have same Issue.

Update from paper-1.19.2-134 to paper-1.19.2-211

derEchteJaanHD avatar Oct 15 '22 13:10 derEchteJaanHD

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.

sleepylessons avatar Oct 16 '22 22:10 sleepylessons

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.

0-x-2-2 avatar Oct 17 '22 10:10 0-x-2-2

(Why this whole thing sounds eerily like a really nasty dupe?)

masterdesky avatar Oct 17 '22 10:10 masterdesky

~~The core problem is that the chunk is not added to the full loaded chunk map before the entity callbacks are made.~~ Wrong issue.

Spottedleaf avatar Oct 18 '22 14:10 Spottedleaf

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.

stijnb1234 avatar Oct 30 '23 20:10 stijnb1234

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

Danieljunek17 avatar Nov 06 '23 22:11 Danieljunek17