From 8a3df1212a3f1632715f3f32f68b540ec861bc8e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 27 Apr 2021 21:28:26 +0100 Subject: [PATCH] Added a trait for blocks which face opposite their placing player --- src/block/CarvedPumpkin.php | 14 +----- src/block/ChemistryTable.php | 10 +---- src/block/Chest.php | 11 +---- src/block/EndPortalFrame.php | 13 +----- src/block/EnderChest.php | 10 +---- src/block/Furnace.php | 12 +---- src/block/GlazedTerracotta.php | 15 +------ .../utils/FacesOppositePlacingPlayerTrait.php | 45 +++++++++++++++++++ 8 files changed, 59 insertions(+), 71 deletions(-) create mode 100644 src/block/utils/FacesOppositePlacingPlayerTrait.php diff --git a/src/block/CarvedPumpkin.php b/src/block/CarvedPumpkin.php index e5284bf49..d4513a08d 100644 --- a/src/block/CarvedPumpkin.php +++ b/src/block/CarvedPumpkin.php @@ -24,14 +24,11 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\utils\BlockDataSerializer; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\HorizontalFacingTrait; -use pocketmine\item\Item; -use pocketmine\math\Facing; -use pocketmine\math\Vector3; -use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class CarvedPumpkin extends Opaque{ + use FacesOppositePlacingPlayerTrait; use HorizontalFacingTrait; public function readStateFromData(int $id, int $stateMeta) : void{ @@ -45,11 +42,4 @@ class CarvedPumpkin extends Opaque{ public function getStateBitmask() : int{ return 0b11; } - - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } } diff --git a/src/block/ChemistryTable.php b/src/block/ChemistryTable.php index e33159762..3ec37718f 100644 --- a/src/block/ChemistryTable.php +++ b/src/block/ChemistryTable.php @@ -24,14 +24,15 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\utils\BlockDataSerializer; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\item\Item; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; final class ChemistryTable extends Opaque{ + use FacesOppositePlacingPlayerTrait; use HorizontalFacingTrait; public function readStateFromData(int $id, int $stateMeta) : void{ @@ -46,13 +47,6 @@ final class ChemistryTable extends Opaque{ return 0b0011; } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } - public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ //TODO return false; diff --git a/src/block/Chest.php b/src/block/Chest.php index 85feebc24..c48d3d1fa 100644 --- a/src/block/Chest.php +++ b/src/block/Chest.php @@ -24,15 +24,16 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\tile\Chest as TileChest; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\NormalHorizontalFacingInMetadataTrait; use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class Chest extends Transparent{ + use FacesOppositePlacingPlayerTrait; use NormalHorizontalFacingInMetadataTrait; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ @@ -47,14 +48,6 @@ class Chest extends Transparent{ return [AxisAlignedBB::one()->contract(0.025, 0, 0.025)->trim(Facing::UP, 0.05)]; } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } - public function onPostPlace() : void{ $tile = $this->pos->getWorld()->getTile($this->pos); if($tile instanceof TileChest){ diff --git a/src/block/EndPortalFrame.php b/src/block/EndPortalFrame.php index 221b206d0..2a553a2a9 100644 --- a/src/block/EndPortalFrame.php +++ b/src/block/EndPortalFrame.php @@ -24,15 +24,13 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\utils\BlockDataSerializer; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\HorizontalFacingTrait; -use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; -use pocketmine\math\Vector3; -use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class EndPortalFrame extends Opaque{ + use FacesOppositePlacingPlayerTrait; use HorizontalFacingTrait; /** @var bool */ @@ -73,11 +71,4 @@ class EndPortalFrame extends Opaque{ protected function recalculateCollisionBoxes() : array{ return [AxisAlignedBB::one()->trim(Facing::UP, 3 / 16)]; } - - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } } diff --git a/src/block/EnderChest.php b/src/block/EnderChest.php index 61868cbf5..69dc97a27 100644 --- a/src/block/EnderChest.php +++ b/src/block/EnderChest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\tile\EnderChest as TileEnderChest; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\NormalHorizontalFacingInMetadataTrait; use pocketmine\item\Item; use pocketmine\item\ToolTier; @@ -31,9 +32,9 @@ use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class EnderChest extends Transparent{ + use FacesOppositePlacingPlayerTrait; use NormalHorizontalFacingInMetadataTrait; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ @@ -52,13 +53,6 @@ class EnderChest extends Transparent{ return [AxisAlignedBB::one()->contract(0.025, 0, 0.025)->trim(Facing::UP, 0.05)]; } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } - public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($player instanceof Player){ $enderChest = $this->pos->getWorld()->getTile($this->pos); diff --git a/src/block/Furnace.php b/src/block/Furnace.php index bcf29c1e9..5977da582 100644 --- a/src/block/Furnace.php +++ b/src/block/Furnace.php @@ -24,15 +24,15 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\tile\Furnace as TileFurnace; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\NormalHorizontalFacingInMetadataTrait; use pocketmine\item\Item; use pocketmine\item\ToolTier; -use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class Furnace extends Opaque{ + use FacesOppositePlacingPlayerTrait; use NormalHorizontalFacingInMetadataTrait { readStateFromData as readFacingStateFromData; } @@ -72,14 +72,6 @@ class Furnace extends Opaque{ return $this; } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } - public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($player instanceof Player){ $furnace = $this->pos->getWorld()->getTile($this->pos); diff --git a/src/block/GlazedTerracotta.php b/src/block/GlazedTerracotta.php index 6513149ba..5e81b2cc8 100644 --- a/src/block/GlazedTerracotta.php +++ b/src/block/GlazedTerracotta.php @@ -23,26 +23,15 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; use pocketmine\block\utils\NormalHorizontalFacingInMetadataTrait; -use pocketmine\item\Item; use pocketmine\item\ToolTier; -use pocketmine\math\Facing; -use pocketmine\math\Vector3; -use pocketmine\player\Player; -use pocketmine\world\BlockTransaction; class GlazedTerracotta extends Opaque{ + use FacesOppositePlacingPlayerTrait; use NormalHorizontalFacingInMetadataTrait; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(1.4, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel())); } - - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($player !== null){ - $this->facing = Facing::opposite($player->getHorizontalFacing()); - } - - return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); - } } diff --git a/src/block/utils/FacesOppositePlacingPlayerTrait.php b/src/block/utils/FacesOppositePlacingPlayerTrait.php new file mode 100644 index 000000000..1f566ca30 --- /dev/null +++ b/src/block/utils/FacesOppositePlacingPlayerTrait.php @@ -0,0 +1,45 @@ +facing = Facing::opposite($player->getHorizontalFacing()); + } + return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); + } +}