From 007aee72f848df4a064e9b72e236f1b483c40463 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 8 Jul 2019 15:01:11 +0100 Subject: [PATCH] SubChunk: remove BlockLegacyIds dependency, allow parameterising default block --- src/pocketmine/world/format/Chunk.php | 2 +- src/pocketmine/world/format/SubChunk.php | 17 ++++++++++------- .../world/format/io/FastChunkSerializer.php | 3 ++- .../world/format/io/leveldb/LevelDB.php | 8 ++++---- src/pocketmine/world/format/io/region/Anvil.php | 3 ++- .../world/format/io/region/McRegion.php | 3 ++- .../world/format/io/region/PMAnvil.php | 3 ++- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/pocketmine/world/format/Chunk.php b/src/pocketmine/world/format/Chunk.php index a0851eafb..cf76026cf 100644 --- a/src/pocketmine/world/format/Chunk.php +++ b/src/pocketmine/world/format/Chunk.php @@ -644,7 +644,7 @@ class Chunk{ if($y < 0 or $y >= $this->height){ return $this->emptySubChunk; }elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){ - $this->subChunks[$y] = new SubChunk([new PalettedBlockArray(BlockLegacyIds::AIR << 4)]); + $this->subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, []); } return $this->subChunks[$y]; diff --git a/src/pocketmine/world/format/SubChunk.php b/src/pocketmine/world/format/SubChunk.php index c84976bf3..dd7f228f1 100644 --- a/src/pocketmine/world/format/SubChunk.php +++ b/src/pocketmine/world/format/SubChunk.php @@ -23,10 +23,11 @@ declare(strict_types=1); namespace pocketmine\world\format; -use pocketmine\block\BlockLegacyIds; use function array_values; class SubChunk implements SubChunkInterface{ + /** @var int */ + private $defaultBlock; /** @var PalettedBlockArray[] */ private $blockLayers; @@ -38,11 +39,13 @@ class SubChunk implements SubChunkInterface{ /** * SubChunk constructor. * + * @param int $default * @param PalettedBlockArray[] $blocks * @param LightArray|null $skyLight * @param LightArray|null $blockLight */ - public function __construct(array $blocks, ?LightArray $skyLight = null, ?LightArray $blockLight = null){ + public function __construct(int $default, array $blocks, ?LightArray $skyLight = null, ?LightArray $blockLight = null){ + $this->defaultBlock = $default; $this->blockLayers = $blocks; $this->skyLight = $skyLight ?? new LightArray(LightArray::FIFTEEN); @@ -53,7 +56,7 @@ class SubChunk implements SubChunkInterface{ foreach($this->blockLayers as $layer){ $palette = $layer->getPalette(); foreach($palette as $p){ - if($p !== (BlockLegacyIds::AIR << 4)){ + if($p !== $this->defaultBlock){ return false; } } @@ -68,14 +71,14 @@ class SubChunk implements SubChunkInterface{ public function getFullBlock(int $x, int $y, int $z) : int{ if(empty($this->blockLayers)){ - return BlockLegacyIds::AIR << 4; + return $this->defaultBlock; } return $this->blockLayers[0]->get($x, $y, $z); } public function setFullBlock(int $x, int $y, int $z, int $block) : void{ if(empty($this->blockLayers)){ - $this->blockLayers[] = new PalettedBlockArray(BlockLegacyIds::AIR << 4); + $this->blockLayers[] = new PalettedBlockArray($this->defaultBlock); } $this->blockLayers[0]->set($x, $y, $z, $block); } @@ -112,7 +115,7 @@ class SubChunk implements SubChunkInterface{ return -1; } for($y = 15; $y >= 0; --$y){ - if($this->blockLayers[0]->get($x, $y, $z) !== (BlockLegacyIds::AIR << 4)){ + if($this->blockLayers[0]->get($x, $y, $z) !== $this->defaultBlock){ return $y; } } @@ -145,7 +148,7 @@ class SubChunk implements SubChunkInterface{ $layer->collectGarbage(); foreach($layer->getPalette() as $p){ - if($p !== (BlockLegacyIds::AIR << 4)){ + if($p !== $this->defaultBlock){ continue 2; } } diff --git a/src/pocketmine/world/format/io/FastChunkSerializer.php b/src/pocketmine/world/format/io/FastChunkSerializer.php index a4ab01c5c..20490c4de 100644 --- a/src/pocketmine/world/format/io/FastChunkSerializer.php +++ b/src/pocketmine/world/format/io/FastChunkSerializer.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io; +use pocketmine\block\BlockLegacyIds; use pocketmine\utils\BinaryStream; use pocketmine\world\format\Chunk; use pocketmine\world\format\EmptySubChunk; @@ -135,7 +136,7 @@ final class FastChunkSerializer{ $layers[] = PalettedBlockArray::fromData($bitsPerBlock, $words, $palette); } $subChunks[$y] = new SubChunk( - $layers, $lightPopulated ? new LightArray($stream->get(2048)) : null, $lightPopulated ? new LightArray($stream->get(2048)) : null + BlockLegacyIds::AIR << 4, $layers, $lightPopulated ? new LightArray($stream->get(2048)) : null, $lightPopulated ? new LightArray($stream->get(2048)) : null ); } diff --git a/src/pocketmine/world/format/io/leveldb/LevelDB.php b/src/pocketmine/world/format/io/leveldb/LevelDB.php index c608e19e1..9b9e0a06f 100644 --- a/src/pocketmine/world/format/io/leveldb/LevelDB.php +++ b/src/pocketmine/world/format/io/leveldb/LevelDB.php @@ -303,14 +303,14 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{ $storages[] = $convertedLegacyExtraData[$y]; } - $subChunks[$y] = new SubChunk($storages); + $subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages); break; case 1: //paletted v1, has a single blockstorage $storages = [$this->deserializePaletted($binaryStream)]; if(isset($convertedLegacyExtraData[$y])){ $storages[] = $convertedLegacyExtraData[$y]; } - $subChunks[$y] = new SubChunk($storages); + $subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages); break; case 8: //legacy extradata layers intentionally ignored because they aren't supposed to exist in v8 @@ -321,7 +321,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{ for($k = 0; $k < $storageCount; ++$k){ $storages[] = $this->deserializePaletted($binaryStream); } - $subChunks[$y] = new SubChunk($storages); + $subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages); } break; default: @@ -365,7 +365,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{ if(isset($convertedLegacyExtraData[$yy])){ $storages[] = $convertedLegacyExtraData[$yy]; } - $subChunks[$yy] = new SubChunk($storages); + $subChunks[$yy] = new SubChunk(BlockLegacyIds::AIR << 4, $storages); } try{ diff --git a/src/pocketmine/world/format/io/region/Anvil.php b/src/pocketmine/world/format/io/region/Anvil.php index e66e2a345..1bd9450f8 100644 --- a/src/pocketmine/world/format/io/region/Anvil.php +++ b/src/pocketmine/world/format/io/region/Anvil.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io\region; +use pocketmine\block\BlockLegacyIds; use pocketmine\nbt\tag\CompoundTag; use pocketmine\world\format\io\SubChunkConverter; use pocketmine\world\format\SubChunk; @@ -35,7 +36,7 @@ class Anvil extends RegionWorldProvider{ } protected function deserializeSubChunk(CompoundTag $subChunk) : SubChunk{ - return new SubChunk([SubChunkConverter::convertSubChunkYZX($subChunk->getByteArray("Blocks"), $subChunk->getByteArray("Data"))]); + return new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkYZX($subChunk->getByteArray("Blocks"), $subChunk->getByteArray("Data"))]); //ignore legacy light information } diff --git a/src/pocketmine/world/format/io/region/McRegion.php b/src/pocketmine/world/format/io/region/McRegion.php index 46e2b9f38..a695a3372 100644 --- a/src/pocketmine/world/format/io/region/McRegion.php +++ b/src/pocketmine/world/format/io/region/McRegion.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io\region; +use pocketmine\block\BlockLegacyIds; use pocketmine\nbt\BigEndianNbtSerializer; use pocketmine\nbt\NbtDataException; use pocketmine\nbt\tag\ByteArrayTag; @@ -71,7 +72,7 @@ class McRegion extends RegionWorldProvider{ $fullData = $chunk->hasTag("Data", ByteArrayTag::class) ? $chunk->getByteArray("Data") : str_repeat("\x00", 16384); for($y = 0; $y < 8; ++$y){ - $subChunks[$y] = new SubChunk([SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]); + $subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]); } if($chunk->hasTag("BiomeColors", IntArrayTag::class)){ diff --git a/src/pocketmine/world/format/io/region/PMAnvil.php b/src/pocketmine/world/format/io/region/PMAnvil.php index 3842dff7d..557026a9d 100644 --- a/src/pocketmine/world/format/io/region/PMAnvil.php +++ b/src/pocketmine/world/format/io/region/PMAnvil.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io\region; +use pocketmine\block\BlockLegacyIds; use pocketmine\nbt\tag\CompoundTag; use pocketmine\world\format\io\SubChunkConverter; use pocketmine\world\format\SubChunk; @@ -35,7 +36,7 @@ class PMAnvil extends RegionWorldProvider{ use LegacyAnvilChunkTrait; protected function deserializeSubChunk(CompoundTag $subChunk) : SubChunk{ - return new SubChunk([SubChunkConverter::convertSubChunkXZY($subChunk->getByteArray("Blocks"), $subChunk->getByteArray("Data"))]); + return new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkXZY($subChunk->getByteArray("Blocks"), $subChunk->getByteArray("Data"))]); } protected static function getRegionFileExtension() : string{