diff --git a/src/world/ChunkManager.php b/src/world/ChunkManager.php index 97ef38a30..50ec60b23 100644 --- a/src/world/ChunkManager.php +++ b/src/world/ChunkManager.php @@ -40,7 +40,7 @@ interface ChunkManager{ */ public function setBlockAt(int $x, int $y, int $z, Block $block) : void; - public function getChunk(int $chunkX, int $chunkZ, bool $create = false) : ?Chunk; + public function getChunk(int $chunkX, int $chunkZ) : ?Chunk; public function setChunk(int $chunkX, int $chunkZ, ?Chunk $chunk) : void; diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 41e24ed6d..cca2373eb 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -124,7 +124,7 @@ class Explosion{ $pointerY += $shiftY; $pointerZ += $shiftZ; - if($this->subChunkExplorer->moveTo($vBlockX, $vBlockY, $vBlockZ, false) === SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($vBlockX, $vBlockY, $vBlockZ) === SubChunkExplorerStatus::INVALID){ continue; } diff --git a/src/world/SimpleChunkManager.php b/src/world/SimpleChunkManager.php index 5540ad51b..7911158b0 100644 --- a/src/world/SimpleChunkManager.php +++ b/src/world/SimpleChunkManager.php @@ -51,14 +51,14 @@ class SimpleChunkManager implements ChunkManager{ } public function getBlockAt(int $x, int $y, int $z) : Block{ - if($this->terrainPointer->moveTo($x, $y, $z, false) !== SubChunkExplorerStatus::INVALID){ + if($this->terrainPointer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ return BlockFactory::getInstance()->fromFullBlock($this->terrainPointer->currentSubChunk->getFullBlock($x & 0xf, $y & 0xf, $z & 0xf)); } return VanillaBlocks::AIR(); } public function setBlockAt(int $x, int $y, int $z, Block $block) : void{ - if($this->terrainPointer->moveTo($x, $y, $z, true) !== SubChunkExplorerStatus::INVALID){ + if($this->terrainPointer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ $this->terrainPointer->currentSubChunk->setFullBlock($x & 0xf, $y & 0xf, $z & 0xf, $block->getFullId()); $this->terrainPointer->currentChunk->setDirtyFlag(Chunk::DIRTY_FLAG_TERRAIN, true); }else{ @@ -66,9 +66,8 @@ class SimpleChunkManager implements ChunkManager{ } } - public function getChunk(int $chunkX, int $chunkZ, bool $create = false) : ?Chunk{ - $hash = World::chunkHash($chunkX, $chunkZ); - return $this->chunks[$hash] ?? ($create ? $this->chunks[$hash] = new Chunk($chunkX, $chunkZ) : null); + public function getChunk(int $chunkX, int $chunkZ) : ?Chunk{ + return $this->chunks[World::chunkHash($chunkX, $chunkZ)] ?? null; } public function setChunk(int $chunkX, int $chunkZ, ?Chunk $chunk) : void{ diff --git a/src/world/World.php b/src/world/World.php index a2b6a7fea..8f2af6cdf 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1839,7 +1839,7 @@ class World implements ChunkManager{ * @return int 0-15 */ public function getPotentialBlockSkyLightAt(int $x, int $y, int $z) : int{ - if(($chunk = $this->getChunk($x >> 4, $z >> 4, false)) !== null){ + if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){ return $chunk->getSubChunk($y >> 4)->getBlockSkyLightArray()->get($x & 0x0f, $y & 0xf, $z & 0x0f); } return 0; //TODO: this should probably throw instead (light not calculated yet) @@ -1851,7 +1851,7 @@ class World implements ChunkManager{ * @return int 0-15 */ public function getBlockLightAt(int $x, int $y, int $z) : int{ - if(($chunk = $this->getChunk($x >> 4, $z >> 4, false)) !== null){ + if(($chunk = $this->getChunk($x >> 4, $z >> 4)) !== null){ return $chunk->getSubChunk($y >> 4)->getBlockLightArray()->get($x & 0x0f, $y & 0xf, $z & 0x0f); } return 0; //TODO: this should probably throw instead (light not calculated yet) @@ -1895,12 +1895,8 @@ class World implements ChunkManager{ return null; } - public function getChunk(int $chunkX, int $chunkZ, bool $create = false) : ?Chunk{ - $hash = World::chunkHash($chunkX, $chunkZ); - if(isset($this->chunks[$hash])){ - return $this->chunks[$hash]; - } - return $create ? ($this->chunks[$hash] = new Chunk($chunkX, $chunkZ)) : null; + public function getChunk(int $chunkX, int $chunkZ) : ?Chunk{ + return $this->chunks[World::chunkHash($chunkX, $chunkZ)] ?? null; } /** diff --git a/src/world/generator/GeneratorChunkManager.php b/src/world/generator/GeneratorChunkManager.php deleted file mode 100644 index 312bf182e..000000000 --- a/src/world/generator/GeneratorChunkManager.php +++ /dev/null @@ -1,38 +0,0 @@ -chunks[World::chunkHash($chunkX, $chunkZ)])){ - throw new \InvalidArgumentException("Chunk does not exist"); - } - return parent::getChunk($chunkX, $chunkZ, $create); - } -} diff --git a/src/world/generator/GeneratorRegisterTask.php b/src/world/generator/GeneratorRegisterTask.php index 25f4777d6..06060ae0b 100644 --- a/src/world/generator/GeneratorRegisterTask.php +++ b/src/world/generator/GeneratorRegisterTask.php @@ -25,6 +25,7 @@ namespace pocketmine\world\generator; use pocketmine\scheduler\AsyncTask; use pocketmine\world\biome\Biome; +use pocketmine\world\SimpleChunkManager; use pocketmine\world\World; use function igbinary_serialize; use function igbinary_unserialize; @@ -56,7 +57,7 @@ class GeneratorRegisterTask extends AsyncTask{ public function onRun() : void{ Biome::init(); - $manager = new GeneratorChunkManager($this->worldHeight); + $manager = new SimpleChunkManager($this->worldHeight); $this->worker->saveToThreadStore("generation.world{$this->worldId}.manager", $manager); /** diff --git a/src/world/generator/PopulationTask.php b/src/world/generator/PopulationTask.php index 2ead5ece6..dd1f152ca 100644 --- a/src/world/generator/PopulationTask.php +++ b/src/world/generator/PopulationTask.php @@ -26,6 +26,7 @@ namespace pocketmine\world\generator; use pocketmine\scheduler\AsyncTask; use pocketmine\world\format\Chunk; use pocketmine\world\format\io\FastChunkSerializer; +use pocketmine\world\SimpleChunkManager; use pocketmine\world\World; class PopulationTask extends AsyncTask{ @@ -73,7 +74,7 @@ class PopulationTask extends AsyncTask{ public function onRun() : void{ $manager = $this->worker->getFromThreadStore("generation.world{$this->worldId}.manager"); $generator = $this->worker->getFromThreadStore("generation.world{$this->worldId}.generator"); - if(!($manager instanceof GeneratorChunkManager) or !($generator instanceof Generator)){ + if(!($manager instanceof SimpleChunkManager) or !($generator instanceof Generator)){ $this->state = false; return; } diff --git a/src/world/light/BlockLightUpdate.php b/src/world/light/BlockLightUpdate.php index d97b87d53..ed1bf5d27 100644 --- a/src/world/light/BlockLightUpdate.php +++ b/src/world/light/BlockLightUpdate.php @@ -53,14 +53,14 @@ class BlockLightUpdate extends LightUpdate{ } public function recalculateNode(int $x, int $y, int $z) : void{ - if($this->subChunkExplorer->moveTo($x, $y, $z, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ $block = $this->subChunkExplorer->currentSubChunk->getFullBlock($x & 0xf, $y & 0xf, $z & 0xf); $this->setAndUpdateLight($x, $y, $z, max($this->lightEmitters[$block], $this->getHighestAdjacentLight($x, $y, $z) - $this->lightFilters[$block])); } } public function recalculateChunk(int $chunkX, int $chunkZ) : int{ - if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ, false) === SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ) === SubChunkExplorerStatus::INVALID){ throw new \InvalidArgumentException("Chunk $chunkX $chunkZ does not exist"); } $chunk = $this->subChunkExplorer->currentChunk; diff --git a/src/world/light/LightUpdate.php b/src/world/light/LightUpdate.php index 8fac2e378..bc98e5b53 100644 --- a/src/world/light/LightUpdate.php +++ b/src/world/light/LightUpdate.php @@ -68,7 +68,7 @@ abstract class LightUpdate{ abstract public function recalculateChunk(int $chunkX, int $chunkZ) : int; protected function getEffectiveLight(int $x, int $y, int $z) : int{ - if($this->subChunkExplorer->moveTo($x, $y, $z, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ return $this->getCurrentLightArray()->get($x & 0xf, $y & 0xf, $z & 0xf); } return 0; @@ -98,7 +98,7 @@ abstract class LightUpdate{ private function prepareNodes() : LightPropagationContext{ $context = new LightPropagationContext(); foreach($this->updateNodes as $blockHash => [$x, $y, $z, $newLevel]){ - if($this->subChunkExplorer->moveTo($x, $y, $z, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ $lightArray = $this->getCurrentLightArray(); $oldLevel = $lightArray->get($x & 0xf, $y & 0xf, $z & 0xf); @@ -135,7 +135,7 @@ abstract class LightUpdate{ ]; foreach($points as [$cx, $cy, $cz]){ - if($this->subChunkExplorer->moveTo($cx, $cy, $cz, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($cx, $cy, $cz) !== SubChunkExplorerStatus::INVALID){ $this->computeRemoveLight($cx, $cy, $cz, $oldAdjacentLight, $context); }elseif($this->getEffectiveLight($cx, $cy, $cz) > 0 and !isset($context->spreadVisited[$index = World::blockHash($cx, $cy, $cz)])){ $context->spreadVisited[$index] = true; @@ -165,7 +165,7 @@ abstract class LightUpdate{ ]; foreach($points as [$cx, $cy, $cz]){ - if($this->subChunkExplorer->moveTo($cx, $cy, $cz, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($cx, $cy, $cz) !== SubChunkExplorerStatus::INVALID){ $this->computeSpreadLight($cx, $cy, $cz, $newAdjacentLight, $context); } } diff --git a/src/world/light/SkyLightUpdate.php b/src/world/light/SkyLightUpdate.php index 9b7674646..4408a97e5 100644 --- a/src/world/light/SkyLightUpdate.php +++ b/src/world/light/SkyLightUpdate.php @@ -61,7 +61,7 @@ class SkyLightUpdate extends LightUpdate{ } public function recalculateNode(int $x, int $y, int $z) : void{ - if($this->subChunkExplorer->moveTo($x, $y, $z, false) === SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($x, $y, $z) === SubChunkExplorerStatus::INVALID){ return; } $chunk = $this->subChunkExplorer->currentChunk; @@ -98,7 +98,7 @@ class SkyLightUpdate extends LightUpdate{ } public function recalculateChunk(int $chunkX, int $chunkZ) : int{ - if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ, false) === SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ) === SubChunkExplorerStatus::INVALID){ throw new \InvalidArgumentException("Chunk $chunkX $chunkZ does not exist"); } $chunk = $this->subChunkExplorer->currentChunk; @@ -151,7 +151,7 @@ class SkyLightUpdate extends LightUpdate{ $lightSources++; } for($y = $nodeColumnEnd + 1, $yMax = $lowestClearSubChunk * 16; $y < $yMax; $y++){ - if($this->subChunkExplorer->moveTo($x + $baseX, $y, $z + $baseZ, false) !== SubChunkExplorerStatus::INVALID){ + if($this->subChunkExplorer->moveTo($x + $baseX, $y, $z + $baseZ) !== SubChunkExplorerStatus::INVALID){ $this->getCurrentLightArray()->set($x, $y & 0xf, $z, 15); } } diff --git a/src/world/utils/SubChunkExplorer.php b/src/world/utils/SubChunkExplorer.php index 18f9b3b7d..0905473e3 100644 --- a/src/world/utils/SubChunkExplorer.php +++ b/src/world/utils/SubChunkExplorer.php @@ -58,13 +58,13 @@ class SubChunkExplorer{ /** * @phpstan-return SubChunkExplorerStatus::* */ - public function moveTo(int $x, int $y, int $z, bool $create) : int{ + public function moveTo(int $x, int $y, int $z) : int{ if($this->currentChunk === null or $this->currentX !== ($x >> 4) or $this->currentZ !== ($z >> 4)){ $this->currentX = $x >> 4; $this->currentZ = $z >> 4; $this->currentSubChunk = null; - $this->currentChunk = $this->world->getChunk($this->currentX, $this->currentZ, $create); + $this->currentChunk = $this->world->getChunk($this->currentX, $this->currentZ); if($this->currentChunk === null){ return SubChunkExplorerStatus::INVALID; } @@ -93,9 +93,9 @@ class SubChunkExplorer{ /** * @phpstan-return SubChunkExplorerStatus::* */ - public function moveToChunk(int $chunkX, int $chunkY, int $chunkZ, bool $create) : int{ + public function moveToChunk(int $chunkX, int $chunkY, int $chunkZ) : int{ //this is a cold path, so we don't care much if it's a bit slower (extra fcall overhead) - return $this->moveTo($chunkX << 4, $chunkY << 4, $chunkZ << 4, $create); + return $this->moveTo($chunkX << 4, $chunkY << 4, $chunkZ << 4); } /**