From 5899f2fc1d39175ce7bf0672cc6c09514779b5f2 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 21 Jul 2023 15:02:25 +0100 Subject: [PATCH] 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. --- src/block/BaseRail.php | 4 ++-- src/block/Bed.php | 8 ++++---- src/block/Bell.php | 20 ++++++++++---------- src/block/Block.php | 5 +++++ src/block/Button.php | 8 ++++---- src/block/Candle.php | 3 +-- src/block/CaveVines.php | 8 ++++---- src/block/Coral.php | 8 ++++---- src/block/Door.php | 9 ++++----- src/block/FloorCoralFan.php | 8 ++++---- src/block/FlowerPot.php | 8 ++++---- src/block/GlowLichen.php | 4 ++-- src/block/ItemFrame.php | 8 ++++---- src/block/Ladder.php | 8 ++++---- src/block/Lantern.php | 11 ++++++----- src/block/Lever.php | 9 ++++----- src/block/NetherVines.php | 13 +++++-------- src/block/PressurePlate.php | 8 ++++---- src/block/RedstoneComparator.php | 8 ++++---- src/block/RedstoneRepeater.php | 8 ++++---- src/block/RedstoneWire.php | 8 ++++---- src/block/SnowLayer.php | 6 +++--- src/block/SporeBlossom.php | 8 ++++---- src/block/Torch.php | 17 +++++++---------- src/block/WallCoralFan.php | 8 ++++---- 25 files changed, 105 insertions(+), 108 deletions(-) diff --git a/src/block/BaseRail.php b/src/block/BaseRail.php index 971beffac..0bcb2f340 100644 --- a/src/block/BaseRail.php +++ b/src/block/BaseRail.php @@ -38,7 +38,7 @@ use function in_array; abstract class BaseRail extends Flowable{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($blockReplace->getSide(Facing::DOWN)->getSupportType(Facing::UP)->hasEdgeSupport()){ + if($blockReplace->getAdjacentSupportType(Facing::DOWN)->hasEdgeSupport()){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } @@ -222,7 +222,7 @@ abstract class BaseRail extends Flowable{ public function onNearbyBlockChange() : void{ $world = $this->position->getWorld(); - if(!$this->getSide(Facing::DOWN)->getSupportType(Facing::UP)->hasEdgeSupport()){ + if(!$this->getAdjacentSupportType(Facing::DOWN)->hasEdgeSupport()){ $world->useBreakOn($this->position); }else{ foreach($this->getCurrentShapeConnections() as $connection){ diff --git a/src/block/Bed.php b/src/block/Bed.php index 13b466026..312b21cd1 100644 --- a/src/block/Bed.php +++ b/src/block/Bed.php @@ -177,11 +177,11 @@ class Bed extends Transparent{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ $this->facing = $player !== null ? $player->getHorizontalFacing() : Facing::NORTH; $next = $this->getSide($this->getOtherHalfSide()); - if($next->canBeReplaced() && $this->canBeSupportedBy($next->getSide(Facing::DOWN))){ + if($next->canBeReplaced() && $this->canBeSupportedAt($next)){ $nextState = clone $this; $nextState->head = true; $tx->addBlock($blockReplace->position, $this)->addBlock($next->position, $nextState); @@ -208,8 +208,8 @@ class Bed extends Transparent{ return parent::getAffectedBlocks(); } - private function canBeSupportedBy(Block $block) : bool{ - return !$block->getSupportType(Facing::UP)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block) : bool{ + return !$block->getAdjacentSupportType(Facing::DOWN)->equals(SupportType::NONE()); } public function getMaxStackSize() : int{ return 1; } diff --git a/src/block/Bell.php b/src/block/Bell.php index 753d6453d..0e2941d8e 100644 --- a/src/block/Bell.php +++ b/src/block/Bell.php @@ -87,13 +87,13 @@ final class Bell extends Transparent{ return $this; } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return !$block->getSupportType($face)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return !$block->getAdjacentSupportType($face)->equals(SupportType::NONE()); } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($face === Facing::UP){ - if(!$this->canBeSupportedBy($tx->fetchBlock($this->position->down()), Facing::UP)){ + if(!$this->canBeSupportedAt($blockReplace, Facing::DOWN)){ return false; } if($player !== null){ @@ -101,18 +101,18 @@ final class Bell extends Transparent{ } $this->setAttachmentType(BellAttachmentType::FLOOR()); }elseif($face === Facing::DOWN){ - if(!$this->canBeSupportedBy($tx->fetchBlock($this->position->up()), Facing::DOWN)){ + if(!$this->canBeSupportedAt($blockReplace, Facing::UP)){ return false; } $this->setAttachmentType(BellAttachmentType::CEILING()); }else{ $this->setFacing($face); - if($this->canBeSupportedBy($tx->fetchBlock($this->position->getSide(Facing::opposite($face))), $face)){ + if($this->canBeSupportedAt($blockReplace, Facing::opposite($face))){ $this->setAttachmentType(BellAttachmentType::ONE_WALL()); }else{ return false; } - if($this->canBeSupportedBy($tx->fetchBlock($this->position->getSide($face)), Facing::opposite($face))){ + if($this->canBeSupportedAt($blockReplace, $face)){ $this->setAttachmentType(BellAttachmentType::TWO_WALLS()); } } @@ -121,10 +121,10 @@ final class Bell extends Transparent{ public function onNearbyBlockChange() : void{ if( - ($this->attachmentType->equals(BellAttachmentType::CEILING()) && !$this->canBeSupportedBy($this->getSide(Facing::UP), Facing::DOWN)) || - ($this->attachmentType->equals(BellAttachmentType::FLOOR()) && !$this->canBeSupportedBy($this->getSide(Facing::DOWN), Facing::UP)) || - ($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) && !$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $this->facing)) || - ($this->attachmentType->equals(BellAttachmentType::TWO_WALLS()) && (!$this->canBeSupportedBy($this->getSide($this->facing), Facing::opposite($this->facing)) || !$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $this->facing))) + ($this->attachmentType->equals(BellAttachmentType::CEILING()) && !$this->canBeSupportedAt($this, Facing::UP)) || + ($this->attachmentType->equals(BellAttachmentType::FLOOR()) && !$this->canBeSupportedAt($this, Facing::DOWN)) || + ($this->attachmentType->equals(BellAttachmentType::ONE_WALL()) && !$this->canBeSupportedAt($this, Facing::opposite($this->facing))) || + ($this->attachmentType->equals(BellAttachmentType::TWO_WALLS()) && (!$this->canBeSupportedAt($this, $this->facing) || !$this->canBeSupportedAt($this, Facing::opposite($this->facing)))) ){ $this->position->getWorld()->useBreakOn($this->position); } diff --git a/src/block/Block.php b/src/block/Block.php index b4203e6b6..0e045792f 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -41,6 +41,7 @@ use pocketmine\item\Item; use pocketmine\item\ItemBlock; use pocketmine\math\Axis; use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Facing; use pocketmine\math\RayTraceResult; use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; @@ -863,6 +864,10 @@ class Block{ return SupportType::FULL(); } + protected function getAdjacentSupportType(int $facing) : SupportType{ + return $this->getSide($facing)->getSupportType(Facing::opposite($facing)); + } + public function isFullCube() : bool{ $bb = $this->getCollisionBoxes(); diff --git a/src/block/Button.php b/src/block/Button.php index 85d1d3e09..73bd1d556 100644 --- a/src/block/Button.php +++ b/src/block/Button.php @@ -52,7 +52,7 @@ abstract class Button extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face)){ + if($this->canBeSupportedAt($blockReplace, $face)){ $this->facing = $face; return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } @@ -83,12 +83,12 @@ abstract class Button extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $this->facing)){ + if(!$this->canBeSupportedAt($this, $this->facing)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $support, int $face) : bool{ - return $support->getSupportType($face)->hasCenterSupport(); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return $block->getAdjacentSupportType(Facing::opposite($face))->hasCenterSupport(); } } diff --git a/src/block/Candle.php b/src/block/Candle.php index 5936a0812..7009acef6 100644 --- a/src/block/Candle.php +++ b/src/block/Candle.php @@ -104,8 +104,7 @@ class Candle extends Transparent{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - $down = $blockReplace->getSide(Facing::DOWN); - if(!$down->getSupportType(Facing::UP)->hasCenterSupport()){ + if(!$blockReplace->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport()){ return false; } $existing = $this->getCandleIfCompatibleType($blockReplace); diff --git a/src/block/CaveVines.php b/src/block/CaveVines.php index 55f73fb65..e56b8b720 100644 --- a/src/block/CaveVines.php +++ b/src/block/CaveVines.php @@ -87,18 +87,18 @@ class CaveVines extends Flowable{ return $this->berries ? 14 : 0; } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::DOWN)->equals(SupportType::FULL()) || $block->hasSameTypeId($this); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::UP)->equals(SupportType::FULL()) || $block->hasSameTypeId($this); } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::UP))){ + if(!$this->canBeSupportedAt($this)){ $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(!$this->canBeSupportedBy($blockReplace->getSide(Facing::UP))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } $this->age = mt_rand(0, self::MAX_AGE); diff --git a/src/block/Coral.php b/src/block/Coral.php index b621a3ab0..837a81857 100644 --- a/src/block/Coral.php +++ b/src/block/Coral.php @@ -32,7 +32,7 @@ use pocketmine\world\BlockTransaction; final class Coral extends BaseCoral{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($tx->fetchBlock($blockReplace->getPosition()->down()))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); @@ -40,14 +40,14 @@ final class Coral extends BaseCoral{ public function onNearbyBlockChange() : void{ $world = $this->position->getWorld(); - if(!$this->canBeSupportedBy($world->getBlock($this->position->down()))){ + if(!$this->canBeSupportedAt($this)){ $world->useBreakOn($this->position); }else{ parent::onNearbyBlockChange(); } } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::UP)->hasCenterSupport(); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport(); } } diff --git a/src/block/Door.php b/src/block/Door.php index 06da8e68b..a03427d5a 100644 --- a/src/block/Door.php +++ b/src/block/Door.php @@ -106,7 +106,7 @@ class Door extends Transparent{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN)) && !$this->getSide(Facing::DOWN) instanceof Door){ //Replace with common break method + if(!$this->canBeSupportedAt($this) && !$this->getSide(Facing::DOWN) instanceof Door){ //Replace with common break method $this->position->getWorld()->useBreakOn($this->position); //this will delete both halves if they exist } } @@ -114,8 +114,7 @@ class Door extends Transparent{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($face === Facing::UP){ $blockUp = $this->getSide(Facing::UP); - $blockDown = $this->getSide(Facing::DOWN); - if(!$blockUp->canBeReplaced() || !$this->canBeSupportedBy($blockDown)){ + if(!$blockUp->canBeReplaced() || !$this->canBeSupportedAt($blockReplace)){ return false; } @@ -172,7 +171,7 @@ class Door extends Transparent{ return parent::getAffectedBlocks(); } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::UP)->hasEdgeSupport(); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->hasEdgeSupport(); } } diff --git a/src/block/FloorCoralFan.php b/src/block/FloorCoralFan.php index efa560467..a267a0385 100644 --- a/src/block/FloorCoralFan.php +++ b/src/block/FloorCoralFan.php @@ -53,7 +53,7 @@ final class FloorCoralFan extends BaseCoral{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($tx->fetchBlock($blockReplace->getPosition()->down()))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } if($player !== null){ @@ -75,15 +75,15 @@ final class FloorCoralFan extends BaseCoral{ public function onNearbyBlockChange() : void{ $world = $this->position->getWorld(); - if(!$this->canBeSupportedBy($world->getBlock($this->position->down()))){ + if(!$this->canBeSupportedAt($this)){ $world->useBreakOn($this->position); }else{ parent::onNearbyBlockChange(); } } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::UP)->hasCenterSupport(); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport(); } public function asItem() : Item{ diff --git a/src/block/FlowerPot.php b/src/block/FlowerPot.php index 1c85ea0d8..4e4dbfa6e 100644 --- a/src/block/FlowerPot.php +++ b/src/block/FlowerPot.php @@ -90,7 +90,7 @@ class FlowerPot extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } @@ -98,13 +98,13 @@ class FlowerPot extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::UP)->hasCenterSupport(); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport(); } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ diff --git a/src/block/GlowLichen.php b/src/block/GlowLichen.php index d1baaa7d2..39ce512a6 100644 --- a/src/block/GlowLichen.php +++ b/src/block/GlowLichen.php @@ -121,7 +121,7 @@ class GlowLichen extends Transparent{ $changed = false; foreach($this->faces as $face){ - if(!$this->getSide($face)->getSupportType(Facing::opposite($face))->equals(SupportType::FULL())){ + if(!$this->getAdjacentSupportType($face)->equals(SupportType::FULL())){ unset($this->faces[$face]); $changed = true; } @@ -275,7 +275,7 @@ class GlowLichen extends Transparent{ private function getAvailableFaces() : array{ $faces = []; foreach(Facing::ALL as $face){ - if(!$this->hasFace($face) && $this->getSide($face)->getSupportType(Facing::opposite($face))->equals(SupportType::FULL())){ + if(!$this->hasFace($face) && $this->getAdjacentSupportType($face)->equals(SupportType::FULL())){ $faces[$face] = $face; } } diff --git a/src/block/ItemFrame.php b/src/block/ItemFrame.php index f14720fe4..21bc5c20a 100644 --- a/src/block/ItemFrame.php +++ b/src/block/ItemFrame.php @@ -163,18 +163,18 @@ class ItemFrame extends Flowable{ return true; } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return !$block->getSupportType($face)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return !$block->getAdjacentSupportType($face)->equals(SupportType::NONE()); } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $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(!$this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face)){ + if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){ return false; } diff --git a/src/block/Ladder.php b/src/block/Ladder.php index 66e5f28cd..83adada82 100644 --- a/src/block/Ladder.php +++ b/src/block/Ladder.php @@ -70,7 +70,7 @@ class Ladder extends Transparent{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face) && Facing::axis($face) !== Axis::Y){ + if($this->canBeSupportedAt($blockReplace, Facing::opposite($face)) && Facing::axis($face) !== Axis::Y){ $this->facing = $face; return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } @@ -79,12 +79,12 @@ class Ladder extends Transparent{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $this->facing)){ //Replace with common break method + if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing))){ //Replace with common break method $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return $block->getSupportType($face)->equals(SupportType::FULL()); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return $block->getAdjacentSupportType($face)->equals(SupportType::FULL()); } } diff --git a/src/block/Lantern.php b/src/block/Lantern.php index bc50c3cb6..8ebc8ba2c 100644 --- a/src/block/Lantern.php +++ b/src/block/Lantern.php @@ -77,22 +77,23 @@ class Lantern extends Transparent{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($blockReplace->getSide(Facing::UP), Facing::DOWN) && !$this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN), Facing::UP)){ + $downSupport = $this->canBeSupportedAt($blockReplace, Facing::DOWN); + if(!$downSupport && !$this->canBeSupportedAt($blockReplace, Facing::UP)){ return false; } - $this->hanging = ($face === Facing::DOWN || !$this->canBeSupportedBy($this->position->getWorld()->getBlock($blockReplace->getPosition()->down()), Facing::UP)); + $this->hanging = $face === Facing::DOWN || !$downSupport; return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } public function onNearbyBlockChange() : void{ $face = $this->hanging ? Facing::UP : Facing::DOWN; - if(!$this->canBeSupportedBy($this->getSide($face), Facing::opposite($face))){ + if(!$this->canBeSupportedAt($this, $face)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return $block->getSupportType($face)->hasCenterSupport(); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return $block->getAdjacentSupportType($face)->hasCenterSupport(); } } diff --git a/src/block/Lever.php b/src/block/Lever.php index 5d86ac7d2..e4b8c0811 100644 --- a/src/block/Lever.php +++ b/src/block/Lever.php @@ -66,7 +66,7 @@ class Lever extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face)){ + if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){ return false; } @@ -90,8 +90,7 @@ class Lever extends Flowable{ } public function onNearbyBlockChange() : void{ - $facing = $this->facing->getFacing(); - if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($facing)), $facing)){ + if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing->getFacing()))){ $this->position->getWorld()->useBreakOn($this->position); } } @@ -107,8 +106,8 @@ class Lever extends Flowable{ return true; } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return $block->getSupportType($face)->hasCenterSupport(); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return $block->getAdjacentSupportType($face)->hasCenterSupport(); } //TODO diff --git a/src/block/NetherVines.php b/src/block/NetherVines.php index ac075f667..c78000fa1 100644 --- a/src/block/NetherVines.php +++ b/src/block/NetherVines.php @@ -83,16 +83,13 @@ class NetherVines extends Flowable{ return true; } - private function getSupportFace() : int{ - return Facing::opposite($this->growthFace); - } - - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType($this->getSupportFace())->hasCenterSupport() || $block->hasSameTypeId($this); + private function canBeSupportedAt(Block $block) : bool{ + $supportBlock = $block->getSide(Facing::opposite($this->growthFace)); + return $supportBlock->getSupportType($this->growthFace)->hasCenterSupport() || $supportBlock->hasSameTypeId($this); } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide($this->getSupportFace()))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } @@ -109,7 +106,7 @@ class NetherVines extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($blockReplace->getSide($this->getSupportFace()))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } $this->age = mt_rand(0, self::MAX_AGE - 1); diff --git a/src/block/PressurePlate.php b/src/block/PressurePlate.php index 7f9403b74..4df0bf927 100644 --- a/src/block/PressurePlate.php +++ b/src/block/PressurePlate.php @@ -45,18 +45,18 @@ abstract class PressurePlate extends Transparent{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } return false; } - private function canBeSupportedBy(Block $block) : bool{ - return !$block->getSupportType(Facing::UP)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block) : bool{ + return !$block->getAdjacentSupportType(Facing::DOWN)->equals(SupportType::NONE()); } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } diff --git a/src/block/RedstoneComparator.php b/src/block/RedstoneComparator.php index 2158f1a84..8b436020b 100644 --- a/src/block/RedstoneComparator.php +++ b/src/block/RedstoneComparator.php @@ -85,7 +85,7 @@ class RedstoneComparator extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ if($player !== null){ $this->facing = Facing::opposite($player->getHorizontalFacing()); } @@ -102,13 +102,13 @@ class RedstoneComparator extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block) : bool{ - return !$block->getSupportType(Facing::UP)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block) : bool{ + return !$block->getAdjacentSupportType(Facing::DOWN)->equals(SupportType::NONE()); } //TODO: redstone functionality diff --git a/src/block/RedstoneRepeater.php b/src/block/RedstoneRepeater.php index d4f145238..518eeb9e5 100644 --- a/src/block/RedstoneRepeater.php +++ b/src/block/RedstoneRepeater.php @@ -68,7 +68,7 @@ class RedstoneRepeater extends Flowable{ } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ if($player !== null){ $this->facing = Facing::opposite($player->getHorizontalFacing()); } @@ -88,13 +88,13 @@ class RedstoneRepeater extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block) : bool{ - return !$block->getSupportType(Facing::UP)->equals(SupportType::NONE()); + private function canBeSupportedAt(Block $block) : bool{ + return !$block->getAdjacentSupportType(Facing::DOWN)->equals(SupportType::NONE()); } //TODO: redstone functionality diff --git a/src/block/RedstoneWire.php b/src/block/RedstoneWire.php index 022672b5d..167365f56 100644 --- a/src/block/RedstoneWire.php +++ b/src/block/RedstoneWire.php @@ -35,7 +35,7 @@ class RedstoneWire extends Flowable{ use AnalogRedstoneSignalEmitterTrait; public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } return false; @@ -49,13 +49,13 @@ class RedstoneWire extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::DOWN))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::UP)->hasCenterSupport(); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport(); } public function asItem() : Item{ diff --git a/src/block/SnowLayer.php b/src/block/SnowLayer.php index f2425455c..f561c8ff5 100644 --- a/src/block/SnowLayer.php +++ b/src/block/SnowLayer.php @@ -80,8 +80,8 @@ class SnowLayer extends Flowable implements Fallable{ return SupportType::NONE(); } - private function canBeSupportedBy(Block $b) : bool{ - return $b->getSupportType(Facing::UP)->equals(SupportType::FULL()); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::DOWN)->equals(SupportType::FULL()); } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ @@ -91,7 +91,7 @@ class SnowLayer extends Flowable implements Fallable{ } $this->layers = $blockReplace->layers + 1; } - if($this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN))){ + if($this->canBeSupportedAt($blockReplace)){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } diff --git a/src/block/SporeBlossom.php b/src/block/SporeBlossom.php index 73e31edf2..909932178 100644 --- a/src/block/SporeBlossom.php +++ b/src/block/SporeBlossom.php @@ -32,12 +32,12 @@ use pocketmine\world\BlockTransaction; final class SporeBlossom extends Flowable{ - private function canBeSupportedBy(Block $block) : bool{ - return $block->getSupportType(Facing::DOWN)->equals(SupportType::FULL()); + private function canBeSupportedAt(Block $block) : bool{ + return $block->getAdjacentSupportType(Facing::UP)->equals(SupportType::FULL()); } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(!$this->canBeSupportedBy($blockReplace->getSide(Facing::UP))){ + if(!$this->canBeSupportedAt($blockReplace)){ return false; } @@ -45,7 +45,7 @@ final class SporeBlossom extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->getSide(Facing::UP))){ + if(!$this->canBeSupportedAt($this)){ $this->position->getWorld()->useBreakOn($this->position); } } diff --git a/src/block/Torch.php b/src/block/Torch.php index 163c0d115..66b62bc19 100644 --- a/src/block/Torch.php +++ b/src/block/Torch.php @@ -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()); } } diff --git a/src/block/WallCoralFan.php b/src/block/WallCoralFan.php index 432dd5ddb..f9dece1cd 100644 --- a/src/block/WallCoralFan.php +++ b/src/block/WallCoralFan.php @@ -42,7 +42,7 @@ final class WallCoralFan extends BaseCoral{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ $axis = Facing::axis($face); - if(($axis !== Axis::X && $axis !== Axis::Z) || !$this->canBeSupportedBy($blockReplace->getSide(Facing::opposite($face)), $face)){ + if(($axis !== Axis::X && $axis !== Axis::Z) || !$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){ return false; } $this->facing = $face; @@ -54,15 +54,15 @@ final class WallCoralFan extends BaseCoral{ public function onNearbyBlockChange() : void{ $world = $this->position->getWorld(); - if(!$this->canBeSupportedBy($world->getBlock($this->position->getSide(Facing::opposite($this->facing))), $this->facing)){ + if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing))){ $world->useBreakOn($this->position); }else{ parent::onNearbyBlockChange(); } } - private function canBeSupportedBy(Block $block, int $face) : bool{ - return $block->getSupportType($face)->hasCenterSupport(); + private function canBeSupportedAt(Block $block, int $face) : bool{ + return $block->getAdjacentSupportType($face)->hasCenterSupport(); } public function asItem() : Item{