From 5a1131d72d718383e53a7c654631dd4cd051fdeb Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 27 Jan 2021 21:08:46 +0000 Subject: [PATCH] Populator: require dependencies explicitly, don't make bad assumptions about fields this also leaks ChunkManagers on the worker threads because the generator context is long-lived. --- src/world/generator/populator/TallGrass.php | 19 ++++++++----------- src/world/generator/populator/Tree.php | 11 ++++------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/world/generator/populator/TallGrass.php b/src/world/generator/populator/TallGrass.php index 30a1d8135e..2c2667f8ca 100644 --- a/src/world/generator/populator/TallGrass.php +++ b/src/world/generator/populator/TallGrass.php @@ -29,8 +29,6 @@ use pocketmine\utils\Random; use pocketmine\world\ChunkManager; class TallGrass extends Populator{ - /** @var ChunkManager */ - private $world; /** @var int */ private $randomAmount = 1; /** @var int */ @@ -45,29 +43,28 @@ class TallGrass extends Populator{ } public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{ - $this->world = $world; $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount; $block = VanillaBlocks::TALL_GRASS(); for($i = 0; $i < $amount; ++$i){ $x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15); $z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15); - $y = $this->getHighestWorkableBlock($x, $z); + $y = $this->getHighestWorkableBlock($world, $x, $z); - if($y !== -1 and $this->canTallGrassStay($x, $y, $z)){ - $this->world->setBlockAt($x, $y, $z, $block); + if($y !== -1 and $this->canTallGrassStay($world, $x, $y, $z)){ + $world->setBlockAt($x, $y, $z, $block); } } } - private function canTallGrassStay(int $x, int $y, int $z) : bool{ - $b = $this->world->getBlockAt($x, $y, $z)->getId(); - return ($b === BlockLegacyIds::AIR or $b === BlockLegacyIds::SNOW_LAYER) and $this->world->getBlockAt($x, $y - 1, $z)->getId() === BlockLegacyIds::GRASS; + private function canTallGrassStay(ChunkManager $world, int $x, int $y, int $z) : bool{ + $b = $world->getBlockAt($x, $y, $z)->getId(); + return ($b === BlockLegacyIds::AIR or $b === BlockLegacyIds::SNOW_LAYER) and $world->getBlockAt($x, $y - 1, $z)->getId() === BlockLegacyIds::GRASS; } - private function getHighestWorkableBlock(int $x, int $z) : int{ + private function getHighestWorkableBlock(ChunkManager $world, int $x, int $z) : int{ for($y = 127; $y >= 0; --$y){ - $b = $this->world->getBlockAt($x, $y, $z)->getId(); + $b = $world->getBlockAt($x, $y, $z)->getId(); if($b !== BlockLegacyIds::AIR and $b !== BlockLegacyIds::LEAVES and $b !== BlockLegacyIds::LEAVES2 and $b !== BlockLegacyIds::SNOW_LAYER){ return $y + 1; } diff --git a/src/world/generator/populator/Tree.php b/src/world/generator/populator/Tree.php index 8680e0d51f..2e1df68d65 100644 --- a/src/world/generator/populator/Tree.php +++ b/src/world/generator/populator/Tree.php @@ -30,8 +30,6 @@ use pocketmine\world\ChunkManager; use pocketmine\world\generator\object\Tree as ObjectTree; class Tree extends Populator{ - /** @var ChunkManager */ - private $world; /** @var int */ private $randomAmount = 1; /** @var int */ @@ -56,22 +54,21 @@ class Tree extends Populator{ } public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{ - $this->world = $world; $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount; for($i = 0; $i < $amount; ++$i){ $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15); $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15); - $y = $this->getHighestWorkableBlock($x, $z); + $y = $this->getHighestWorkableBlock($world, $x, $z); if($y === -1){ continue; } - ObjectTree::growTree($this->world, $x, $y, $z, $random, $this->type); + ObjectTree::growTree($world, $x, $y, $z, $random, $this->type); } } - private function getHighestWorkableBlock(int $x, int $z) : int{ + private function getHighestWorkableBlock(ChunkManager $world, int $x, int $z) : int{ for($y = 127; $y >= 0; --$y){ - $b = $this->world->getBlockAt($x, $y, $z)->getId(); + $b = $world->getBlockAt($x, $y, $z)->getId(); if($b === BlockLegacyIds::DIRT or $b === BlockLegacyIds::GRASS){ return $y + 1; }elseif($b !== BlockLegacyIds::AIR and $b !== BlockLegacyIds::SNOW_LAYER){