Refactor Tree classes (#4407)

This commit is contained in:
Colin 2021-08-27 21:11:05 +02:00 committed by GitHub
parent ee16a00c57
commit 8f89c04c51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 36 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\TreeType; use pocketmine\block\utils\TreeType;
use pocketmine\event\block\StructureGrowEvent;
use pocketmine\item\Fertilizer; use pocketmine\item\Fertilizer;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Facing; use pocketmine\math\Facing;
@ -76,7 +77,7 @@ class Sapling extends Flowable{
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($item instanceof Fertilizer){ if($item instanceof Fertilizer){
Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType, true); $this->grow();
$item->pop(); $item->pop();
@ -97,9 +98,9 @@ class Sapling extends Flowable{
} }
public function onRandomTick() : void{ public function onRandomTick() : void{
if($this->position->getWorld()->getFullLightAt($this->position->x, $this->position->y, $this->position->z) >= 8 and mt_rand(1, 7) === 1){ if($this->position->getWorld()->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 and mt_rand(1, 7) === 1){
if($this->ready){ if($this->ready){
Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType, true); $this->grow();
}else{ }else{
$this->ready = true; $this->ready = true;
$this->position->getWorld()->setBlock($this->position, $this); $this->position->getWorld()->setBlock($this->position, $this);
@ -107,6 +108,23 @@ class Sapling extends Flowable{
} }
} }
private function grow() : void{
$random = new Random(mt_rand());
$tree = Tree::get($random, $this->treeType);
$transaction = $tree?->getBlockTransaction($this->position->getWorld(), $this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ(), $random);
if($transaction === null){
return;
}
$ev = new StructureGrowEvent($this, $transaction);
$ev->call();
if($ev->isCancelled()){
return;
}
$transaction->apply();
}
public function getFuelTime() : int{ public function getFuelTime() : int{
return 100; return 100;
} }

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\generator\object;
use pocketmine\block\VanillaBlocks; use pocketmine\block\VanillaBlocks;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use pocketmine\world\BlockTransaction;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
class BirchTree extends Tree{ class BirchTree extends Tree{
@ -36,11 +37,11 @@ class BirchTree extends Tree{
$this->superBirch = $superBirch; $this->superBirch = $superBirch;
} }
public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ public function getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random) : ?BlockTransaction{
$this->treeHeight = $random->nextBoundedInt(3) + 5; $this->treeHeight = $random->nextBoundedInt(3) + 5;
if($this->superBirch){ if($this->superBirch){
$this->treeHeight += 5; $this->treeHeight += 5;
} }
parent::placeObject($world, $x, $y, $z, $random, $callEvent); return parent::getBlockTransaction($world, $x, $y, $z, $random);
} }
} }

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\generator\object;
use pocketmine\block\VanillaBlocks; use pocketmine\block\VanillaBlocks;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use pocketmine\world\BlockTransaction;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
class OakTree extends Tree{ class OakTree extends Tree{
@ -33,8 +34,8 @@ class OakTree extends Tree{
parent::__construct(VanillaBlocks::OAK_LOG(), VanillaBlocks::OAK_LEAVES()); parent::__construct(VanillaBlocks::OAK_LOG(), VanillaBlocks::OAK_LEAVES());
} }
public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ public function getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random) : ?BlockTransaction{
$this->treeHeight = $random->nextBoundedInt(3) + 4; $this->treeHeight = $random->nextBoundedInt(3) + 4;
parent::placeObject($world, $x, $y, $z, $random, $callEvent); return parent::getBlockTransaction($world, $x, $y, $z, $random);
} }
} }

View File

