PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
Rail.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
30use function array_keys;
31use function implode;
32
33class Rail extends BaseRail{
34
35 private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
36
37 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
38 $w->railShape($this->railShape);
39 }
40
41 protected function setShapeFromConnections(array $connections) : void{
42 $railShape = self::searchState($connections, RailConnectionInfo::CONNECTIONS) ?? self::searchState($connections, RailConnectionInfo::CURVE_CONNECTIONS);
43 if($railShape === null){
44 throw new \InvalidArgumentException("No rail shape matches these connections");
45 }
46 $this->railShape = $railShape;
47 }
48
49 protected function getCurrentShapeConnections() : array{
50 return RailConnectionInfo::CURVE_CONNECTIONS[$this->railShape] ?? RailConnectionInfo::CONNECTIONS[$this->railShape];
51 }
52
53 protected function getPossibleConnectionDirectionsOneConstraint(int $constraint) : array{
54 $possible = parent::getPossibleConnectionDirectionsOneConstraint($constraint);
55
56 if(($constraint & RailConnectionInfo::FLAG_ASCEND) === 0){
57 foreach([
58 Facing::NORTH,
59 Facing::SOUTH,
60 Facing::WEST,
61 Facing::EAST
62 ] as $d){
63 if($constraint !== $d){
64 $possible[$d] = true;
65 }
66 }
67 }
68
69 return $possible;
70 }
71
72 public function getShape() : int{ return $this->railShape; }
73
75 public function setShape(int $shape) : self{
76 if(!isset(RailConnectionInfo::CONNECTIONS[$shape]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$shape])){
77 throw new \InvalidArgumentException("Invalid shape, must be one of " . implode(", ", [...array_keys(RailConnectionInfo::CONNECTIONS), ...array_keys(RailConnectionInfo::CURVE_CONNECTIONS)]));
78 }
79 $this->railShape = $shape;
80 return $this;
81 }
82}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Rail.php:37
getCurrentShapeConnections()
Definition: Rail.php:49
setShapeFromConnections(array $connections)
Definition: Rail.php:41
getPossibleConnectionDirectionsOneConstraint(int $constraint)
Definition: Rail.php:53
setShape(int $shape)
Definition: Rail.php:75