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