@ -39,9 +39,9 @@ class SpruceTree extends Tree{
return $this->treeHeight - $random->nextBoundedInt(3); return $this->treeHeight - $random->nextBoundedInt(3);
} }
public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ public function getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random) : ?BlockTransaction{
$this->treeHeight = $random->nextBoundedInt(4) + 6; $this->treeHeight = $random->nextBoundedInt(4) + 6;
parent::placeObject($world, $x, $y, $z, $random, $callEvent); return parent::getBlockTransaction($world, $x, $y, $z, $random);
} }
protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{

View File

@ -28,7 +28,6 @@ use pocketmine\block\Leaves;
use pocketmine\block\Sapling; use pocketmine\block\Sapling;
use pocketmine\block\utils\TreeType; use pocketmine\block\utils\TreeType;
use pocketmine\block\VanillaBlocks; use pocketmine\block\VanillaBlocks;
use pocketmine\event\block\StructureGrowEvent;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use pocketmine\world\BlockTransaction; use pocketmine\world\BlockTransaction;
use pocketmine\world\ChunkManager; use pocketmine\world\ChunkManager;
@ -52,36 +51,28 @@ abstract class Tree{
/** /**
* @param TreeType|null $type default oak * @param TreeType|null $type default oak
* @param bool $callEvent set this parameter to true to allow the calling of the BlockSproutEvent when the tree grows
*
* @throws \InvalidArgumentException
*/ */
public static function growTree(ChunkManager $world, int $x, int $y, int $z, Random $random, ?TreeType $type = null, bool $callEvent = false) : void{ public static function get(Random $random, ?TreeType $type = null) : ?self {
/** @var null|Tree $tree */
$tree = null;
$type = $type ?? TreeType::OAK(); $type = $type ?? TreeType::OAK();
if($type->equals(TreeType::SPRUCE())){ if($type->equals(TreeType::SPRUCE())){
$tree = new SpruceTree(); return new SpruceTree();
}elseif($type->equals(TreeType::BIRCH())){ }elseif($type->equals(TreeType::BIRCH())){
if($random->nextBoundedInt(39) === 0){ if($random->nextBoundedInt(39) === 0){
$tree = new BirchTree(true); return new BirchTree(true);
}else{ }else{
$tree = new BirchTree(); return new BirchTree();
} }
}elseif($type->equals(TreeType::JUNGLE())){ }elseif($type->equals(TreeType::JUNGLE())){
$tree = new JungleTree(); return new JungleTree();
}elseif($type->equals(TreeType::OAK())){ //default }elseif($type->equals(TreeType::OAK())){ //default
$tree = new OakTree(); return new OakTree();
/*if($random->nextRange(0, 9) === 0){ /*if($random->nextRange(0, 9) === 0){
$tree = new BigTree(); $tree = new BigTree();
}else{*/ }else{*/
//} //}
} }
return null;
if($tree !== null and $tree->canPlaceObject($world, $x, $y, $z, $random)){
$tree->placeObject($world, $x, $y, $z, $random, $callEvent);
}
} }
public function canPlaceObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : bool{ public function canPlaceObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : bool{
@ -102,20 +93,20 @@ abstract class Tree{
return true; return true;
} }
public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ /**
* Returns the BlockTransaction containing all the blocks the tree would change upon growing at the given coordinates
* or null if the tree can't be grown
*/
public function getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random) : ?BlockTransaction{
if(!$this->canPlaceObject($world, $x, $y, $z, $random)){
return null;
}
$transaction = new BlockTransaction($world); $transaction = new BlockTransaction($world);
$this->placeTrunk($x, $y, $z, $random, $this->generateChunkHeight($random), $transaction); $this->placeTrunk($x, $y, $z, $random, $this->generateChunkHeight($random), $transaction);
$this->placeCanopy($x, $y, $z, $random, $transaction); $this->placeCanopy($x, $y, $z, $random, $transaction);
if($callEvent){ return $transaction;
$ev = new StructureGrowEvent($world->getBlockAt($x, $y, $z), $transaction);
$ev->call();
if($ev->isCancelled()){
return;
}
}
$transaction->apply(); //TODO: handle return value on failure
} }
protected function generateChunkHeight(Random $random) : int{ protected function generateChunkHeight(Random $random) : int{

View File

@ -62,7 +62,9 @@ class Tree extends Populator{
if($y === -1){ if($y === -1){
continue; continue;
} }
ObjectTree::growTree($world, $x, $y, $z, $random, $this->type); $tree = ObjectTree::get($random, $this->type);
$transaction = $tree?->getBlockTransaction($world, $x, $y, $z, $random);
$transaction?->apply();
} }
} }