diff --git a/src/pocketmine/level/generator/object/BirchTree.php b/src/pocketmine/level/generator/object/BirchTree.php index 1dedf230d..be6046fb5 100644 --- a/src/pocketmine/level/generator/object/BirchTree.php +++ b/src/pocketmine/level/generator/object/BirchTree.php @@ -29,13 +29,11 @@ use pocketmine\level\ChunkManager; use pocketmine\utils\Random; class BirchTree extends Tree{ - + /** @var bool */ protected $superBirch = false; public function __construct(bool $superBirch = false){ - $this->trunkBlock = Block::LOG; - $this->leafBlock = Block::LEAVES; - $this->type = Wood::BIRCH; + parent::__construct(Block::LOG, Block::LEAVES, Wood::BIRCH); $this->superBirch = $superBirch; } diff --git a/src/pocketmine/level/generator/object/JungleTree.php b/src/pocketmine/level/generator/object/JungleTree.php index a237e5303..ae749f0df 100644 --- a/src/pocketmine/level/generator/object/JungleTree.php +++ b/src/pocketmine/level/generator/object/JungleTree.php @@ -29,9 +29,6 @@ use pocketmine\block\Wood; class JungleTree extends Tree{ public function __construct(){ - $this->trunkBlock = Block::LOG; - $this->leafBlock = Block::LEAVES; - $this->type = Wood::JUNGLE; - $this->treeHeight = 8; + parent::__construct(Block::LOG, Block::LEAVES, Wood::JUNGLE, 8); } } diff --git a/src/pocketmine/level/generator/object/OakTree.php b/src/pocketmine/level/generator/object/OakTree.php index 27d24326d..66e7f81d7 100644 --- a/src/pocketmine/level/generator/object/OakTree.php +++ b/src/pocketmine/level/generator/object/OakTree.php @@ -31,9 +31,7 @@ use pocketmine\utils\Random; class OakTree extends Tree{ public function __construct(){ - $this->trunkBlock = Block::LOG; - $this->leafBlock = Block::LEAVES; - $this->type = Wood::OAK; + parent::__construct(Block::LOG, Block::LEAVES, Wood::OAK); } public function placeObject(ChunkManager $level, int $x, int $y, int $z, Random $random) : void{ diff --git a/src/pocketmine/level/generator/object/SpruceTree.php b/src/pocketmine/level/generator/object/SpruceTree.php index 8201e20b4..2ff0304ac 100644 --- a/src/pocketmine/level/generator/object/SpruceTree.php +++ b/src/pocketmine/level/generator/object/SpruceTree.php @@ -32,10 +32,7 @@ use pocketmine\utils\Random; class SpruceTree extends Tree{ public function __construct(){ - $this->trunkBlock = Block::LOG; - $this->leafBlock = Block::LEAVES; - $this->type = Wood::SPRUCE; - $this->treeHeight = 10; + parent::__construct(Block::LOG, Block::LEAVES, Wood::SPRUCE, 10); } public function placeObject(ChunkManager $level, int $x, int $y, int $z, Random $random) : void{ @@ -63,7 +60,7 @@ class SpruceTree extends Tree{ if(!BlockFactory::get($level->getBlockIdAt($xx, $yyy, $zz))->isSolid()){ $level->setBlockIdAt($xx, $yyy, $zz, $this->leafBlock); - $level->setBlockDataAt($xx, $yyy, $zz, $this->type); + $level->setBlockDataAt($xx, $yyy, $zz, $this->blockMeta); } } } diff --git a/src/pocketmine/level/generator/object/Tree.php b/src/pocketmine/level/generator/object/Tree.php index 5b9110f9e..5d3367bdb 100644 --- a/src/pocketmine/level/generator/object/Tree.php +++ b/src/pocketmine/level/generator/object/Tree.php @@ -25,25 +25,30 @@ namespace pocketmine\level\generator\object; use pocketmine\block\Block; use pocketmine\block\BlockFactory; +use pocketmine\block\Leaves; +use pocketmine\block\Sapling; use pocketmine\block\utils\WoodType; +use pocketmine\block\Wood; use pocketmine\level\ChunkManager; use pocketmine\utils\Random; abstract class Tree{ - public $overridable = [ - Block::AIR => true, - Block::SAPLING => true, - Block::LOG => true, - Block::LEAVES => true, - Block::SNOW_LAYER => true, - Block::LOG2 => true, - Block::LEAVES2 => true - ]; - public $type = 0; - public $trunkBlock = Block::LOG; - public $leafBlock = Block::LEAVES; - public $treeHeight = 7; + /** @var int */ + protected $blockMeta; + /** @var int */ + protected $trunkBlock; + /** @var int */ + protected $leafBlock; + /** @var int */ + protected $treeHeight; + + public function __construct(int $trunkBlock, int $leafBlock, int $blockMeta, int $treeHeight = 7){ + $this->trunkBlock = $trunkBlock; + $this->leafBlock = $leafBlock; + $this->blockMeta = $blockMeta; + $this->treeHeight = $treeHeight; + } public static function growTree(ChunkManager $level, int $x, int $y, int $z, Random $random, int $type = WoodType::OAK) : void{ switch($type){ @@ -86,7 +91,7 @@ abstract class Tree{ } for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){ for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){ - if(!isset($this->overridable[$level->getBlockIdAt($x + $xx, $y + $yy, $z + $zz)])){ + if(!$this->canOverride(BlockFactory::get($level->getBlockIdAt($x + $xx, $y + $yy, $z + $zz)))){ return false; } } @@ -111,7 +116,7 @@ abstract class Tree{ } if(!BlockFactory::get($level->getBlockIdAt($xx, $yy, $zz))->isSolid()){ $level->setBlockIdAt($xx, $yy, $zz, $this->leafBlock); - $level->setBlockDataAt($xx, $yy, $zz, $this->type); + $level->setBlockDataAt($xx, $yy, $zz, $this->blockMeta); } } } @@ -124,10 +129,14 @@ abstract class Tree{ for($yy = 0; $yy < $trunkHeight; ++$yy){ $blockId = $level->getBlockIdAt($x, $y + $yy, $z); - if(isset($this->overridable[$blockId])){ + if($this->canOverride(BlockFactory::get($blockId))){ $level->setBlockIdAt($x, $y + $yy, $z, $this->trunkBlock); - $level->setBlockDataAt($x, $y + $yy, $z, $this->type); + $level->setBlockDataAt($x, $y + $yy, $z, $this->blockMeta); } } } + + protected function canOverride(Block $block) : bool{ + return $block->canBeReplaced() or $block instanceof Wood or $block instanceof Sapling or $block instanceof Leaves; + } }