Reduce repeated block-change-event related code

the new helper code reveals even more repetition, but this is at least consistent now.
This commit is contained in:
Dylan K. Taylor 2023-08-02 13:40:12 +01:00
parent 6000bcccdd
commit 82f87cc2da
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
20 changed files with 166 additions and 158 deletions

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\CoralTypeTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\event\block\BlockDeathEvent;
use pocketmine\item\Item;
use function mt_rand;
@ -46,11 +46,7 @@ abstract class BaseCoral extends Transparent{
public function onScheduledUpdate() : void{
if(!$this->dead && !$this->isCoveredWithWater()){
$ev = new BlockDeathEvent($this, $this->setDead(true));
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::die($this, (clone $this)->setDead(true));
}
}

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\Item;
@ -111,12 +111,7 @@ class Cactus extends Transparent{
}
$b = $world->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z);
if($b->getTypeId() === BlockTypeIds::AIR){
$ev = new BlockGrowEvent($b, VanillaBlocks::CACTUS());
$ev->call();
if($ev->isCancelled()){
break;
}
$world->setBlock($b->position, $ev->getNewState());
BlockEventHelper::grow($b, VanillaBlocks::CACTUS(), null);
}else{
break;
}

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
@ -114,16 +114,12 @@ class CaveVines extends Flowable{
return true;
}
if($item instanceof Fertilizer){
$ev = new BlockGrowEvent($this, (clone $this)
$newState = (clone $this)
->setBerries(true)
->setHead(!$this->getSide(Facing::DOWN)->hasSameTypeId($this))
);
$ev->call();
if($ev->isCancelled()){
return false;
->setHead(!$this->getSide(Facing::DOWN)->hasSameTypeId($this));
if(BlockEventHelper::grow($this, $newState, $player)){
$item->pop();
}
$item->pop();
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
return true;
}
return false;
@ -141,16 +137,10 @@ class CaveVines extends Flowable{
if($world->isInWorld($growthPos->getFloorX(), $growthPos->getFloorY(), $growthPos->getFloorZ())){
$block = $world->getBlock($growthPos);
if($block->getTypeId() === BlockTypeIds::AIR){
$ev = new BlockGrowEvent($block, VanillaBlocks::CAVE_VINES()
$newState = VanillaBlocks::CAVE_VINES()
->setAge($this->age + 1)
->setBerries(mt_rand(1, 9) === 1)
);
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($growthPos, $ev->getNewState());
}
->setBerries(mt_rand(1, 9) === 1);
BlockEventHelper::grow($block, $newState, null);
}
}
}

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\block\utils\WoodType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
@ -123,12 +123,7 @@ class CocoaBlock extends Transparent{
if($this->age < self::MAX_AGE){
$block = clone $this;
$block->age++;
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
return true;
}
return BlockEventHelper::grow($this, $block, $player);
}
return false;
}

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\ColoredTrait;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\event\block\BlockFormEvent;
use pocketmine\math\Facing;
class ConcretePowder extends Opaque implements Fallable{
@ -43,11 +43,7 @@ class ConcretePowder extends Opaque implements Fallable{
public function onNearbyBlockChange() : void{
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());
}
BlockEventHelper::form($this, VanillaBlocks::CONCRETE()->setColor($this->color), $water);
}else{
$this->startFalling();
}

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\CoralTypeTrait;
use pocketmine\event\block\BlockDeathEvent;
use pocketmine\item\Item;
use function mt_rand;
@ -55,11 +55,7 @@ final class CoralBlock extends Opaque{
}
}
if(!$hasWater){
$ev = new BlockDeathEvent($this, $this->setDead(true));
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::die($this, (clone $this)->setDead(true));
}
}
}

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
use pocketmine\math\Facing;
@ -68,11 +68,7 @@ abstract class Crops extends Flowable{
if($block->age > self::MAX_AGE){
$block->age = self::MAX_AGE;
}
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
if(BlockEventHelper::grow($this, $block, $player)){
$item->pop();
}
@ -96,11 +92,7 @@ abstract class Crops extends Flowable{
if($this->age < self::MAX_AGE && mt_rand(0, 2) === 1){
$block = clone $this;
++$block->age;
$ev = new BlockGrowEvent($this, $block);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::grow($this, $block, null);
}
}
}

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockBurnEvent;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\math\Facing;
use pocketmine\world\format\Chunk;
use pocketmine\world\World;
@ -225,13 +225,6 @@ class Fire extends BaseFire{
}
private function spreadBlock(Block $block, Block $newState) : bool{
$ev = new BlockSpreadEvent($block, $this, $newState);
$ev->call();
if(!$ev->isCancelled()){
$block->position->getWorld()->setBlock($block->position, $ev->getNewState());
return true;
}
return false;
return BlockEventHelper::spread($block, $newState, $this);
}
}

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockMeltEvent;
use function mt_rand;
class FrostedIce extends Ice{
@ -97,11 +97,7 @@ class FrostedIce extends Ice{
private function tryMelt() : bool{
$world = $this->position->getWorld();
if($this->age >= self::MAX_AGE){
$ev = new BlockMeltEvent($this, VanillaBlocks::WATER());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::melt($this, VanillaBlocks::WATER());
return true;
}

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
@ -166,14 +166,7 @@ class GlowLichen extends Transparent{
return false;
}
$ev = new BlockSpreadEvent($replacedBlock, $this, $replacementBlock);
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($replacedBlock->getPosition(), $ev->getNewState());
return true;
}
return false;
return BlockEventHelper::spread($replacedBlock, $replacementBlock, $this);
}
/**

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\DirtType;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Hoe;
use pocketmine\item\Item;
@ -58,11 +58,7 @@ class Grass extends Opaque{
$lightAbove = $world->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z);
if($lightAbove < 4 && $world->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){
//grass dies
$ev = new BlockSpreadEvent($this, $this, VanillaBlocks::DIRT());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState(), false);
}
BlockEventHelper::spread($this, VanillaBlocks::DIRT(), $this);
}elseif($lightAbove >= 9){
//try grass spread
for($i = 0; $i < 4; ++$i){
@ -80,11 +76,7 @@ class Grass extends Opaque{
continue;
}
$ev = new BlockSpreadEvent($b, $this, VanillaBlocks::GRASS());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($b->position, $ev->getNewState(), false);
}
BlockEventHelper::spread($b, VanillaBlocks::GRASS(), $this);
}
}
}

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\event\block\BlockMeltEvent;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\Item;
use pocketmine\player\Player;
@ -53,11 +53,7 @@ class Ice extends Transparent{
public function onRandomTick() : void{
$world = $this->position->getWorld();
if($world->getHighestAdjacentBlockLight($this->position->x, $this->position->y, $this->position->z) >= 12){
$ev = new BlockMeltEvent($this, VanillaBlocks::WATER());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::melt($this, VanillaBlocks::WATER());
}
}

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\MinimumCostFlowCalculator;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity;
use pocketmine\event\block\BlockFormEvent;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
@ -363,12 +363,8 @@ abstract class Liquid extends Transparent{
}
protected function liquidCollide(Block $cause, Block $result) : bool{
$ev = new BlockFormEvent($this, $result, $cause);
$ev->call();
if(!$ev->isCancelled()){
$world = $this->position->getWorld();
$world->setBlock($this->position, $ev->getNewState());
$world->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (lcg_value() - lcg_value()) * 0.8));
if(BlockEventHelper::form($this, $result, $cause)){
$this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (lcg_value() - lcg_value()) * 0.8));
}
return true;
}

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\DirtType;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use function mt_rand;
@ -54,11 +54,7 @@ class Mycelium extends Opaque{
$block = $world->getBlockAt($x, $y, $z);
if($block instanceof Dirt && $block->getDirtType()->equals(DirtType::NORMAL())){
if($block->getSide(Facing::UP) instanceof Transparent){
$ev = new BlockSpreadEvent($block, $this, VanillaBlocks::MYCELIUM());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($block->position, $ev->getNewState());
}
BlockEventHelper::spread($block, VanillaBlocks::MYCELIUM(), $this);
}
}
}

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\FortuneDropHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
@ -76,11 +76,7 @@ class NetherWartPlant extends Flowable{
if($this->age < self::MAX_AGE && mt_rand(0, 10) === 0){ //Still growing
$block = clone $this;
$block->age++;
$ev = new BlockGrowEvent($this, $block);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::grow($this, $block, null);
}
}

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockMeltEvent;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use pocketmine\math\AxisAlignedBB;
@ -105,11 +105,7 @@ class SnowLayer extends Flowable implements Fallable{
public function onRandomTick() : void{
$world = $this->position->getWorld();
if($world->getBlockLightAt($this->position->x, $this->position->y, $this->position->z) >= 12){
$ev = new BlockMeltEvent($this, VanillaBlocks::AIR());
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::melt($this, VanillaBlocks::AIR());
}
}

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use function array_rand;
@ -64,11 +64,7 @@ abstract class Stem extends Crops{
if($this->age < self::MAX_AGE){
$block = clone $this;
++$block->age;
$ev = new BlockGrowEvent($this, $block);
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::grow($this, $block, null);
}else{
$grow = $this->getPlant();
foreach(Facing::HORIZONTAL as $side){
@ -80,12 +76,7 @@ abstract class Stem extends Crops{
$facing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
$side = $this->getSide($facing);
if($side->getTypeId() === BlockTypeIds::AIR && $side->getSide(Facing::DOWN)->hasTypeTag(BlockTypeTags::DIRT)){
$ev = new BlockGrowEvent($side, $grow);
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $this->setFacing($facing));
$world->setBlock($side->position, $ev->getNewState());
}
BlockEventHelper::grow($side, $grow, null);
}
}
}

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
use pocketmine\math\Facing;
@ -60,13 +60,11 @@ 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(), $player);
$ev->call();
if($ev->isCancelled()){
if(BlockEventHelper::grow($b, VanillaBlocks::SUGARCANE(), $player)){
$grew = true;
}else{
break;
}
$world->setBlock($b->position, $ev->getNewState());
$grew = true;
}elseif(!$b->hasSameTypeId($this)){
break;
}

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\FortuneDropHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity;
use pocketmine\entity\Living;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
@ -87,15 +87,9 @@ class SweetBerryBush extends Flowable{
if($this->age < self::STAGE_MATURE && $item instanceof Fertilizer){
$block = clone $this;
$block->age++;
$ev = new BlockGrowEvent($this, $block, $player);
$ev->call();
if(!$ev->isCancelled()){
$world->setBlock($this->position, $ev->getNewState());
if(BlockEventHelper::grow($this, $block, $player)){
$item->pop();
}
}elseif(($dropAmount = $this->getBerryDropAmount()) > 0){
$world->setBlock($this->position, $this->setAge(self::STAGE_BUSH_NO_BERRIES));
$world->dropItem($this->position, $this->asItem()->setCount($dropAmount));
@ -133,11 +127,7 @@ class SweetBerryBush extends Flowable{
if($this->age < self::STAGE_MATURE && mt_rand(0, 2) === 1){
$block = clone $this;
++$block->age;
$ev = new BlockGrowEvent($this, $block);
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState());
}
BlockEventHelper::grow($this, $block, null);
}
}

View File

@ -0,0 +1,115 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\block\utils;
use pocketmine\block\Block;
use pocketmine\event\block\BlockDeathEvent;
use pocketmine\event\block\BlockFormEvent;
use pocketmine\event\block\BlockGrowEvent;
use pocketmine\event\block\BlockMeltEvent;
use pocketmine\event\block\BlockSpreadEvent;
use pocketmine\player\Player;
/**
* Helper class to call block changing events and apply the results to the world.
* TODO: try to further reduce the amount of code duplication here - while this is much better than before, it's still
* very repetitive.
*/
final class BlockEventHelper{
public static function grow(Block $oldState, Block $newState, ?Player $causingPlayer) : bool{
if(BlockGrowEvent::hasHandlers()){
$ev = new BlockGrowEvent($oldState, $newState, $causingPlayer);
$ev->call();
if($ev->isCancelled()){
return false;
}
$newState = $ev->getNewState();
}
$position = $oldState->getPosition();
$position->getWorld()->setBlock($position, $newState);
return true;
}
public static function spread(Block $oldState, Block $newState, Block $source) : bool{
if(BlockSpreadEvent::hasHandlers()){
$ev = new BlockSpreadEvent($oldState, $source, $newState);
$ev->call();
if($ev->isCancelled()){
return false;
}
$newState = $ev->getNewState();
}
$position = $oldState->getPosition();
$position->getWorld()->setBlock($position, $newState);
return true;
}
public static function form(Block $oldState, Block $newState, Block $cause) : bool{
if(BlockFormEvent::hasHandlers()){
$ev = new BlockFormEvent($oldState, $newState, $cause);
$ev->call();
if($ev->isCancelled()){
return false;
}
$newState = $ev->getNewState();
}
$position = $oldState->getPosition();
$position->getWorld()->setBlock($position, $newState);
return true;
}
public static function melt(Block $oldState, Block $newState) : bool{
if(BlockMeltEvent::hasHandlers()){
$ev = new BlockMeltEvent($oldState, $newState);
$ev->call();
if($ev->isCancelled()){
return false;
}
$newState = $ev->getNewState();
}
$position = $oldState->getPosition();
$position->getWorld()->setBlock($position, $newState);
return true;
}
public static function die(Block $oldState, Block $newState) : bool{
if(BlockDeathEvent::hasHandlers()){
$ev = new BlockDeathEvent($oldState, $newState);
$ev->call();
if($ev->isCancelled()){
return false;
}
$newState = $ev->getNewState();
}
$position = $oldState->getPosition();
$position->getWorld()->setBlock($position, $newState);
return true;
}
}