From 72781f4042a6da374cfe4bea8a11d5b226d67161 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Fri, 27 Jun 2025 15:54:42 +0000 Subject: [PATCH] Add helper methods for layer emptiness checks and update serializers - Add isBlockLayerEmpty() and isLiquidLayerEmpty() methods to SubChunk to avoid code duplication - Update ChunkSerializer to use emptiness checks instead of null checks for layers - Update LevelDB to use the new helper methods for cleaner code - Ensures consistent emptiness checking logic across all serializers --- src/network/mcpe/serializer/ChunkSerializer.php | 10 +++++----- src/world/format/SubChunk.php | 14 ++++++++++++++ src/world/format/io/leveldb/LevelDB.php | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/network/mcpe/serializer/ChunkSerializer.php b/src/network/mcpe/serializer/ChunkSerializer.php index 855bcd24e..62b50eab2 100644 --- a/src/network/mcpe/serializer/ChunkSerializer.php +++ b/src/network/mcpe/serializer/ChunkSerializer.php @@ -112,13 +112,13 @@ final class ChunkSerializer{ } public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, PacketSerializer $stream, bool $persistentBlockStates) : void{ - // Create array from the new methods to minimize code changes + // Create array from non-empty layers $layers = []; - if(($blockLayer = $subChunk->getBlockLayer()) !== null){ - $layers[] = $blockLayer; + if(!$subChunk->isBlockLayerEmpty()){ + $layers[] = $subChunk->getBlockLayer(); } - if(($liquidLayer = $subChunk->getLiquidLayer()) !== null){ - $layers[] = $liquidLayer; + if(!$subChunk->isLiquidLayerEmpty()){ + $layers[] = $subChunk->getLiquidLayer(); } $stream->putByte(8); //version diff --git a/src/world/format/SubChunk.php b/src/world/format/SubChunk.php index 510d34487..dbcf3fdd7 100644 --- a/src/world/format/SubChunk.php +++ b/src/world/format/SubChunk.php @@ -76,6 +76,20 @@ class SubChunk{ $this->liquidLayer->getBitsPerBlock() === 0 && $this->liquidLayer->get(0, 0, 0) === $this->emptyBlockId; } + /** + * Returns whether the block layer is empty (contains only empty blocks). + */ + public function isBlockLayerEmpty() : bool{ + return $this->blockLayer->getBitsPerBlock() === 0 && $this->blockLayer->get(0, 0, 0) === $this->emptyBlockId; + } + + /** + * Returns whether the liquid layer is empty (contains only empty blocks). + */ + public function isLiquidLayerEmpty() : bool{ + return $this->liquidLayer->getBitsPerBlock() === 0 && $this->liquidLayer->get(0, 0, 0) === $this->emptyBlockId; + } + /** * Returns the block used as the default. This is assumed to refer to air. * If all the blocks in a subchunk layer are equal to this block, the layer is assumed to be empty. diff --git a/src/world/format/io/leveldb/LevelDB.php b/src/world/format/io/leveldb/LevelDB.php index 86cd8594d..a82d34c28 100644 --- a/src/world/format/io/leveldb/LevelDB.php +++ b/src/world/format/io/leveldb/LevelDB.php @@ -778,8 +778,8 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{ $blockLayer = $subChunk->getBlockLayer(); $liquidLayer = $subChunk->getLiquidLayer(); - $isBlockLayerEmpty = $blockLayer->getBitsPerBlock() === 0 && $blockLayer->get(0, 0, 0) === $subChunk->getEmptyBlockId(); - $isLiquidLayerEmpty = $liquidLayer->getBitsPerBlock() === 0 && $liquidLayer->get(0, 0, 0) === $subChunk->getEmptyBlockId(); + $isBlockLayerEmpty = $subChunk->isBlockLayerEmpty(); + $isLiquidLayerEmpty = $subChunk->isLiquidLayerEmpty(); $layerCount = 0; if(!$isBlockLayerEmpty) $layerCount++;