BlockGrowEvent: add player information for bonemeal usage (#5596)

this is in line with StructureGrowEvent, which also has a similar API.
This commit is contained in:
Hugo_ 2023-05-08 18:38:07 +02:00 committed by GitHub
parent 6f0eb019d2
commit d834266635
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 8 deletions

View File

@ -94,7 +94,7 @@ class CocoaBlock extends Transparent{
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($item instanceof Fertilizer && $this->grow()){
if($item instanceof Fertilizer && $this->grow($player)){
$item->pop();
return true;
@ -119,11 +119,11 @@ class CocoaBlock extends Transparent{
}
}
private function grow() : bool{
private function grow(?Player $player = null) : bool{
if($this->age < self::MAX_AGE){
$block = clone $this;
$block->age++;
$ev = new BlockGrowEvent($this, $block);
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());

View File

@ -69,7 +69,7 @@ abstract class Crops extends Flowable{
$block->age = self::MAX_AGE;
}
$ev = new BlockGrowEvent($this, $block);
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());

View File

@ -51,7 +51,7 @@ class Sugarcane extends Flowable{
return $bottom;
}
private function grow(Position $pos) : bool{
private function grow(Position $pos, ?Player $player = null) : bool{
$grew = false;
$world = $pos->getWorld();
for($y = 1; $y < 3; ++$y){
@ -60,7 +60,7 @@ class Sugarcane extends Flowable{
}
$b = $world->getBlockAt($pos->x, $pos->y + $y, $pos->z);
if($b->getTypeId() === BlockTypeIds::AIR){
$ev = new BlockGrowEvent($b, VanillaBlocks::SUGARCANE());
$ev = new BlockGrowEvent($b, VanillaBlocks::SUGARCANE(), $player);
$ev->call();
if($ev->isCancelled()){
break;
@ -89,7 +89,7 @@ class Sugarcane extends Flowable{
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($item instanceof Fertilizer){
if($this->grow($this->seekToBottom())){
if($this->grow($this->seekToBottom(), $player)){
$item->pop();
}

View File

@ -87,7 +87,7 @@ class SweetBerryBush extends Flowable{
$block = clone $this;
$block->age++;
$ev = new BlockGrowEvent($this, $block);
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){

View File

@ -23,9 +23,27 @@ declare(strict_types=1);
namespace pocketmine\event\block;
use pocketmine\block\Block;
use pocketmine\player\Player;
/**
* Called when plants or crops grow.
*/
class BlockGrowEvent extends BaseBlockChangeEvent{
public function __construct(
Block $block,
Block $newState,
private ?Player $player = null,
){
parent::__construct($block, $newState);
}
/**
* It returns the player which grows the crop.
* It returns null when the crop grows by itself.
*/
public function getPlayer() : ?Player{
return $this->player;
}
}