From 7c41bfcdf3f2157f68d28832fa83f8ccfb76c2aa Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 19 Aug 2018 16:37:04 +0100 Subject: [PATCH] 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. --- src/pocketmine/level/Level.php | 25 +++++-------------------- src/pocketmine/level/format/Chunk.php | 15 ++++++--------- src/pocketmine/tile/Tile.php | 12 +----------- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 768dd0920..9e9514fa9 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -746,9 +746,9 @@ class Level implements ChunkManager, Metadatable{ $this->timings->tileEntityTick->startTiming(); Timings::$tickTileEntityTimer->startTiming(); //Update tiles that need update - foreach($this->updateTiles as $id => $tile){ + foreach($this->updateTiles as $blockHash => $tile){ if(!$tile->onUpdate()){ - unset($this->updateTiles[$id]); + unset($this->updateTiles[$blockHash]); } } Timings::$tickTileEntityTimer->stopTiming(); @@ -1999,15 +1999,6 @@ class Level implements ChunkManager, Metadatable{ 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 * @@ -2048,13 +2039,7 @@ class Level implements ChunkManager, Metadatable{ * @return Tile|null */ public function getTileAt(int $x, int $y, int $z) : ?Tile{ - $chunk = $this->getChunk($x >> 4, $z >> 4); - - if($chunk !== null){ - return $chunk->getTile($x & 0x0f, $y, $z & 0x0f); - } - - return null; + return $this->tiles[Level::blockHash($x, $y, $z)] ?? 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"); } - $this->tiles[$tile->getId()] = $tile; + $this->tiles[Level::blockHash($tile->x, $tile->y, $tile->z)] = $tile; $this->clearChunkCache($chunkX, $chunkZ); } @@ -2603,7 +2588,7 @@ class Level implements ChunkManager, Metadatable{ 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; $chunkZ = $tile->getFloorZ() >> 4; diff --git a/src/pocketmine/level/format/Chunk.php b/src/pocketmine/level/format/Chunk.php index a373a2cd0..88d8d5e1d 100644 --- a/src/pocketmine/level/format/Chunk.php +++ b/src/pocketmine/level/format/Chunk.php @@ -69,8 +69,6 @@ class Chunk{ /** @var Tile[] */ protected $tiles = []; - /** @var Tile[] */ - protected $tileList = []; /** @var Entity[] */ protected $entities = []; @@ -595,11 +593,11 @@ class Chunk{ if($tile->isClosed()){ 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(); + + if(isset($this->tiles[$index = (($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]) and $this->tiles[$index] !== $tile){ + $this->tiles[$index]->close(); } - $this->tileList[$index] = $tile; + $this->tiles[$index] = $tile; if($this->isInit){ $this->hasChanged = true; } @@ -609,8 +607,7 @@ class Chunk{ * @param Tile $tile */ public function removeTile(Tile $tile){ - unset($this->tiles[$tile->getId()]); - unset($this->tileList[(($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]); + unset($this->tiles[(($tile->x & 0x0f) << 12) | (($tile->z & 0x0f) << 8) | ($tile->y & 0xff)]); if($this->isInit){ $this->hasChanged = true; } @@ -650,7 +647,7 @@ class Chunk{ */ public function getTile(int $x, int $y, int $z){ $index = ($x << 12) | ($z << 8) | $y; - return $this->tileList[$index] ?? null; + return $this->tiles[$index] ?? null; } /** diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index 19c16df75..2443e49b7 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -61,9 +61,6 @@ abstract class Tile extends Position{ public const SIGN = "Sign"; public const SKULL = "Skull"; - /** @var int */ - public static $tileCount = 1; - /** @var string[] classes that extend Tile */ private static $knownTiles = []; /** @var string[][] */ @@ -71,8 +68,6 @@ abstract class Tile extends Position{ /** @var string */ public $name; - /** @var int */ - public $id; /** @var bool */ public $closed = false; /** @var Server */ @@ -148,7 +143,6 @@ abstract class Tile extends Position{ $this->server = $level->getServer(); $this->name = ""; - $this->id = Tile::$tileCount++; parent::__construct($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z), $level); $this->readSaveData($nbt); @@ -156,10 +150,6 @@ abstract class Tile extends Position{ $this->getLevel()->addTile($this); } - public function getId() : int{ - return $this->id; - } - /** * Reads additional data from the CompoundTag on tile creation. * @@ -253,7 +243,7 @@ abstract class Tile extends Position{ if($this->closed){ 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{