From e11f1e94e9fc37332b934c54191d5dee79d5812c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 19 Jun 2017 15:40:31 +0100 Subject: [PATCH] Cleaned up SubChunk implementation --- src/pocketmine/level/format/Chunk.php | 31 +-- src/pocketmine/level/format/EmptySubChunk.php | 14 +- src/pocketmine/level/format/SubChunk.php | 6 +- .../level/format/SubChunkInterface.php | 198 ++++++++++++++++++ .../level/format/io/leveldb/LevelDB.php | 2 +- .../level/format/io/region/Anvil.php | 2 +- .../level/format/io/region/McRegion.php | 2 +- .../level/format/io/region/PMAnvil.php | 2 +- 8 files changed, 228 insertions(+), 29 deletions(-) create mode 100644 src/pocketmine/level/format/SubChunkInterface.php diff --git a/src/pocketmine/level/format/Chunk.php b/src/pocketmine/level/format/Chunk.php index 62faf4d95..b71fae29e 100644 --- a/src/pocketmine/level/format/Chunk.php +++ b/src/pocketmine/level/format/Chunk.php @@ -54,7 +54,7 @@ class Chunk{ protected $height = Chunk::MAX_SUBCHUNKS; - /** @var SubChunk[] */ + /** @var SubChunkInterface[] */ protected $subChunks = []; /** @var EmptySubChunk */ @@ -82,13 +82,13 @@ class Chunk{ protected $NBTentities = []; /** - * @param int $chunkX - * @param int $chunkZ - * @param SubChunk[] $subChunks - * @param CompoundTag[] $entities - * @param CompoundTag[] $tiles - * @param string $biomeIds - * @param int[] $heightMap + * @param int $chunkX + * @param int $chunkZ + * @param SubChunkInterface[] $subChunks + * @param CompoundTag[] $entities + * @param CompoundTag[] $tiles + * @param string $biomeIds + * @param int[] $heightMap */ public function __construct(int $chunkX, int $chunkZ, array $subChunks = [], array $entities = [], array $tiles = [], string $biomeIds = "", array $heightMap = []){ $this->x = $chunkX; @@ -514,7 +514,7 @@ class Chunk{ public function getBlockSkyLightColumn(int $x, int $z) : string{ $result = ""; foreach($this->subChunks as $subChunk){ - $result .= $subChunk->getSkyLightColumn($x, $z); + $result .= $subChunk->getBlockSkyLightColumn($x, $z); } return $result; } @@ -797,9 +797,9 @@ class Chunk{ * @param int $y * @param bool $generateNew Whether to create a new, modifiable subchunk if there is not one in place * - * @return SubChunk|EmptySubChunk + * @return SubChunkInterface */ - public function getSubChunk(int $y, bool $generateNew = false) : SubChunk{ + public function getSubChunk(int $y, bool $generateNew = false) : SubChunkInterface{ if($y < 0 or $y >= $this->height){ return $this->emptySubChunk; }elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){ @@ -811,13 +811,14 @@ class Chunk{ /** * Sets a subchunk in the chunk index - * @param int $y - * @param SubChunk|null $subChunk - * @param bool $allowEmpty Whether to check if the chunk is empty, and if so replace it with an empty stub + * + * @param int $y + * @param SubChunkInterface|null $subChunk + * @param bool $allowEmpty Whether to check if the chunk is empty, and if so replace it with an empty stub * * @return bool */ - public function setSubChunk(int $y, SubChunk $subChunk = null, bool $allowEmpty = false) : bool{ + public function setSubChunk(int $y, SubChunkInterface $subChunk = null, bool $allowEmpty = false) : bool{ if($y < 0 or $y >= $this->height){ return false; } diff --git a/src/pocketmine/level/format/EmptySubChunk.php b/src/pocketmine/level/format/EmptySubChunk.php index 8111fafae..bc5b52b8a 100644 --- a/src/pocketmine/level/format/EmptySubChunk.php +++ b/src/pocketmine/level/format/EmptySubChunk.php @@ -23,11 +23,7 @@ declare(strict_types=1); namespace pocketmine\level\format; -class EmptySubChunk extends SubChunk{ - - public function __construct(){ - - } +class EmptySubChunk implements SubChunkInterface{ public function isEmpty() : bool{ return true; @@ -73,6 +69,10 @@ class EmptySubChunk extends SubChunk{ return false; } + public function getHighestBlockAt(int $x, int $z) : int{ + return -1; + } + public function getBlockIdColumn(int $x, int $z) : string{ return "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; } @@ -85,7 +85,7 @@ class EmptySubChunk extends SubChunk{ return "\x00\x00\x00\x00\x00\x00\x00\x00"; } - public function getSkyLightColumn(int $x, int $z) : string{ + public function getBlockSkyLightColumn(int $x, int $z) : string{ return "\xff\xff\xff\xff\xff\xff\xff\xff"; } @@ -101,7 +101,7 @@ class EmptySubChunk extends SubChunk{ return str_repeat("\x00", 2048); } - public function getSkyLightArray() : string{ + public function getBlockSkyLightArray() : string{ return str_repeat("\xff", 2048); } diff --git a/src/pocketmine/level/format/SubChunk.php b/src/pocketmine/level/format/SubChunk.php index 31d2ff2dd..a3f4931d6 100644 --- a/src/pocketmine/level/format/SubChunk.php +++ b/src/pocketmine/level/format/SubChunk.php @@ -23,7 +23,7 @@ declare(strict_types=1); namespace pocketmine\level\format; -class SubChunk{ +class SubChunk implements SubChunkInterface{ protected $ids; protected $data; @@ -182,7 +182,7 @@ class SubChunk{ return substr($this->blockLight, (($x << 7) | ($z << 3)), 8); } - public function getSkyLightColumn(int $x, int $z) : string{ + public function getBlockSkyLightColumn(int $x, int $z) : string{ return substr($this->skyLight, (($x << 7) | ($z << 3)), 8); } @@ -196,7 +196,7 @@ class SubChunk{ return $this->data; } - public function getSkyLightArray() : string{ + public function getBlockSkyLightArray() : string{ assert(strlen($this->skyLight) === 2048, "Wrong length of skylight array, expecting 2048 bytes, got " . strlen($this->skyLight)); return $this->skyLight; } diff --git a/src/pocketmine/level/format/SubChunkInterface.php b/src/pocketmine/level/format/SubChunkInterface.php new file mode 100644 index 000000000..2690cfc17 --- /dev/null +++ b/src/pocketmine/level/format/SubChunkInterface.php @@ -0,0 +1,198 @@ +getBlockIdArray() . $subChunks[$y]->getBlockDataArray() . - $subChunks[$y]->getSkyLightArray() . + $subChunks[$y]->getBlockSkyLightArray() . $subChunks[$y]->getBlockLightArray() ); } diff --git a/src/pocketmine/level/format/io/region/Anvil.php b/src/pocketmine/level/format/io/region/Anvil.php index 7e35da205..46c53383e 100644 --- a/src/pocketmine/level/format/io/region/Anvil.php +++ b/src/pocketmine/level/format/io/region/Anvil.php @@ -60,7 +60,7 @@ class Anvil extends McRegion{ "Y" => new ByteTag("Y", $y), "Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY "Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())), - "SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray(), "\xff")), + "SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockSkyLightArray(), "\xff")), "BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray())) ]); } diff --git a/src/pocketmine/level/format/io/region/McRegion.php b/src/pocketmine/level/format/io/region/McRegion.php index 7491f8eb2..1d0e9f5ad 100644 --- a/src/pocketmine/level/format/io/region/McRegion.php +++ b/src/pocketmine/level/format/io/region/McRegion.php @@ -72,7 +72,7 @@ class McRegion extends BaseLevelProvider{ $subChunk = $subChunks[$y]; $ids .= $subChunk->getBlockIdColumn($x, $z); $data .= $subChunk->getBlockDataColumn($x, $z); - $skyLight .= $subChunk->getSkyLightColumn($x, $z); + $skyLight .= $subChunk->getBlockSkyLightColumn($x, $z); $blockLight .= $subChunk->getBlockLightColumn($x, $z); } } diff --git a/src/pocketmine/level/format/io/region/PMAnvil.php b/src/pocketmine/level/format/io/region/PMAnvil.php index c3b8c590e..dda1f3a37 100644 --- a/src/pocketmine/level/format/io/region/PMAnvil.php +++ b/src/pocketmine/level/format/io/region/PMAnvil.php @@ -63,7 +63,7 @@ class PMAnvil extends Anvil{ "Y" => new ByteTag("Y", $y), "Blocks" => new ByteArrayTag("Blocks", $subChunk->getBlockIdArray()), "Data" => new ByteArrayTag("Data", $subChunk->getBlockDataArray()), - "SkyLight" => new ByteArrayTag("SkyLight", $subChunk->getSkyLightArray()), + "SkyLight" => new ByteArrayTag("SkyLight", $subChunk->getBlockSkyLightArray()), "BlockLight" => new ByteArrayTag("BlockLight", $subChunk->getBlockLightArray()) ]); }