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.
This commit is contained in:
Dylan K. Taylor 2018-03-15 10:21:42 +00:00
parent 9c598d1345
commit 596c8a7b4f
3 changed files with 23 additions and 22 deletions

View File

@ -2388,7 +2388,6 @@ class Level implements ChunkManager, Metadatable{
foreach($oldTiles as $tile){ foreach($oldTiles as $tile){
$chunk->addTile($tile); $chunk->addTile($tile);
$oldChunk->removeTile($tile); $oldChunk->removeTile($tile);
$tile->chunk = $chunk;
} }
} }
@ -2600,8 +2599,18 @@ class Level implements ChunkManager, Metadatable{
if($tile->getLevel() !== $this){ if($tile->getLevel() !== $this){
throw new LevelException("Invalid Tile level"); 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->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"); throw new LevelException("Invalid Tile level");
} }
unset($this->tiles[$tile->getId()]); unset($this->tiles[$tile->getId()], $this->updateTiles[$tile->getId()]);
unset($this->updateTiles[$tile->getId()]);
$this->clearChunkCache($tile->getFloorX() >> 4, $tile->getFloorZ() >> 4); $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);
} }
/** /**

View File

@ -67,7 +67,7 @@ abstract class Spawnable extends Tile{
} }
$pk = $this->createSpawnPacket(); $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->spawnCompoundCache = null;
$this->spawnToAll(); $this->spawnToAll();
if($this->chunk !== null){ $this->level->clearChunkCache($this->getFloorX() >> 4, $this->getFloorZ() >> 4);
$this->level->clearChunkCache($this->chunk->getX(), $this->chunk->getZ());
}
} }
/** /**

View File

@ -31,7 +31,6 @@ use pocketmine\block\Block;
use pocketmine\event\Timings; use pocketmine\event\Timings;
use pocketmine\event\TimingsHandler; use pocketmine\event\TimingsHandler;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
@ -69,8 +68,6 @@ abstract class Tile extends Position{
/** @var string[][] */ /** @var string[][] */
private static $saveNames = []; private static $saveNames = [];
/** @var Chunk */
public $chunk;
/** @var string */ /** @var string */
public $name; public $name;
/** @var int */ /** @var int */
@ -161,10 +158,6 @@ abstract class Tile extends Position{
$this->namedtag = $nbt; $this->namedtag = $nbt;
$this->server = $level->getServer(); $this->server = $level->getServer();
$this->setLevel($level); $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->name = "";
$this->id = Tile::$tileCount++; $this->id = Tile::$tileCount++;
@ -172,7 +165,6 @@ abstract class Tile extends Position{
$this->y = $this->namedtag->getInt(self::TAG_Y); $this->y = $this->namedtag->getInt(self::TAG_Y);
$this->z = $this->namedtag->getInt(self::TAG_Z); $this->z = $this->namedtag->getInt(self::TAG_Z);
$this->chunk->addTile($this);
$this->getLevel()->addTile($this); $this->getLevel()->addTile($this);
} }
@ -276,11 +268,7 @@ abstract class Tile extends Position{
public function close() : void{ public function close() : void{
if(!$this->closed){ if(!$this->closed){
$this->closed = true; $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){ if(($level = $this->getLevel()) instanceof Level){
$level->removeTile($this); $level->removeTile($this);
$this->setLevel(null); $this->setLevel(null);