diff --git a/src/block/Bamboo.php b/src/block/Bamboo.php index 7e1e7f1da..fc5182cfb 100644 --- a/src/block/Bamboo.php +++ b/src/block/Bamboo.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\utils\BlockDataSerializer; +use pocketmine\event\block\StructureGrowEvent; use pocketmine\item\Bamboo as ItemBamboo; use pocketmine\item\Fertilizer; use pocketmine\item\Item; @@ -210,6 +211,13 @@ class Bamboo extends Transparent{ foreach($newBlocks as $idx => $newBlock){ $tx->addBlock($this->position->subtract(0, $idx - $growAmount, 0), $newBlock); } + + $ev = new StructureGrowEvent($this, $tx); + $ev->call(); + if($ev->isCancelled()){ + return false; + } + return $tx->apply(); } diff --git a/src/block/BambooSapling.php b/src/block/BambooSapling.php index e01094912..b617236ed 100644 --- a/src/block/BambooSapling.php +++ b/src/block/BambooSapling.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\event\block\StructureGrowEvent; use pocketmine\item\Bamboo as ItemBamboo; use pocketmine\item\Fertilizer; use pocketmine\item\Item; @@ -96,6 +97,13 @@ final class BambooSapling extends Flowable{ $bamboo = VanillaBlocks::BAMBOO(); $tx->addBlock($this->position, $bamboo) ->addBlock($this->position->up(), (clone $bamboo)->setLeafSize(Bamboo::SMALL_LEAVES)); + + $ev = new StructureGrowEvent($this, $tx); + $ev->call(); + if($ev->isCancelled()){ + return false; + } + return $tx->apply(); } diff --git a/src/block/Sapling.php b/src/block/Sapling.php index 586f956f3..c084a8296 100644 --- a/src/block/Sapling.php +++ b/src/block/Sapling.php @@ -76,7 +76,7 @@ class Sapling extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer){ - Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType); + Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType, true); $item->pop(); @@ -99,7 +99,7 @@ class Sapling extends Flowable{ 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->ready){ - Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType); + Tree::growTree($this->position->getWorld(), $this->position->x, $this->position->y, $this->position->z, new Random(mt_rand()), $this->treeType, true); }else{ $this->ready = true; $this->position->getWorld()->setBlock($this->position, $this); diff --git a/src/event/block/StructureGrowEvent.php b/src/event/block/StructureGrowEvent.php new file mode 100644 index 000000000..88fd54ae8 --- /dev/null +++ b/src/event/block/StructureGrowEvent.php @@ -0,0 +1,29 @@ +transaction = $transaction; + } + + public function getTransaction() : BlockTransaction{ + return $this->transaction; + } +} \ No newline at end of file diff --git a/src/world/generator/object/BirchTree.php b/src/world/generator/object/BirchTree.php index c3fdc8de1..b320d5890 100644 --- a/src/world/generator/object/BirchTree.php +++ b/src/world/generator/object/BirchTree.php @@ -36,11 +36,11 @@ class BirchTree extends Tree{ $this->superBirch = $superBirch; } - public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : void{ + public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ $this->treeHeight = $random->nextBoundedInt(3) + 5; if($this->superBirch){ $this->treeHeight += 5; } - parent::placeObject($world, $x, $y, $z, $random); + parent::placeObject($world, $x, $y, $z, $random, $callEvent); } } diff --git a/src/world/generator/object/OakTree.php b/src/world/generator/object/OakTree.php index e66b7a0e2..14bbcaf7d 100644 --- a/src/world/generator/object/OakTree.php +++ b/src/world/generator/object/OakTree.php @@ -33,8 +33,8 @@ class OakTree extends Tree{ parent::__construct(VanillaBlocks::OAK_LOG(), VanillaBlocks::OAK_LEAVES()); } - public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : void{ + public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ $this->treeHeight = $random->nextBoundedInt(3) + 4; - parent::placeObject($world, $x, $y, $z, $random); + parent::placeObject($world, $x, $y, $z, $random, $callEvent); } } diff --git a/src/world/generator/object/SpruceTree.php b/src/world/generator/object/SpruceTree.php index 679b5fcc0..7686c6cd2 100644 --- a/src/world/generator/object/SpruceTree.php +++ b/src/world/generator/object/SpruceTree.php @@ -39,9 +39,9 @@ class SpruceTree extends Tree{ return $this->treeHeight - $random->nextBoundedInt(3); } - public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : void{ + public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ $this->treeHeight = $random->nextBoundedInt(4) + 6; - parent::placeObject($world, $x, $y, $z, $random); + parent::placeObject($world, $x, $y, $z, $random, $callEvent); } protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ diff --git a/src/world/generator/object/Tree.php b/src/world/generator/object/Tree.php index c15ffb561..61aa1ae74 100644 --- a/src/world/generator/object/Tree.php +++ b/src/world/generator/object/Tree.php @@ -28,6 +28,7 @@ use pocketmine\block\Leaves; use pocketmine\block\Sapling; use pocketmine\block\utils\TreeType; use pocketmine\block\VanillaBlocks; +use pocketmine\event\block\StructureGrowEvent; use pocketmine\utils\Random; use pocketmine\world\BlockTransaction; use pocketmine\world\ChunkManager; @@ -51,10 +52,11 @@ abstract class Tree{ /** * @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) : void{ + public static function growTree(ChunkManager $world, int $x, int $y, int $z, Random $random, ?TreeType $type = null, bool $callEvent = false) : void{ /** @var null|Tree $tree */ $tree = null; $type = $type ?? TreeType::OAK(); @@ -78,7 +80,7 @@ abstract class Tree{ } if($tree !== null and $tree->canPlaceObject($world, $x, $y, $z, $random)){ - $tree->placeObject($world, $x, $y, $z, $random); + $tree->placeObject($world, $x, $y, $z, $random, $callEvent); } } @@ -100,11 +102,19 @@ abstract class Tree{ return true; } - public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random) : void{ + public function placeObject(ChunkManager $world, int $x, int $y, int $z, Random $random, bool $callEvent = false) : void{ $transaction = new BlockTransaction($world); $this->placeTrunk($x, $y, $z, $random, $this->generateChunkHeight($random), $transaction); $this->placeCanopy($x, $y, $z, $random, $transaction); + if($callEvent){ + $ev = new StructureGrowEvent($world->getBlockAt($x, $y, $z), $transaction); + $ev->call(); + if($ev->isCancelled()){ + return; + } + } + $transaction->apply(); //TODO: handle return value on failure }