PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
Stair.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
26use pocketmine\block\utils\HorizontalFacingTrait;
27use pocketmine\block\utils\StairShape;
28use pocketmine\block\utils\SupportType;
37
38class Stair extends Transparent{
39 use HorizontalFacingTrait;
40
41 protected bool $upsideDown = false;
42 protected StairShape $shape = StairShape::STRAIGHT;
43
44 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
45 $w->horizontalFacing($this->facing);
46 $w->bool($this->upsideDown);
47 }
48
49 public function readStateFromWorld() : Block{
50 parent::readStateFromWorld();
51
52 $this->collisionBoxes = null;
53
54 $clockwise = Facing::rotateY($this->facing, true);
55 if(($backFacing = $this->getPossibleCornerFacing(false)) !== null){
56 $this->shape = $backFacing === $clockwise ? StairShape::OUTER_RIGHT : StairShape::OUTER_LEFT;
57 }elseif(($frontFacing = $this->getPossibleCornerFacing(true)) !== null){
58 $this->shape = $frontFacing === $clockwise ? StairShape::INNER_RIGHT : StairShape::INNER_LEFT;
59 }else{
60 $this->shape = StairShape::STRAIGHT;
61 }
62
63 return $this;
64 }
65
66 public function isUpsideDown() : bool{ return $this->upsideDown; }
67
69 public function setUpsideDown(bool $upsideDown) : self{
70 $this->upsideDown = $upsideDown;
71 return $this;
72 }
73
74 public function getShape() : StairShape{ return $this->shape; }
75
77 public function setShape(StairShape $shape) : self{
78 $this->shape = $shape;
79 return $this;
80 }
81
82 protected function recalculateCollisionBoxes() : array{
83 $topStepFace = $this->upsideDown ? Facing::DOWN : Facing::UP;
84 $bbs = [
85 AxisAlignedBB::one()->trim($topStepFace, 0.5)
86 ];
87
88 $topStep = AxisAlignedBB::one()
89 ->trim(Facing::opposite($topStepFace), 0.5)
90 ->trim(Facing::opposite($this->facing), 0.5);
91
92 if($this->shape === StairShape::OUTER_LEFT || $this->shape === StairShape::OUTER_RIGHT){
93 $topStep->trim(Facing::rotateY($this->facing, $this->shape === StairShape::OUTER_LEFT), 0.5);
94 }elseif($this->shape === StairShape::INNER_LEFT || $this->shape === StairShape::INNER_RIGHT){
95 //add an extra cube
96 $bbs[] = AxisAlignedBB::one()
97 ->trim(Facing::opposite($topStepFace), 0.5)
98 ->trim($this->facing, 0.5) //avoid overlapping with main step
99 ->trim(Facing::rotateY($this->facing, $this->shape === StairShape::INNER_LEFT), 0.5);
100 }
101
102 $bbs[] = $topStep;
103
104 return $bbs;
105 }
106
107 public function getSupportType(int $facing) : SupportType{
108 if(
109 $facing === Facing::UP && $this->upsideDown ||
110 $facing === Facing::DOWN && !$this->upsideDown ||
111 ($facing === $this->facing && $this->shape !== StairShape::OUTER_LEFT && $this->shape !== StairShape::OUTER_RIGHT) ||
112 ($facing === Facing::rotate($this->facing, Axis::Y, false) && $this->shape === StairShape::INNER_LEFT) ||
113 ($facing === Facing::rotate($this->facing, Axis::Y, true) && $this->shape === StairShape::INNER_RIGHT)
114 ){
115 return SupportType::FULL;
116 }
117 return SupportType::NONE;
118 }
119
120 private function getPossibleCornerFacing(bool $oppositeFacing) : ?int{
121 $side = $this->getSide($oppositeFacing ? Facing::opposite($this->facing) : $this->facing);
122 return (
123 $side instanceof Stair &&
124 $side->upsideDown === $this->upsideDown &&
125 Facing::axis($side->facing) !== Facing::axis($this->facing) //perpendicular
126 ) ? $side->facing : null;
127 }
128
129 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
130 if($player !== null){
131 $this->facing = $player->getHorizontalFacing();
132 }
133 $this->upsideDown = (($clickVector->y > 0.5 && $face !== Facing::UP) || $face === Facing::DOWN);
134
135 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
136 }
137}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Stair.php:44
getSupportType(int $facing)
Definition: Stair.php:107
setShape(StairShape $shape)
Definition: Stair.php:77
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Stair.php:129
setUpsideDown(bool $upsideDown)
Definition: Stair.php:69
static rotateY(int $direction, bool $clockwise)
Definition: Facing.php:132