diff --git a/src/block/Bamboo.php b/src/block/Bamboo.php index fc5182cfb..171491406 100644 --- a/src/block/Bamboo.php +++ b/src/block/Bamboo.php @@ -145,12 +145,12 @@ class Bamboo extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer){ $top = $this->seekToTop(); - if($top->grow(self::getMaxHeight($top->position->getFloorX(), $top->position->getFloorZ()), mt_rand(1, 2))){ + if($top->grow(self::getMaxHeight($top->position->getFloorX(), $top->position->getFloorZ()), mt_rand(1, 2), $player)){ $item->pop(); return true; } }elseif($item instanceof ItemBamboo){ - if($this->seekToTop()->grow(PHP_INT_MAX, 1)){ + if($this->seekToTop()->grow(PHP_INT_MAX, 1, $player)){ $item->pop(); return true; } @@ -165,7 +165,7 @@ class Bamboo extends Transparent{ } } - private function grow(int $maxHeight, int $growAmount) : bool{ + private function grow(int $maxHeight, int $growAmount, ?Player $player) : bool{ $world = $this->position->getWorld(); if(!$world->getBlock($this->position->up())->canBeReplaced()){ return false; @@ -212,7 +212,7 @@ class Bamboo extends Transparent{ $tx->addBlock($this->position->subtract(0, $idx - $growAmount, 0), $newBlock); } - $ev = new StructureGrowEvent($this, $tx); + $ev = new StructureGrowEvent($this, $tx, $player); $ev->call(); if($ev->isCancelled()){ return false; @@ -229,7 +229,7 @@ class Bamboo extends Transparent{ $world = $this->position->getWorld(); if($this->ready){ $this->ready = false; - if($world->getFullLight($this->position) < 9 || !$this->grow(self::getMaxHeight($this->position->getFloorX(), $this->position->getFloorZ()), 1)){ + if($world->getFullLight($this->position) < 9 || !$this->grow(self::getMaxHeight($this->position->getFloorX(), $this->position->getFloorZ()), 1, null)){ $world->setBlock($this->position, $this); } }elseif($world->getBlock($this->position->up())->canBeReplaced()){ diff --git a/src/block/BambooSapling.php b/src/block/BambooSapling.php index b617236ed..3da1e593a 100644 --- a/src/block/BambooSapling.php +++ b/src/block/BambooSapling.php @@ -73,7 +73,7 @@ final class BambooSapling extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer || $item instanceof ItemBamboo){ - if($this->grow()){ + if($this->grow($player)){ $item->pop(); return true; } @@ -87,7 +87,7 @@ final class BambooSapling extends Flowable{ } } - private function grow() : bool{ + private function grow(?Player $player) : bool{ $world = $this->position->getWorld(); if(!$world->getBlock($this->position->up())->canBeReplaced()){ return false; @@ -98,7 +98,7 @@ final class BambooSapling extends Flowable{ $tx->addBlock($this->position, $bamboo) ->addBlock($this->position->up(), (clone $bamboo)->setLeafSize(Bamboo::SMALL_LEAVES)); - $ev = new StructureGrowEvent($this, $tx); + $ev = new StructureGrowEvent($this, $tx, $player); $ev->call(); if($ev->isCancelled()){ return false; @@ -115,7 +115,7 @@ final class BambooSapling extends Flowable{ $world = $this->position->getWorld(); if($this->ready){ $this->ready = false; - if($world->getFullLight($this->position) < 9 || !$this->grow()){ + if($world->getFullLight($this->position) < 9 || !$this->grow(null)){ $world->setBlock($this->position, $this); } }elseif($world->getBlock($this->position->up())->canBeReplaced()){ diff --git a/src/block/Sapling.php b/src/block/Sapling.php index cb9d36b02..169cb9a32 100644 --- a/src/block/Sapling.php +++ b/src/block/Sapling.php @@ -77,7 +77,7 @@ class Sapling extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer){ - $this->grow(); + $this->grow($player); $item->pop(); @@ -100,7 +100,7 @@ class Sapling extends Flowable{ public function onRandomTick() : void{ if($this->position->getWorld()->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 and mt_rand(1, 7) === 1){ if($this->ready){ - $this->grow(); + $this->grow(null); }else{ $this->ready = true; $this->position->getWorld()->setBlock($this->position, $this); @@ -108,7 +108,7 @@ class Sapling extends Flowable{ } } - private function grow() : void{ + private function grow(?Player $player) : void{ $random = new Random(mt_rand()); $tree = TreeFactory::get($random, $this->treeType); $transaction = $tree?->getBlockTransaction($this->position->getWorld(), $this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ(), $random); @@ -116,7 +116,7 @@ class Sapling extends Flowable{ return; } - $ev = new StructureGrowEvent($this, $transaction); + $ev = new StructureGrowEvent($this, $transaction, $player); $ev->call(); if($ev->isCancelled()){ return; diff --git a/src/event/block/StructureGrowEvent.php b/src/event/block/StructureGrowEvent.php index 88fd54ae8..30d7c7ceb 100644 --- a/src/event/block/StructureGrowEvent.php +++ b/src/event/block/StructureGrowEvent.php @@ -7,6 +7,7 @@ namespace pocketmine\event\block; use pocketmine\block\Block; use pocketmine\event\Cancellable; use pocketmine\event\CancellableTrait; +use pocketmine\player\Player; use pocketmine\world\BlockTransaction; /** @@ -17,13 +18,23 @@ class StructureGrowEvent extends BlockEvent implements Cancellable{ use CancellableTrait; private BlockTransaction $transaction; + private ?Player $player; - public function __construct(Block $block, BlockTransaction $transaction){ + public function __construct(Block $block, BlockTransaction $transaction, ?Player $player){ parent::__construct($block); $this->transaction = $transaction; + $this->player = $player; } public function getTransaction() : BlockTransaction{ return $this->transaction; } + + /** + * It returns the player which grows the structure. + * It returns null when the structure grows by itself. + */ + public function getPlayer() : ?Player{ + return $this->player; + } } \ No newline at end of file