World: Track entities separately from chunks

this allows entities to exist outside of generated chunks, with one caveat: they won't be saved in such cases.
Obviously, for player entities, this doesn't matter.

fixes #3947
This commit is contained in:
Dylan K. Taylor
2021-09-09 01:17:41 +01:00
parent ba2bfe0e11
commit 34f01a3ce3
4 changed files with 67 additions and 102 deletions

View File

@@ -630,12 +630,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$world = $world ?? $this->getWorld();
$index = World::chunkHash($x, $z);
if(isset($this->usedChunks[$index])){
$chunk = $world->getChunk($x, $z);
if($chunk !== null){ //this might be a chunk that hasn't been generated yet
foreach($chunk->getEntities() as $entity){
if($entity !== $this){
$entity->despawnFrom($this);
}
foreach($world->getChunkEntities($x, $z) as $entity){
if($entity !== $this){
$entity->despawnFrom($this);
}
}
$this->getNetworkSession()->stopUsingChunk($x, $z);
@@ -656,7 +653,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
}
protected function spawnEntitiesOnChunk(int $chunkX, int $chunkZ) : void{
foreach($this->getWorld()->getChunk($chunkX, $chunkZ)->getEntities() as $entity){
foreach($this->getWorld()->getChunkEntities($chunkX, $chunkZ) as $entity){
if($entity !== $this and !$entity->isFlaggedForDespawn()){
$entity->spawnTo($this);
}