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

@ -0,0 +1,64 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\RailConnectionInfo;
/**
* Simple non-curvable rail.
*/
class StraightOnlyRail extends BaseRail{
private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
public function readStateFromData(int $id, int $stateMeta) : void{
$railShape = $stateMeta & ~BlockLegacyMetadata::REDSTONE_RAIL_FLAG_POWERED;
if(!isset(RailConnectionInfo::CONNECTIONS[$railShape])){
throw new InvalidBlockStateException("No rail shape matches meta $stateMeta");
}
$this->railShape = $railShape;
}
protected function writeStateToMeta() : int{
//TODO: railShape won't be plain metadata in the future
return $this->railShape;
}
public function getStateBitmask() : int{
return 0b111;
}
protected function setShapeFromConnections(array $connections) : void{
$railShape = self::searchState($connections, RailConnectionInfo::CONNECTIONS);
if($railShape === null){
throw new \InvalidArgumentException("No rail shape matches these connections");
}
$this->railShape = $railShape;
}
protected function getCurrentShapeConnections() : array{
return RailConnectionInfo::CONNECTIONS[$this->railShape];
}
}