From 4fc3bc53f7fa171eb981dd3c480ee959e95b96ba Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Feb 2021 21:25:34 +0000 Subject: [PATCH] Clean up hierarchy of rails detector rail has fundamentally different functionality than activator and powered rails, so it's misleading to present the same APIs for both. detector rail's 'powered' state is better referred to as 'activated', since it means the detector rail is actually _producing_ power, and not _receiving_ power. --- src/block/ActivatorRail.php | 5 ++- src/block/DetectorRail.php | 24 ++++++++++++- src/block/PoweredRail.php | 5 ++- src/block/utils/PoweredByRedstoneTrait.php | 36 +++++++++++++++++++ .../RailPoweredByRedstoneTrait.php} | 8 ++--- 5 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/block/utils/PoweredByRedstoneTrait.php rename src/block/{RedstoneRail.php => utils/RailPoweredByRedstoneTrait.php} (90%) diff --git a/src/block/ActivatorRail.php b/src/block/ActivatorRail.php index 063cd6d25c..84538db7b6 100644 --- a/src/block/ActivatorRail.php +++ b/src/block/ActivatorRail.php @@ -23,7 +23,10 @@ declare(strict_types=1); namespace pocketmine\block; -class ActivatorRail extends RedstoneRail{ +use pocketmine\block\utils\RailPoweredByRedstoneTrait; + +class ActivatorRail extends BaseRail{ + use RailPoweredByRedstoneTrait; //TODO } diff --git a/src/block/DetectorRail.php b/src/block/DetectorRail.php index 91f29bcb5e..2ddc4a271a 100644 --- a/src/block/DetectorRail.php +++ b/src/block/DetectorRail.php @@ -23,7 +23,29 @@ declare(strict_types=1); namespace pocketmine\block; -class DetectorRail extends RedstoneRail{ +class DetectorRail extends BaseRail{ + protected bool $activated = false; + + public function isActivated() : bool{ return $this->activated; } + + /** @return $this */ + public function setActivated(bool $activated) : self{ + $this->activated = $activated; + return $this; + } + + protected function writeStateToMeta() : int{ + return parent::writeStateToMeta() | ($this->activated ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0); + } + + public function readStateFromData(int $id, int $stateMeta) : void{ + parent::readStateFromData($id, $stateMeta); + $this->activated = ($stateMeta & BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED) !== 0; + } + + protected function getConnectionsFromMeta(int $meta) : ?array{ + return self::CONNECTIONS[$meta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED] ?? null; + } //TODO } diff --git a/src/block/PoweredRail.php b/src/block/PoweredRail.php index 0a66be5be4..320227619d 100644 --- a/src/block/PoweredRail.php +++ b/src/block/PoweredRail.php @@ -23,5 +23,8 @@ declare(strict_types=1); namespace pocketmine\block; -class PoweredRail extends RedstoneRail{ +use pocketmine\block\utils\RailPoweredByRedstoneTrait; + +class PoweredRail extends BaseRail{ + use RailPoweredByRedstoneTrait; } diff --git a/src/block/utils/PoweredByRedstoneTrait.php b/src/block/utils/PoweredByRedstoneTrait.php new file mode 100644 index 0000000000..683437a4f9 --- /dev/null +++ b/src/block/utils/PoweredByRedstoneTrait.php @@ -0,0 +1,36 @@ +powered; } + + /** @return $this */ + public function setPowered(bool $powered) : self{ + $this->powered = $powered; + return $this; + } +} diff --git a/src/block/RedstoneRail.php b/src/block/utils/RailPoweredByRedstoneTrait.php similarity index 90% rename from src/block/RedstoneRail.php rename to src/block/utils/RailPoweredByRedstoneTrait.php index a9c1e5315b..0314c4bec7 100644 --- a/src/block/RedstoneRail.php +++ b/src/block/utils/RailPoweredByRedstoneTrait.php @@ -21,12 +21,12 @@ declare(strict_types=1); -namespace pocketmine\block; +namespace pocketmine\block\utils; -abstract class RedstoneRail extends BaseRail{ +use pocketmine\block\BlockLegacyMetadata; - /** @var bool */ - protected $powered = false; +trait RailPoweredByRedstoneTrait{ + use PoweredByRedstoneTrait; protected function writeStateToMeta() : int{ return parent::writeStateToMeta() | ($this->powered ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0);