mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Rail: store shape instead of connections
shape is what we'll eventually expose on the API.
This commit is contained in:
parent
77138c5c06
commit
2476f40cc6
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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{
|
||||
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user