Rail: store shape instead of connections

shape is what we'll eventually expose on the API.
This commit is contained in:
Dylan K. Taylor 2021-07-22 16:45:42 +01:00
parent 77138c5c06
commit 2476f40cc6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 35 additions and 23 deletions

View File

@ -71,24 +71,22 @@ abstract class BaseRail extends Flowable{
] ]
]; ];
/** @var int[] */ protected int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
protected array $connections = [];
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
if(count($this->connections) === 0){ return $this->railShape;
return BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
}
return $this->getMetaForState($this->connections);
} }
public function readStateFromData(int $id, int $stateMeta) : void{ public function readStateFromData(int $id, int $stateMeta) : void{
$connections = $this->getConnectionsFromMeta($stateMeta); $railShape = $this->readRailShapeFromMeta($stateMeta);
if($connections === null){ if($railShape === null){
throw new InvalidBlockStateException("Invalid rail type meta $stateMeta"); 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{ public function getStateBitmask() : int{
return 0b1111; return 0b1111;
} }
@ -129,7 +127,7 @@ abstract class BaseRail extends Flowable{
* *
* @throws \InvalidArgumentException if no state matches the given connections * @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); return self::searchState($connections, self::CONNECTIONS);
} }
@ -138,7 +136,7 @@ abstract class BaseRail extends Flowable{
* *
* @return int[] * @return int[]
*/ */
abstract protected function getConnectionsFromMeta(int $meta) : ?array; abstract protected function getCurrentShapeConnections() : array;
/** /**
* Returns all the directions this rail is already connected in. * Returns all the directions this rail is already connected in.
@ -150,7 +148,7 @@ abstract class BaseRail extends Flowable{
$connections = []; $connections = [];
/** @var int $connection */ /** @var int $connection */
foreach($this->connections as $connection){ foreach($this->getCurrentShapeConnections() as $connection){
$other = $this->getSide($connection & ~self::FLAG_ASCEND); $other = $this->getSide($connection & ~self::FLAG_ASCEND);
$otherConnection = Facing::opposite($connection & ~self::FLAG_ASCEND); $otherConnection = Facing::opposite($connection & ~self::FLAG_ASCEND);
@ -164,7 +162,7 @@ abstract class BaseRail extends Flowable{
if( if(
$other instanceof BaseRail and $other instanceof BaseRail and
in_array($otherConnection, $other->connections, true) in_array($otherConnection, $other->getCurrentShapeConnections(), true)
){ ){
$connections[] = $connection; $connections[] = $connection;
} }
@ -278,14 +276,14 @@ abstract class BaseRail extends Flowable{
throw new \InvalidArgumentException("Expected exactly 2 connections, got " . count($connections)); throw new \InvalidArgumentException("Expected exactly 2 connections, got " . count($connections));
} }
$this->connections = $connections; $this->railShape = $this->getShapeForConnections($connections);
} }
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
if($this->getSide(Facing::DOWN)->isTransparent()){ if($this->getSide(Facing::DOWN)->isTransparent()){
$this->pos->getWorld()->useBreakOn($this->pos); $this->pos->getWorld()->useBreakOn($this->pos);
}else{ }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()){ if(($connection & self::FLAG_ASCEND) !== 0 and $this->getSide($connection & ~self::FLAG_ASCEND)->isTransparent()){
$this->pos->getWorld()->useBreakOn($this->pos); $this->pos->getWorld()->useBreakOn($this->pos);
break; break;

View File

@ -34,6 +34,11 @@ class DetectorRail extends BaseRail{
return $this; 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{ protected function writeStateToMeta() : int{
return parent::writeStateToMeta() | ($this->activated ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0); 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; $this->activated = ($stateMeta & BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED) !== 0;
} }
protected function getConnectionsFromMeta(int $meta) : ?array{ protected function getCurrentShapeConnections() : array{
return self::CONNECTIONS[$meta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED] ?? null; return self::CONNECTIONS[$this->railShape];
} }
//TODO //TODO

View File

@ -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{ try{
return self::searchState($connections, self::CURVE_CONNECTIONS); return self::searchState($connections, self::CURVE_CONNECTIONS);
}catch(\InvalidArgumentException $e){ }catch(\InvalidArgumentException $e){
return parent::getMetaForState($connections); return parent::getShapeForConnections($connections);
} }
} }
protected function getConnectionsFromMeta(int $meta) : ?array{ protected function getCurrentShapeConnections() : array{
return self::CURVE_CONNECTIONS[$meta] ?? self::CONNECTIONS[$meta] ?? null; return self::CURVE_CONNECTIONS[$this->railShape] ?? self::CONNECTIONS[$this->railShape];
} }
protected function getPossibleConnectionDirectionsOneConstraint(int $constraint) : array{ protected function getPossibleConnectionDirectionsOneConstraint(int $constraint) : array{

View File

@ -28,6 +28,11 @@ use pocketmine\block\BlockLegacyMetadata;
trait RailPoweredByRedstoneTrait{ trait RailPoweredByRedstoneTrait{
use PoweredByRedstoneTrait; 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{ protected function writeStateToMeta() : int{
return parent::writeStateToMeta() | ($this->powered ? BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED : 0); 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; $this->powered = ($stateMeta & BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED) !== 0;
} }
protected function getConnectionsFromMeta(int $meta) : ?array{ protected function getCurrentShapeConnections() : array{
return self::CONNECTIONS[$meta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED] ?? null; return self::CONNECTIONS[$this->railShape];
} }
} }