Files
PocketMine-MP/src/block/StraightOnlyRail.php
Dylan K. Taylor 6d4279671e A giant hack to cut down code needed for runtime block serialization by 50%
this also avoids repeated information and inconsistencies.
2022-07-18 18:25:41 +01:00

70 lines
2.1 KiB
PHP

<?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\RailConnectionInfo;
use pocketmine\data\bedrock\block\BlockLegacyMetadata;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use function array_keys;
use function implode;
/**
* Simple non-curvable rail.
*/
class StraightOnlyRail extends BaseRail{
private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
public function getRequiredStateDataBits() : int{ return 3; }
protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$w->straightOnlyRailShape($this->railShape);
}
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];
}
public function getShape() : int{ return $this->railShape; }
/** @return $this */
public function setShape(int $shape) : self{
if(!isset(RailConnectionInfo::CONNECTIONS[$shape])){
throw new \InvalidArgumentException("Invalid rail shape, must be one of " . implode(", ", array_keys(RailConnectionInfo::CONNECTIONS)));
}
$this->railShape = $shape;
return $this;
}
}