properly removed from the chunk.
This could lead to dead entities accumulating in memory over time if the
chunk never gets fully unloaded (as it is the case for chunks around the
spawn region).
The issue is that Minecraft processes the removal of these entities
during the next tick, when the chunk has already switched to state
INACCESSIBLE and can no longer be retrieved as usual.
For the purpose of removing dead entities from their still loaded but no
longer accessible chunk, this adds and uses a new method with which a
chunk can be accessed without checking its current state first.
The flag for getChunkAt(int, int, ChunkStatus, boolean)
is actually a flag for whether to bring the underlying
PlayerChunk up to the required ticket level to load the
chunk. So, if the chunk is already at the required level,
but has not yet loaded, the call will actually either
start the load if it has not already been started and
block until completion.
This behaviour is not suitable for just
checking if the chunk is loaded.
Clean up implementation and firing of both of these events by routing
both unload and load behaviors to consistent method calls.
This fixes issues where a few places would not call Load or Unload events
when it should have.
Additionally, reduces diff by moving the neighbor marking code into these
consistent points.
Additional benefits of the change include improving the neighbor marking
methods to use getChunkIfLoaded instead of getLoadedChunkAt in some places,
as the latter will cause chunks to be marked active and not unload.
Finally, this also updates CraftWorld.loadChunk to use the new methods, as the
previous logic did not properly handle the new unload queue.