From eefa8abaf218f1dad0976fb2ba3b62390365d311 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 18 Apr 2017 14:45:33 +0100 Subject: [PATCH 1/2] Throw exceptions if something attempts to add a closed Tile or Entity to a chunk --- src/pocketmine/level/format/Chunk.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pocketmine/level/format/Chunk.php b/src/pocketmine/level/format/Chunk.php index 9f47296cf..00db9a89c 100644 --- a/src/pocketmine/level/format/Chunk.php +++ b/src/pocketmine/level/format/Chunk.php @@ -568,6 +568,9 @@ class Chunk{ * @param Entity $entity */ public function addEntity(Entity $entity){ + if($entity->closed){ + throw new \InvalidArgumentException("Attempted to add a garbage closed Entity to a chunk"); + } $this->entities[$entity->getId()] = $entity; if(!($entity instanceof Player) and $this->isInit){ $this->hasChanged = true; @@ -588,6 +591,9 @@ class Chunk{ * @param Tile $tile */ public function addTile(Tile $tile){ + if($tile->closed){ + throw new \InvalidArgumentException("Attempted to add a garbage closed Tile to a chunk"); + } $this->tiles[$tile->getId()] = $tile; if(isset($this->tileList[$index = (($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]) and $this->tileList[$index] !== $tile){ $this->tileList[$index]->close(); From d2efcee115e9edac8c200a9169edfba656ad2c8c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 18 Apr 2017 14:47:42 +0100 Subject: [PATCH 2/2] Fixed tiles and entities being closed when replacing chunks, should fix #490 --- src/pocketmine/level/Level.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 8e480d712..44e27894b 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -2228,18 +2228,20 @@ class Level implements ChunkManager, Metadatable{ $oldEntities = $oldChunk !== null ? $oldChunk->getEntities() : []; $oldTiles = $oldChunk !== null ? $oldChunk->getTiles() : []; - $this->provider->setChunk($chunkX, $chunkZ, $chunk); - $this->chunks[$index] = $chunk; - foreach($oldEntities as $entity){ $chunk->addEntity($entity); + $oldChunk->removeEntity($entity); $entity->chunk = $chunk; } foreach($oldTiles as $tile){ $chunk->addTile($tile); + $oldChunk->removeTile($tile); $tile->chunk = $chunk; } + + $this->provider->setChunk($chunkX, $chunkZ, $chunk); + $this->chunks[$index] = $chunk; } unset($this->chunkCache[$index]);