From ead8ccf08d3d3792224795728d225e949f33ab9f Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 2 Nov 2021 18:05:07 +0100 Subject: [PATCH] CocoaBlock: call BlockGrowEvent when growing for any reason (#4536) --- src/block/CocoaBlock.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/block/CocoaBlock.php b/src/block/CocoaBlock.php index 82b514172..efcc4590f 100644 --- a/src/block/CocoaBlock.php +++ b/src/block/CocoaBlock.php @@ -26,6 +26,7 @@ namespace pocketmine\block; use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\TreeType; +use pocketmine\event\block\BlockGrowEvent; use pocketmine\item\Fertilizer; use pocketmine\item\Item; use pocketmine\item\VanillaItems; @@ -94,10 +95,7 @@ class CocoaBlock extends Transparent{ } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->age < 2 and $item instanceof Fertilizer){ - $this->age++; - $this->position->getWorld()->setBlock($this->position, $this); - + if($item instanceof Fertilizer && $this->grow()){ $item->pop(); return true; @@ -117,12 +115,25 @@ class CocoaBlock extends Transparent{ } public function onRandomTick() : void{ - if($this->age < 2 and mt_rand(1, 5) === 1){ - $this->age++; - $this->position->getWorld()->setBlock($this->position, $this); + if(mt_rand(1, 5) === 1){ + $this->grow(); } } + private function grow() : bool{ + if($this->age < 2){ + $block = clone $this; + $block->age++; + $ev = new BlockGrowEvent($this, $block); + $ev->call(); + if(!$ev->isCancelled()){ + $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + return true; + } + } + return false; + } + public function getDropsForCompatibleTool(Item $item) : array{ return [ VanillaItems::COCOA_BEANS()->setCount($this->age === 2 ? mt_rand(2, 3) : 1)