Block: introduce new methods to reduce support type boilerplate checks

this switches from a 'can be supported by' concept to a 'can stay at this position' paradigm, which requires way less boilerplate code.

there may be further improvements we can make from here, such as adding traits, but this is a good first step.
This commit is contained in:
Dylan K. Taylor
2023-07-21 15:02:25 +01:00
parent 3c34841dfc
commit 5899f2fc1d
25 changed files with 105 additions and 108 deletions

View File

@ -26,7 +26,6 @@ namespace pocketmine\block;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item;
use pocketmine\math\Axis;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
@ -56,15 +55,13 @@ class Torch extends Flowable{
}
public function onNearbyBlockChange() : void{
$face = Facing::opposite($this->facing);
if(!$this->canBeSupportedBy($this->getSide($face), $this->facing)){
if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing))){
$this->position->getWorld()->useBreakOn($this->position);
}
}
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($face !== Facing::DOWN && $this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face)){
if($face !== Facing::DOWN && $this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
$this->facing = $face;
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}else{
@ -75,8 +72,7 @@ class Torch extends Flowable{
Facing::EAST,
Facing::DOWN
] as $side){
$block = $this->getSide($side);
if($this->canBeSupportedBy($block, Facing::opposite($side))){
if($this->canBeSupportedAt($blockReplace, $side)){
$this->facing = Facing::opposite($side);
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
@ -85,8 +81,9 @@ class Torch extends Flowable{
return false;
}
private function canBeSupportedBy(Block $support, int $face) : bool{
return ($face === Facing::UP && $support->getSupportType($face)->hasCenterSupport()) ||
(Facing::axis($face) !== Axis::Y && $support->getSupportType($face)->equals(SupportType::FULL()));
private function canBeSupportedAt(Block $block, int $face) : bool{
return $face === Facing::DOWN ?
$block->getAdjacentSupportType($face)->hasCenterSupport() :
$block->getAdjacentSupportType($face)->equals(SupportType::FULL());
}
}