BlockFormEvent: Added getCausingBlock() method (#5226)

This commit is contained in:
Colin 2022-08-15 17:26:48 +02:00 committed by GitHub
parent cb020988d4
commit 304bb84af2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 7 deletions

View File

@ -42,8 +42,8 @@ class ConcretePowder extends Opaque implements Fallable{
}
public function onNearbyBlockChange() : void{
if(($block = $this->checkAdjacentWater()) !== null){
$ev = new BlockFormEvent($this, $block);
if(($water = $this->getAdjacentWater()) !== null){
$ev = new BlockFormEvent($this, VanillaBlocks::CONCRETE()->setColor($this->color), $water);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
@ -54,16 +54,20 @@ class ConcretePowder extends Opaque implements Fallable{
}
public function tickFalling() : ?Block{
return $this->checkAdjacentWater();
if ($this->getAdjacentWater() === null) {
return null;
}
return VanillaBlocks::CONCRETE()->setColor($this->color);
}
private function checkAdjacentWater() : ?Block{
private function getAdjacentWater() : ?Water{
foreach(Facing::ALL as $i){
if($i === Facing::DOWN){
continue;
}
if($this->getSide($i) instanceof Water){
return VanillaBlocks::CONCRETE()->setColor($this->color);
$block = $this->getSide($i);
if($block instanceof Water){
return $block;
}
}

View File

@ -366,7 +366,7 @@ abstract class Liquid extends Transparent{
}
protected function liquidCollide(Block $cause, Block $result) : bool{
$ev = new BlockFormEvent($this, $result);
$ev = new BlockFormEvent($this, $result, $cause);
$ev->call();
if(!$ev->isCancelled()){
$world = $this->position->getWorld();

View File

@ -23,10 +23,26 @@ declare(strict_types=1);
namespace pocketmine\event\block;
use pocketmine\block\Block;
/**
* Called when a new block forms, usually as the result of some action.
* This could be things like obsidian forming due to collision of lava and water.
*/
class BlockFormEvent extends BaseBlockChangeEvent{
public function __construct(
Block $block,
Block $newState,
private Block $causingBlock
){
parent::__construct($block, $newState);
}
/**
* Returns the block which caused the target block to form into a new state.
*/
public function getCausingBlock() : Block{
return $this->causingBlock;
}
}