Removed Tile numeric runtime IDs, use block hashes instead

Tiles are level-local and are not indexed globally like entities. There is pretty much zero point to them having numeric runtime IDs.
This commit is contained in:
Dylan K. Taylor 2018-08-19 16:37:04 +01:00
parent 92aeab0d00
commit 7c41bfcdf3
3 changed files with 12 additions and 40 deletions

View File

@ -746,9 +746,9 @@ class Level implements ChunkManager, Metadatable{
$this->timings->tileEntityTick->startTiming(); $this->timings->tileEntityTick->startTiming();
Timings::$tickTileEntityTimer->startTiming(); Timings::$tickTileEntityTimer->startTiming();
//Update tiles that need update //Update tiles that need update
foreach($this->updateTiles as $id => $tile){ foreach($this->updateTiles as $blockHash => $tile){
if(!$tile->onUpdate()){ if(!$tile->onUpdate()){
unset($this->updateTiles[$id]); unset($this->updateTiles[$blockHash]);
} }
} }
Timings::$tickTileEntityTimer->stopTiming(); Timings::$tickTileEntityTimer->stopTiming();
@ -1999,15 +1999,6 @@ class Level implements ChunkManager, Metadatable{
return $this->tiles; return $this->tiles;
} }
/**
* @param $tileId
*
* @return Tile|null
*/
public function getTileById(int $tileId){
return $this->tiles[$tileId] ?? null;
}
/** /**
* Returns a list of the players in this level * Returns a list of the players in this level
* *
@ -2048,13 +2039,7 @@ class Level implements ChunkManager, Metadatable{
* @return Tile|null * @return Tile|null
*/ */
public function getTileAt(int $x, int $y, int $z) : ?Tile{ public function getTileAt(int $x, int $y, int $z) : ?Tile{
$chunk = $this->getChunk($x >> 4, $z >> 4); return $this->tiles[Level::blockHash($x, $y, $z)] ?? null;
if($chunk !== null){
return $chunk->getTile($x & 0x0f, $y, $z & 0x0f);
}
return null;
} }
/** /**
@ -2589,7 +2574,7 @@ class Level implements ChunkManager, Metadatable{
throw new \InvalidStateException("Attempted to create tile " . get_class($tile) . " in unloaded chunk $chunkX $chunkZ"); throw new \InvalidStateException("Attempted to create tile " . get_class($tile) . " in unloaded chunk $chunkX $chunkZ");
} }
$this->tiles[$tile->getId()] = $tile; $this->tiles[Level::blockHash($tile->x, $tile->y, $tile->z)] = $tile;
$this->clearChunkCache($chunkX, $chunkZ); $this->clearChunkCache($chunkX, $chunkZ);
} }
@ -2603,7 +2588,7 @@ class Level implements ChunkManager, Metadatable{
throw new LevelException("Invalid Tile level"); throw new LevelException("Invalid Tile level");
} }
unset($this->tiles[$tile->getId()], $this->updateTiles[$tile->getId()]); unset($this->tiles[$blockHash = Level::blockHash($tile->x, $tile->y, $tile->z)], $this->updateTiles[$blockHash]);
$chunkX = $tile->getFloorX() >> 4; $chunkX = $tile->getFloorX() >> 4;
$chunkZ = $tile->getFloorZ() >> 4; $chunkZ = $tile->getFloorZ() >> 4;

View File

@ -69,8 +69,6 @@ class Chunk{
/** @var Tile[] */ /** @var Tile[] */
protected $tiles = []; protected $tiles = [];
/** @var Tile[] */
protected $tileList = [];
/** @var Entity[] */ /** @var Entity[] */
protected $entities = []; protected $entities = [];
@ -595,11 +593,11 @@ class Chunk{
if($tile->isClosed()){ if($tile->isClosed()){
throw new \InvalidArgumentException("Attempted to add a garbage closed Tile to a chunk"); 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){ if(isset($this->tiles[$index = (($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]) and $this->tiles[$index] !== $tile){
$this->tileList[$index]->close(); $this->tiles[$index]->close();
} }
$this->tileList[$index] = $tile; $this->tiles[$index] = $tile;
if($this->isInit){ if($this->isInit){
$this->hasChanged = true; $this->hasChanged = true;
} }
@ -609,8 +607,7 @@ class Chunk{
* @param Tile $tile * @param Tile $tile
*/ */
public function removeTile(Tile $tile){ public function removeTile(Tile $tile){
unset($this->tiles[$tile->getId()]); unset($this->tiles[(($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]);
unset($this->tileList[(($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]);
if($this->isInit){ if($this->isInit){
$this->hasChanged = true; $this->hasChanged = true;
} }
@ -650,7 +647,7 @@ class Chunk{
*/ */
public function getTile(int $x, int $y, int $z){ public function getTile(int $x, int $y, int $z){
$index = ($x << 12) | ($z << 8) | $y; $index = ($x << 12) | ($z << 8) | $y;
return $this->tileList[$index] ?? null; return $this->tiles[$index] ?? null;
} }
/** /**

View File

@ -61,9 +61,6 @@ abstract class Tile extends Position{
public const SIGN = "Sign"; public const SIGN = "Sign";
public const SKULL = "Skull"; public const SKULL = "Skull";
/** @var int */
public static $tileCount = 1;
/** @var string[] classes that extend Tile */ /** @var string[] classes that extend Tile */
private static $knownTiles = []; private static $knownTiles = [];
/** @var string[][] */ /** @var string[][] */
@ -71,8 +68,6 @@ abstract class Tile extends Position{
/** @var string */ /** @var string */
public $name; public $name;
/** @var int */
public $id;
/** @var bool */ /** @var bool */
public $closed = false; public $closed = false;
/** @var Server */ /** @var Server */
@ -148,7 +143,6 @@ abstract class Tile extends Position{
$this->server = $level->getServer(); $this->server = $level->getServer();
$this->name = ""; $this->name = "";
$this->id = Tile::$tileCount++;
parent::__construct($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z), $level); parent::__construct($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z), $level);
$this->readSaveData($nbt); $this->readSaveData($nbt);
@ -156,10 +150,6 @@ abstract class Tile extends Position{
$this->getLevel()->addTile($this); $this->getLevel()->addTile($this);
} }
public function getId() : int{
return $this->id;
}
/** /**
* Reads additional data from the CompoundTag on tile creation. * Reads additional data from the CompoundTag on tile creation.
* *
@ -253,7 +243,7 @@ abstract class Tile extends Position{
if($this->closed){ if($this->closed){
throw new \InvalidStateException("Cannot schedule update on garbage tile " . get_class($this)); throw new \InvalidStateException("Cannot schedule update on garbage tile " . get_class($this));
} }
$this->level->updateTiles[$this->id] = $this; $this->level->updateTiles[Level::blockHash($this->x, $this->y, $this->z)] = $this;
} }
public function isClosed() : bool{ public function isClosed() : bool{