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