From 596c8a7b4f0a4491fc276f3ec3ef359c8ea55bf3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 15 Mar 2018 10:21:42 +0000 Subject: [PATCH] Tile: Removed cyclic dependence on Chunks Chunks were used by tiles for a couple of things: - 1. for coordinates - which can be gotten using bitshifts - 2. setChanged() - which is unnecessary as seen in the previous commit Removing this circular dependency was actually remarkably easy to do. --- src/pocketmine/level/Level.php | 25 ++++++++++++++++++++----- src/pocketmine/tile/Spawnable.php | 6 ++---- src/pocketmine/tile/Tile.php | 14 +------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 4ac9bb8f7..5a64b975d 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -2388,7 +2388,6 @@ class Level implements ChunkManager, Metadatable{ foreach($oldTiles as $tile){ $chunk->addTile($tile); $oldChunk->removeTile($tile); - $tile->chunk = $chunk; } } @@ -2600,8 +2599,18 @@ class Level implements ChunkManager, Metadatable{ if($tile->getLevel() !== $this){ throw new LevelException("Invalid Tile level"); } + + $chunkX = $tile->getFloorX() >> 4; + $chunkZ = $tile->getFloorZ() >> 4; + + if(isset($this->chunks[$hash = Level::chunkHash($chunkX, $chunkZ)])){ + $this->chunks[$hash]->addTile($tile); + }else{ + throw new \InvalidStateException("Attempted to create tile " . get_class($tile) . " in unloaded chunk $chunkX $chunkZ"); + } + $this->tiles[$tile->getId()] = $tile; - $this->clearChunkCache($tile->getFloorX() >> 4, $tile->getFloorZ() >> 4); + $this->clearChunkCache($chunkX, $chunkZ); } /** @@ -2614,9 +2623,15 @@ class Level implements ChunkManager, Metadatable{ throw new LevelException("Invalid Tile level"); } - unset($this->tiles[$tile->getId()]); - unset($this->updateTiles[$tile->getId()]); - $this->clearChunkCache($tile->getFloorX() >> 4, $tile->getFloorZ() >> 4); + unset($this->tiles[$tile->getId()], $this->updateTiles[$tile->getId()]); + + $chunkX = $tile->getFloorX() >> 4; + $chunkZ = $tile->getFloorZ() >> 4; + + if(isset($this->chunks[$hash = Level::chunkHash($chunkX, $chunkZ)])){ + $this->chunks[$hash]->removeTile($tile); + } + $this->clearChunkCache($chunkX, $chunkZ); } /** diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index 07d423a34..4c1bced23 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -67,7 +67,7 @@ abstract class Spawnable extends Tile{ } $pk = $this->createSpawnPacket(); - $this->level->addChunkPacket($this->chunk->getX(), $this->chunk->getZ(), $pk); + $this->level->addChunkPacket($this->getFloorX() >> 4, $this->getFloorZ() >> 4, $pk); } /** @@ -78,9 +78,7 @@ abstract class Spawnable extends Tile{ $this->spawnCompoundCache = null; $this->spawnToAll(); - if($this->chunk !== null){ - $this->level->clearChunkCache($this->chunk->getX(), $this->chunk->getZ()); - } + $this->level->clearChunkCache($this->getFloorX() >> 4, $this->getFloorZ() >> 4); } /** diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index 2d37d8418..aa0f5f843 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -31,7 +31,6 @@ use pocketmine\block\Block; use pocketmine\event\Timings; use pocketmine\event\TimingsHandler; use pocketmine\item\Item; -use pocketmine\level\format\Chunk; use pocketmine\level\Level; use pocketmine\level\Position; use pocketmine\math\Vector3; @@ -69,8 +68,6 @@ abstract class Tile extends Position{ /** @var string[][] */ private static $saveNames = []; - /** @var Chunk */ - public $chunk; /** @var string */ public $name; /** @var int */ @@ -161,10 +158,6 @@ abstract class Tile extends Position{ $this->namedtag = $nbt; $this->server = $level->getServer(); $this->setLevel($level); - $this->chunk = $level->getChunk($this->namedtag->getInt(self::TAG_X) >> 4, $this->namedtag->getInt(self::TAG_Z) >> 4, false); - if($this->chunk === null){ - throw new \InvalidStateException("Cannot create tiles in unloaded chunks"); - } $this->name = ""; $this->id = Tile::$tileCount++; @@ -172,7 +165,6 @@ abstract class Tile extends Position{ $this->y = $this->namedtag->getInt(self::TAG_Y); $this->z = $this->namedtag->getInt(self::TAG_Z); - $this->chunk->addTile($this); $this->getLevel()->addTile($this); } @@ -276,11 +268,7 @@ abstract class Tile extends Position{ public function close() : void{ if(!$this->closed){ $this->closed = true; - unset($this->level->updateTiles[$this->id]); - if($this->chunk instanceof Chunk){ - $this->chunk->removeTile($this); - $this->chunk = null; - } + if(($level = $this->getLevel()) instanceof Level){ $level->removeTile($this); $this->setLevel(null);