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
This commit is contained in:
Dylan T. 2025-06-27 15:54:42 +00:00
parent 30d13508b4
commit 72781f4042
3 changed files with 21 additions and 7 deletions

View File

@ -112,13 +112,13 @@ final class ChunkSerializer{
} }
public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, PacketSerializer $stream, bool $persistentBlockStates) : void{ 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 = []; $layers = [];
if(($blockLayer = $subChunk->getBlockLayer()) !== null){ if(!$subChunk->isBlockLayerEmpty()){
$layers[] = $blockLayer; $layers[] = $subChunk->getBlockLayer();
} }
if(($liquidLayer = $subChunk->getLiquidLayer()) !== null){ if(!$subChunk->isLiquidLayerEmpty()){
$layers[] = $liquidLayer; $layers[] = $subChunk->getLiquidLayer();
} }
$stream->putByte(8); //version $stream->putByte(8); //version

View File

@ -76,6 +76,20 @@ class SubChunk{
$this->liquidLayer->getBitsPerBlock() === 0 && $this->liquidLayer->get(0, 0, 0) === $this->emptyBlockId; $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. * 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. * If all the blocks in a subchunk layer are equal to this block, the layer is assumed to be empty.

View File

@ -778,8 +778,8 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
$blockLayer = $subChunk->getBlockLayer(); $blockLayer = $subChunk->getBlockLayer();
$liquidLayer = $subChunk->getLiquidLayer(); $liquidLayer = $subChunk->getLiquidLayer();
$isBlockLayerEmpty = $blockLayer->getBitsPerBlock() === 0 && $blockLayer->get(0, 0, 0) === $subChunk->getEmptyBlockId(); $isBlockLayerEmpty = $subChunk->isBlockLayerEmpty();
$isLiquidLayerEmpty = $liquidLayer->getBitsPerBlock() === 0 && $liquidLayer->get(0, 0, 0) === $subChunk->getEmptyBlockId(); $isLiquidLayerEmpty = $subChunk->isLiquidLayerEmpty();
$layerCount = 0; $layerCount = 0;
if(!$isBlockLayerEmpty) $layerCount++; if(!$isBlockLayerEmpty) $layerCount++;