Chunk: make all parameters of __construct() mandatory and non-nullable

having the constructor fill in defaults for these invariably causes bugs.
This commit is contained in:
Dylan K. Taylor
2021-10-25 20:22:50 +01:00
parent d53347454b
commit baba25953f
7 changed files with 16 additions and 6 deletions

View File

@ -68,7 +68,7 @@ class Chunk{
/**
* @param SubChunk[] $subChunks
*/
public function __construct(array $subChunks = [], ?BiomeArray $biomeIds = null, bool $terrainPopulated = false){
public function __construct(array $subChunks, BiomeArray $biomeIds, bool $terrainPopulated){
$this->subChunks = new \SplFixedArray(Chunk::MAX_SUBCHUNKS);
foreach($this->subChunks as $y => $null){
@ -77,7 +77,7 @@ class Chunk{
$val = ($this->subChunks->getSize() * SubChunk::EDGE_LENGTH);
$this->heightMap = HeightArray::fill($val); //TODO: what about lazily initializing this?
$this->biomeIds = $biomeIds ?? BiomeArray::fill(BiomeIds::OCEAN);
$this->biomeIds = $biomeIds;
$this->terrainPopulated = $terrainPopulated;
}

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\format\io\leveldb;
use pocketmine\block\Block;
use pocketmine\block\BlockLegacyIds;
use pocketmine\data\bedrock\BiomeIds;
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
@ -416,7 +417,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
$chunk = new Chunk(
$subChunks,
$biomeArray,
$biomeArray ?? BiomeArray::fill(BiomeIds::OCEAN), //TODO: maybe missing biomes should be an error?
$terrainPopulated
);

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\world\format\io\region;
use pocketmine\data\bedrock\BiomeIds;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\ByteArrayTag;
@ -86,6 +87,8 @@ trait LegacyAnvilChunkTrait{
$biomeArray = $makeBiomeArray(ChunkUtils::convertBiomeColors($biomeColorsTag->getValue())); //Convert back to original format
}elseif(($biomesTag = $chunk->getTag("Biomes")) instanceof ByteArrayTag){
$biomeArray = $makeBiomeArray($biomesTag->getValue());
}else{
$biomeArray = BiomeArray::fill(BiomeIds::OCEAN);
}
return new ChunkData(

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\format\io\region;
use pocketmine\block\Block;
use pocketmine\block\BlockLegacyIds;
use pocketmine\data\bedrock\BiomeIds;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\ByteArrayTag;
@ -80,6 +81,8 @@ class McRegion extends RegionWorldProvider{
$biomeIds = $makeBiomeArray(ChunkUtils::convertBiomeColors($biomeColorsTag->getValue())); //Convert back to original format
}elseif(($biomesTag = $chunk->getTag("Biomes")) instanceof ByteArrayTag){
$biomeIds = $makeBiomeArray($biomesTag->getValue());
}else{
$biomeIds = BiomeArray::fill(BiomeIds::OCEAN);
}
return new ChunkData(