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.
This commit is contained in:
Dylan K. Taylor 2021-01-27 21:08:46 +00:00
parent bbae02264d
commit 5a1131d72d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 12 additions and 18 deletions

View File

@ -29,8 +29,6 @@ use pocketmine\utils\Random;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
class TallGrass extends Populator{ class TallGrass extends Populator{
/** @var ChunkManager */
private $world;
/** @var int */ /** @var int */
private $randomAmount = 1; private $randomAmount = 1;
/** @var int */ /** @var int */
@ -45,29 +43,28 @@ class TallGrass extends Populator{
} }
public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{ public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
$this->world = $world;
$amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount; $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
$block = VanillaBlocks::TALL_GRASS(); $block = VanillaBlocks::TALL_GRASS();
for($i = 0; $i < $amount; ++$i){ for($i = 0; $i < $amount; ++$i){
$x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15); $x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15);
$z = $random->nextRange($chunkZ * 16, $chunkZ * 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)){ if($y !== -1 and $this->canTallGrassStay($world, $x, $y, $z)){
$this->world->setBlockAt($x, $y, $z, $block); $world->setBlockAt($x, $y, $z, $block);
} }
} }
} }
private function canTallGrassStay(int $x, int $y, int $z) : bool{ private function canTallGrassStay(ChunkManager $world, int $x, int $y, int $z) : bool{
$b = $this->world->getBlockAt($x, $y, $z)->getId(); $b = $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; 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){ 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){ if($b !== BlockLegacyIds::AIR and $b !== BlockLegacyIds::LEAVES and $b !== BlockLegacyIds::LEAVES2 and $b !== BlockLegacyIds::SNOW_LAYER){
return $y + 1; return $y + 1;
} }

View File

@ -30,8 +30,6 @@ use pocketmine\world\ChunkManager;
use pocketmine\world\generator\object\Tree as ObjectTree; use pocketmine\world\generator\object\Tree as ObjectTree;
class Tree extends Populator{ class Tree extends Populator{
/** @var ChunkManager */
private $world;
/** @var int */ /** @var int */
private $randomAmount = 1; private $randomAmount = 1;
/** @var int */ /** @var int */
@ -56,22 +54,21 @@ class Tree extends Populator{
} }
public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{ public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
$this->world = $world;
$amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount; $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
for($i = 0; $i < $amount; ++$i){ for($i = 0; $i < $amount; ++$i){
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15); $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15); $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
$y = $this->getHighestWorkableBlock($x, $z); $y = $this->getHighestWorkableBlock($world, $x, $z);
if($y === -1){ if($y === -1){
continue; 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){ 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){ if($b === BlockLegacyIds::DIRT or $b === BlockLegacyIds::GRASS){
return $y + 1; return $y + 1;
}elseif($b !== BlockLegacyIds::AIR and $b !== BlockLegacyIds::SNOW_LAYER){ }elseif($b !== BlockLegacyIds::AIR and $b !== BlockLegacyIds::SNOW_LAYER){