mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-07 12:18:46 +00:00
Reuse empty chunk objects, stop creating new ones
This commit is contained in:
parent
69061ba4ad
commit
40d1394e3c
@ -59,6 +59,9 @@ class GenericChunk implements Chunk{
|
|||||||
/** @var SubChunk[] */
|
/** @var SubChunk[] */
|
||||||
protected $subChunks = [];
|
protected $subChunks = [];
|
||||||
|
|
||||||
|
/** @var EmptySubChunk */
|
||||||
|
protected $emptySubChunk = null;
|
||||||
|
|
||||||
/** @var Tile[] */
|
/** @var Tile[] */
|
||||||
protected $tiles = [];
|
protected $tiles = [];
|
||||||
protected $tileList = [];
|
protected $tileList = [];
|
||||||
@ -97,12 +100,14 @@ class GenericChunk implements Chunk{
|
|||||||
|
|
||||||
$this->height = $provider !== null ? ($provider->getWorldHeight() >> 4) : 16;
|
$this->height = $provider !== null ? ($provider->getWorldHeight() >> 4) : 16;
|
||||||
|
|
||||||
|
$this->emptySubChunk = new EmptySubChunk();
|
||||||
|
|
||||||
foreach($subChunks as $y => $subChunk){
|
foreach($subChunks as $y => $subChunk){
|
||||||
if($y < 0 or $y >= $this->height){
|
if($y < 0 or $y >= $this->height){
|
||||||
throw new ChunkException("Invalid subchunk index $y!");
|
throw new ChunkException("Invalid subchunk index $y!");
|
||||||
}
|
}
|
||||||
if($subChunk->isEmpty()){
|
if($subChunk->isEmpty()){
|
||||||
$this->subChunks[$y] = new EmptySubChunk(); //TODO: point to a single object instead of creating many for each chunk
|
$this->subChunks[$y] = $this->emptySubChunk;
|
||||||
}else{
|
}else{
|
||||||
$this->subChunks[$y] = $subChunk;
|
$this->subChunks[$y] = $subChunk;
|
||||||
}
|
}
|
||||||
@ -110,7 +115,7 @@ class GenericChunk implements Chunk{
|
|||||||
|
|
||||||
for($i = 0; $i < $this->height; ++$i){
|
for($i = 0; $i < $this->height; ++$i){
|
||||||
if(!isset($this->subChunks[$i])){
|
if(!isset($this->subChunks[$i])){
|
||||||
$this->subChunks[$i] = new EmptySubChunk(); //TODO: todo above
|
$this->subChunks[$i] = $this->emptySubChunk;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,21 +529,22 @@ class GenericChunk implements Chunk{
|
|||||||
|
|
||||||
public function getSubChunk(int $y, bool $generateNew = false) : SubChunk{
|
public function getSubChunk(int $y, bool $generateNew = false) : SubChunk{
|
||||||
if($y < 0 or $y >= $this->height){
|
if($y < 0 or $y >= $this->height){
|
||||||
return new EmptySubChunk();
|
return $this->emptySubChunk;
|
||||||
}elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){
|
}elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){
|
||||||
$this->subChunks[$y] = new SubChunk();
|
$this->subChunks[$y] = new SubChunk();
|
||||||
}
|
}
|
||||||
|
assert($this->subChunks[$y] !== null, "Somehow something broke, no such subchunk at index $y");
|
||||||
return $this->subChunks[$y];
|
return $this->subChunks[$y];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setSubChunk(int $fY, SubChunk $subChunk = null, bool $allowEmpty = false) : bool{
|
public function setSubChunk(int $y, SubChunk $subChunk = null, bool $allowEmpty = false) : bool{
|
||||||
if($fY < 0 or $fY >= $this->height){
|
if($y < 0 or $y >= $this->height){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if($subChunk === null or ($subChunk->isEmpty() and !$allowEmpty)){
|
if($subChunk === null or ($subChunk->isEmpty() and !$allowEmpty)){
|
||||||
$this->subChunks[$fY] = new EmptySubChunk($fY);
|
$this->subChunks[$y] = $this->emptySubChunk;
|
||||||
}else{
|
}else{
|
||||||
$this->subChunks[$fY] = $subChunk;
|
$this->subChunks[$y] = $subChunk;
|
||||||
}
|
}
|
||||||
$this->hasChanged = true;
|
$this->hasChanged = true;
|
||||||
return true;
|
return true;
|
||||||
@ -572,7 +578,7 @@ class GenericChunk implements Chunk{
|
|||||||
}elseif($subChunk instanceof EmptySubChunk){
|
}elseif($subChunk instanceof EmptySubChunk){
|
||||||
continue;
|
continue;
|
||||||
}elseif($subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub
|
}elseif($subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub
|
||||||
$this->subChunks[$y] = new EmptySubChunk($y);
|
$this->subChunks[$y] = $this->emptySubChunk;
|
||||||
}else{
|
}else{
|
||||||
continue; //do not set changed
|
continue; //do not set changed
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user