diff --git a/src/pocketmine/level/format/Chunk.php b/src/pocketmine/level/format/Chunk.php index c96168e0e..2aa8c0740 100644 --- a/src/pocketmine/level/format/Chunk.php +++ b/src/pocketmine/level/format/Chunk.php @@ -53,7 +53,7 @@ class Chunk{ protected $height = Chunk::MAX_SUBCHUNKS; - /** @var SubChunkInterface[] */ + /** @var \SplFixedArray|SubChunkInterface[] */ protected $subChunks = []; /** @var EmptySubChunk */ @@ -97,23 +97,11 @@ class Chunk{ $this->height = Chunk::MAX_SUBCHUNKS; //TODO: add a way of changing this + $this->subChunks = new \SplFixedArray($this->height); $this->emptySubChunk = new EmptySubChunk(); - foreach($subChunks as $y => $subChunk){ - if($y < 0 or $y >= $this->height){ - throw new ChunkException("Invalid subchunk index $y!"); - } - if($subChunk->isEmpty()){ - $this->subChunks[$y] = $this->emptySubChunk; - }else{ - $this->subChunks[$y] = $subChunk; - } - } - - for($i = 0; $i < $this->height; ++$i){ - if(!isset($this->subChunks[$i])){ - $this->subChunks[$i] = $this->emptySubChunk; - } + foreach($this->subChunks as $y => $null){ + $this->subChunks[$y] = $subChunks[$y] ?? $this->emptySubChunk; } if(count($heightMap) === 256){ @@ -843,7 +831,7 @@ class Chunk{ }elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){ $this->subChunks[$y] = new SubChunk(); } - assert($this->subChunks[$y] !== null, "Somehow something broke, no such subchunk at index $y"); + return $this->subChunks[$y]; } @@ -870,9 +858,9 @@ class Chunk{ } /** - * @return SubChunk[] + * @return \SplFixedArray|SubChunkInterface[] */ - public function getSubChunks() : array{ + public function getSubChunks() : \SplFixedArray{ return $this->subChunks; } @@ -882,8 +870,8 @@ class Chunk{ * @return int */ public function getHighestSubChunkIndex() : int{ - for($y = count($this->subChunks) - 1; $y >= 0; --$y){ - if($this->subChunks[$y] === null or $this->subChunks[$y] instanceof EmptySubChunk){ + for($y = $this->subChunks->count() - 1; $y >= 0; --$y){ + if($this->subChunks[$y] instanceof EmptySubChunk){ //No need to thoroughly prune empties at runtime, this will just reduce performance. continue; } @@ -907,10 +895,7 @@ class Chunk{ */ public function pruneEmptySubChunks(){ foreach($this->subChunks as $y => $subChunk){ - if($y < 0 or $y >= $this->height){ - assert(false, "Invalid subchunk index"); - unset($this->subChunks[$y]); - }elseif($subChunk instanceof EmptySubChunk){ + if($subChunk instanceof EmptySubChunk){ continue; }elseif($subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub $this->subChunks[$y] = $this->emptySubChunk;