Fix some of the implicit immutability issues of EmptySubChunk

it's useful to have an immutable stub around for the sake of feeding back dummy read values, but for write values it has to barf instead of being quiet.
There's still some issues with LightArray which I don't currently have a solution for, but I'm thinking about separating light storage from chunks anyway.
This commit is contained in:
Dylan K. Taylor 2020-09-07 14:43:26 +01:00
parent 5fbc842f7a
commit 01f8116cdd
6 changed files with 22 additions and 28 deletions

View File

@ -152,7 +152,7 @@ class Chunk{
* Sets the blockstate at the given coordinate by internal ID. * Sets the blockstate at the given coordinate by internal ID.
*/ */
public function setFullBlock(int $x, int $y, int $z, int $block) : void{ public function setFullBlock(int $x, int $y, int $z, int $block) : void{
$this->getSubChunk($y >> 4)->setFullBlock($x, $y & 0xf, $z, $block); $this->getWritableSubChunk($y >> 4)->setFullBlock($x, $y & 0xf, $z, $block);
$this->dirtyFlags |= self::DIRTY_FLAG_TERRAIN; $this->dirtyFlags |= self::DIRTY_FLAG_TERRAIN;
} }
@ -178,12 +178,12 @@ class Chunk{
* @param int $level 0-15 * @param int $level 0-15
*/ */
public function setBlockSkyLight(int $x, int $y, int $z, int $level) : void{ public function setBlockSkyLight(int $x, int $y, int $z, int $level) : void{
$this->getSubChunk($y >> 4)->getBlockSkyLightArray()->set($x & 0xf, $y & 0x0f, $z & 0xf, $level); $this->getWritableSubChunk($y >> 4)->getBlockSkyLightArray()->set($x & 0xf, $y & 0x0f, $z & 0xf, $level);
} }
public function setAllBlockSkyLight(int $level) : void{ public function setAllBlockSkyLight(int $level) : void{
for($y = $this->subChunks->count() - 1; $y >= 0; --$y){ for($y = $this->subChunks->count() - 1; $y >= 0; --$y){
$this->getSubChunk($y)->setBlockSkyLightArray(LightArray::fill($level)); $this->getWritableSubChunk($y)->setBlockSkyLightArray(LightArray::fill($level));
} }
} }
@ -209,12 +209,12 @@ class Chunk{
* @param int $level 0-15 * @param int $level 0-15
*/ */
public function setBlockLight(int $x, int $y, int $z, int $level) : void{ public function setBlockLight(int $x, int $y, int $z, int $level) : void{
$this->getSubChunk($y >> 4)->getBlockLightArray()->set($x & 0xf, $y & 0x0f, $z & 0xf, $level); $this->getWritableSubChunk($y >> 4)->getBlockLightArray()->set($x & 0xf, $y & 0x0f, $z & 0xf, $level);
} }
public function setAllBlockLight(int $level) : void{ public function setAllBlockLight(int $level) : void{
for($y = $this->subChunks->count() - 1; $y >= 0; --$y){ for($y = $this->subChunks->count() - 1; $y >= 0; --$y){
$this->getSubChunk($y)->setBlockLightArray(LightArray::fill($level)); $this->getWritableSubChunk($y)->setBlockLightArray(LightArray::fill($level));
} }
} }
@ -340,10 +340,10 @@ class Chunk{
$highestHeightMap = max($this->heightMap->getValues()); $highestHeightMap = max($this->heightMap->getValues());
$lowestFullyLitSubChunk = ($highestHeightMap >> 4) + (($highestHeightMap & 0xf) !== 0 ? 1 : 0); $lowestFullyLitSubChunk = ($highestHeightMap >> 4) + (($highestHeightMap & 0xf) !== 0 ? 1 : 0);
for($y = 0; $y < $lowestFullyLitSubChunk; $y++){ for($y = 0; $y < $lowestFullyLitSubChunk; $y++){
$this->getSubChunk($y)->setBlockSkyLightArray(LightArray::fill(0)); $this->getWritableSubChunk($y)->setBlockSkyLightArray(LightArray::fill(0));
} }
for($y = $lowestFullyLitSubChunk, $yMax = $this->subChunks->count(); $y < $yMax; $y++){ for($y = $lowestFullyLitSubChunk, $yMax = $this->subChunks->count(); $y < $yMax; $y++){
$this->getSubChunk($y)->setBlockSkyLightArray(LightArray::fill(15)); $this->getWritableSubChunk($y)->setBlockSkyLightArray(LightArray::fill(15));
} }
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
@ -623,6 +623,13 @@ class Chunk{
return $this->subChunks[$y]; return $this->subChunks[$y];
} }
public function getWritableSubChunk(int $y) : SubChunk{
if($y < 0 || $y >= $this->subChunks->getSize()){
throw new \InvalidArgumentException("Cannot get subchunk $y for writing");
}
return $this->subChunks[$y];
}
/** /**
* Sets a subchunk in the chunk index * Sets a subchunk in the chunk index
*/ */

View File

@ -47,10 +47,6 @@ class EmptySubChunk implements SubChunkInterface{
return 0; return 0;
} }
public function setFullBlock(int $x, int $y, int $z, int $block) : void{
}
public function getBlockLayers() : array{ public function getBlockLayers() : array{
return []; return [];
} }
@ -63,15 +59,7 @@ class EmptySubChunk implements SubChunkInterface{
return new LightArray(LightArray::ZERO); return new LightArray(LightArray::ZERO);
} }
public function setBlockLightArray(LightArray $data) : void{
}
public function getBlockSkyLightArray() : LightArray{ public function getBlockSkyLightArray() : LightArray{
return new LightArray(LightArray::FIFTEEN); return new LightArray(LightArray::FIFTEEN);
} }
public function setBlockSkyLightArray(LightArray $data) : void{
}
} }

