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,51 +23,46 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\RailConnectionInfo;
use pocketmine\math\Facing;
class Rail extends BaseRail{
/* extended meta values for regular rails, to allow curving */
private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
private const CURVE_CONNECTIONS = [
BlockLegacyMetadata::RAIL_CURVE_SOUTHEAST => [
Facing::SOUTH,
Facing::EAST
],
BlockLegacyMetadata::RAIL_CURVE_SOUTHWEST => [
Facing::SOUTH,
Facing::WEST
],
BlockLegacyMetadata::RAIL_CURVE_NORTHWEST => [
Facing::NORTH,
Facing::WEST
],
BlockLegacyMetadata::RAIL_CURVE_NORTHEAST => [
Facing::NORTH,
Facing::EAST
]
];
protected function readRailShapeFromMeta(int $stateMeta) : ?int{
return isset(self::CURVE_CONNECTIONS[$stateMeta]) || isset(self::CONNECTIONS[$stateMeta]) ? $stateMeta : null;
public function readStateFromData(int $id, int $stateMeta) : void{
if(!isset(RailConnectionInfo::CONNECTIONS[$stateMeta]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$stateMeta])){
throw new InvalidBlockStateException("No rail shape matches metadata $stateMeta");
}
$this->railShape = $stateMeta;
}
protected function getShapeForConnections(array $connections) : int{
try{
return self::searchState($connections, self::CURVE_CONNECTIONS);
}catch(\InvalidArgumentException $e){
return parent::getShapeForConnections($connections);
protected function writeStateToMeta() : int{
//TODO: railShape won't be plain metadata in future
return $this->railShape;
}
public function getStateBitmask() : int{
return 0b1111;
}
protected function setShapeFromConnections(array $connections) : void{
$railShape = self::searchState($connections, RailConnectionInfo::CONNECTIONS) ?? self::searchState($connections, RailConnectionInfo::CURVE_CONNECTIONS);
if($railShape === null){
throw new \InvalidArgumentException("No rail shape matches these connections");
}
$this->railShape = $railShape;
}
protected function getCurrentShapeConnections() : array{
return self::CURVE_CONNECTIONS[$this->railShape] ?? self::CONNECTIONS[$this->railShape];
return RailConnectionInfo::CURVE_CONNECTIONS[$this->railShape] ?? RailConnectionInfo::CONNECTIONS[$this->railShape];
}
protected function getPossibleConnectionDirectionsOneConstraint(int $constraint) : array{
$possible = parent::getPossibleConnectionDirectionsOneConstraint($constraint);
if(($constraint & self::FLAG_ASCEND) === 0){
if(($constraint & RailConnectionInfo::FLAG_ASCEND) === 0){
foreach([
Facing::NORTH,
Facing::SOUTH,