Refactor Rail handling to allow LSP-complaint shape handling

the reason there hasn't been any API until now is because of how inconvenient it was to expose a LSP-compliant API _and_ use the same base class for handling all the connection logic. This commit fixes that problem by abstracting shape handling away from BaseRail entirely, so that now it deals exclusively with connections. Deciding the shape of rail to use is now the job of the subclasses.
This commit is contained in:
Dylan K. Taylor
2021-07-22 17:35:10 +01:00
parent 2476f40cc6
commit e97234d420
8 changed files with 210 additions and 138 deletions

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
class DetectorRail extends BaseRail{
class DetectorRail extends StraightOnlyRail{
protected bool $activated = false;
public function isActivated() : bool{ return $this->activated; }
@ -34,22 +34,17 @@ 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;
public function readStateFromData(int $id, int $stateMeta) : void{
parent::readStateFromData($id, $stateMeta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED);
$this->activated = ($stateMeta & BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED) !== 0;
}
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 getCurrentShapeConnections() : array{
return self::CONNECTIONS[$this->railShape];
public function getStateBitmask() : int{
return 0b1111;
}
//TODO