From 2476f40cc68eebc389597ca787a55f053544bc9d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 22 Jul 2021 16:45:42 +0100 Subject: [PATCH] Rail: store shape instead of connections shape is what we'll eventually expose on the API. --- src/block/BaseRail.php | 28 +++++++++---------- src/block/DetectorRail.php | 9 ++++-- src/block/Rail.php | 12 +++++--- .../utils/RailPoweredByRedstoneTrait.php | 9 ++++-- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/block/BaseRail.php b/src/block/BaseRail.php index bafcfe2a4..aeb62b384 100644 --- a/src/block/BaseRail.php +++ b/src/block/BaseRail.php @@ -71,24 +71,22 @@ abstract class BaseRail extends Flowable{ ] ]; - /** @var int[] */ - protected array $connections = []; + protected int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH; protected function writeStateToMeta() : int{ - if(count($this->connections) === 0){ - return BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH; - } - return $this->getMetaForState($this->connections); + return $this->railShape; } public function readStateFromData(int $id, int $stateMeta) : void{ - $connections = $this->getConnectionsFromMeta($stateMeta); - if($connections === null){ + $railShape = $this->readRailShapeFromMeta($stateMeta); + if($railShape === null){ throw new InvalidBlockStateException("Invalid rail type meta $stateMeta"); } - $this->connections = $connections; + $this->railShape = $railShape; } + abstract protected function readRailShapeFromMeta(int $stateMeta) : ?int; + public function getStateBitmask() : int{ return 0b1111; } @@ -129,7 +127,7 @@ abstract class BaseRail extends Flowable{ * * @throws \InvalidArgumentException if no state matches the given connections */ - protected function getMetaForState(array $connections) : int{ + protected function getShapeForConnections(array $connections) : int{ return self::searchState($connections, self::CONNECTIONS); } @@ -138,7 +136,7 @@ abstract class BaseRail extends Flowable{ * * @return int[] */ - abstract protected function getConnectionsFromMeta(int $meta) : ?array; + abstract protected function getCurrentShapeConnections() : array; /** * Returns all the directions this rail is already connected in. @@ -150,7 +148,7 @@ abstract class BaseRail extends Flowable{ $connections = []; /** @var int $connection */ - foreach($this->connections as $connection){ + foreach($this->getCurrentShapeConnections() as $connection){ $other = $this->getSide($connection & ~self::FLAG_ASCEND); $otherConnection = Facing::opposite($connection & ~self::FLAG_ASCEND); @@ -164,7 +162,7 @@ abstract class BaseRail extends Flowable{ if( $other instanceof BaseRail and - in_array($otherConnection, $other->connections, true) + in_array($otherConnection, $other->getCurrentShapeConnections(), true) ){ $connections[] = $connection; } @@ -278,14 +276,14 @@ abstract class BaseRail extends Flowable{ throw new \InvalidArgumentException("Expected exactly 2 connections, got " . count($connections)); } - $this->connections = $connections; + $this->railShape = $this->getShapeForConnections($connections); } public function onNearbyBlockChange() : void{ if($this->getSide(Facing::DOWN)->isTransparent()){ $this->pos->getWorld()->useBreakOn($this->pos); }else{ - foreach($this->connections as $connection){ + foreach($this->getCurrentShapeConnections() as $connection){ if(($connection & self::FLAG_ASCEND) !== 0 and $this->getSide($connection & ~self::FLAG_ASCEND)->isTransparent()){ $this->pos->getWorld()->useBreakOn($this->pos); break; diff --git a/src/block/DetectorRail.php b/src/block/DetectorRail.php index 2ddc4a271..572cb63cd 100644 --- a/src/block/DetectorRail.php +++ b/src/block/DetectorRail.php @@ -34,6 +34,11 @@ class DetectorRail extends BaseRail{ return $this; } + protected function readRailShapeFromMeta(int $stateMeta) : ?int{ + $stateMeta &= ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED; + return isset(self::CONNECTIONS[$stateMeta]) ? $stateMeta : null; + } + protected function writeStateToMeta() : int{ return parent::writeStateToMeta() | ($this->activated ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0); } @@ -43,8 +48,8 @@ class DetectorRail extends BaseRail{ $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; + protected function getCurrentShapeConnections() : array{ + return self::CONNECTIONS[$this->railShape]; } //TODO diff --git a/src/block/Rail.php b/src/block/Rail.php index c906470ca..82968dcdf 100644 --- a/src/block/Rail.php +++ b/src/block/Rail.php @@ -48,16 +48,20 @@ class Rail extends BaseRail{ ] ]; - protected function getMetaForState(array $connections) : int{ + protected function readRailShapeFromMeta(int $stateMeta) : ?int{ + return isset(self::CURVE_CONNECTIONS[$stateMeta]) || isset(self::CONNECTIONS[$stateMeta]) ? $stateMeta : null; + } + + protected function getShapeForConnections(array $connections) : int{ try{ return self::searchState($connections, self::CURVE_CONNECTIONS); }catch(\InvalidArgumentException $e){ - return parent::getMetaForState($connections); + return parent::getShapeForConnections($connections); } } - protected function getConnectionsFromMeta(int $meta) : ?array{ - return self::CURVE_CONNECTIONS[$meta] ?? self::CONNECTIONS[$meta] ?? null; + protected function getCurrentShapeConnections() : array{ + return self::CURVE_CONNECTIONS[$this->railShape] ?? self::CONNECTIONS[$this->railShape]; } protected function getPossibleConnectionDirectionsOneConstraint(int $constraint) : array{ diff --git a/src/block/utils/RailPoweredByRedstoneTrait.php b/src/block/utils/RailPoweredByRedstoneTrait.php index 0314c4bec..9845eb5b7 100644 --- a/src/block/utils/RailPoweredByRedstoneTrait.php +++ b/src/block/utils/RailPoweredByRedstoneTrait.php @@ -28,6 +28,11 @@ use pocketmine\block\BlockLegacyMetadata; trait RailPoweredByRedstoneTrait{ use PoweredByRedstoneTrait; + protected function readRailShapeFromMeta(int $stateMeta) : ?int{ + $stateMeta &= ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED; + return isset(self::CONNECTIONS[$stateMeta]) ? $stateMeta : null; + } + protected function writeStateToMeta() : int{ return parent::writeStateToMeta() | ($this->powered ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0); } @@ -37,7 +42,7 @@ trait RailPoweredByRedstoneTrait{ $this->powered = ($stateMeta & BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED) !== 0; } - protected function getConnectionsFromMeta(int $meta) : ?array{ - return self::CONNECTIONS[$meta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED] ?? null; + protected function getCurrentShapeConnections() : array{ + return self::CONNECTIONS[$this->railShape]; } }