View File

@ -40,8 +40,6 @@ interface SubChunkInterface{
public function getFullBlock(int $x, int $y, int $z) : int; public function getFullBlock(int $x, int $y, int $z) : int;
public function setFullBlock(int $x, int $y, int $z, int $block) : void;
/** /**
* @return PalettedBlockArray[] * @return PalettedBlockArray[]
*/ */
@ -51,9 +49,5 @@ interface SubChunkInterface{
public function getBlockSkyLightArray() : LightArray; public function getBlockSkyLightArray() : LightArray;
public function setBlockSkyLightArray(LightArray $data) : void;
public function getBlockLightArray() : LightArray; public function getBlockLightArray() : LightArray;
public function setBlockLightArray(LightArray $data) : void;
} }

View File

@ -156,7 +156,7 @@ class Flat extends Generator{
$count = count($this->structure); $count = count($this->structure);
for($sy = 0; $sy < $count; $sy += 16){ for($sy = 0; $sy < $count; $sy += 16){
$subchunk = $this->chunk->getSubChunk($sy >> 4); $subchunk = $this->chunk->getWritableSubChunk($sy >> 4);
for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){ for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){
$id = $this->structure[$y | $sy]; $id = $this->structure[$y | $sy];

View File

@ -94,10 +94,10 @@ class LightPopulationTask extends AsyncTask{
$blockLightArrays = igbinary_unserialize($this->resultBlockLightArrays); $blockLightArrays = igbinary_unserialize($this->resultBlockLightArrays);
foreach($skyLightArrays as $y => $array){ foreach($skyLightArrays as $y => $array){
$chunk->getSubChunk($y)->setBlockSkyLightArray($array); $chunk->getWritableSubChunk($y)->setBlockSkyLightArray($array);
} }
foreach($blockLightArrays as $y => $array){ foreach($blockLightArrays as $y => $array){
$chunk->getSubChunk($y)->setBlockLightArray($array); $chunk->getWritableSubChunk($y)->setBlockLightArray($array);
} }
$chunk->setLightPopulated(); $chunk->setLightPopulated();
} }

View File

@ -760,6 +760,11 @@ parameters:
count: 1 count: 1
path: ../../../src/world/format/Chunk.php path: ../../../src/world/format/Chunk.php
-
message: "#^Method pocketmine\\\\world\\\\format\\\\Chunk\\:\\:getWritableSubChunk\\(\\) should return pocketmine\\\\world\\\\format\\\\SubChunk but returns pocketmine\\\\world\\\\format\\\\SubChunk\\|null\\.$#"
count: 1
path: ../../../src/world/format/Chunk.php
- -
message: "#^Method pocketmine\\\\world\\\\format\\\\HeightArray\\:\\:get\\(\\) should return int but returns int\\|null\\.$#" message: "#^Method pocketmine\\\\world\\\\format\\\\HeightArray\\:\\:get\\(\\) should return int but returns int\\|null\\.$#"
count: 1 count: 1