PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
Door.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\SupportType;
38
39class Door extends Transparent implements HorizontalFacing{
40 use HorizontalFacingTrait;
41
42 protected bool $top = false;
43 protected bool $hingeRight = false;
44 protected bool $open = false;
45
46 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
47 $w->enum($this->facing);
48 $w->bool($this->top);
49 $w->bool($this->hingeRight);
50 $w->bool($this->open);
51 }
52
53 public function readStateFromWorld() : Block{
54 parent::readStateFromWorld();
55
56 $this->collisionBoxes = null;
57
58 //copy door properties from other half
59 $other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
60 if($other instanceof Door && $other->hasSameTypeId($this)){
61 if($this->top){
62 $this->facing = $other->facing;
63 $this->open = $other->open;
64 }else{
65 $this->hingeRight = $other->hingeRight;
66 }
67 }
68
69 return $this;
70 }
71
72 public function isTop() : bool{ return $this->top; }
73
75 public function setTop(bool $top) : self{
76 $this->top = $top;
77 return $this;
78 }
79
80 public function isHingeRight() : bool{ return $this->hingeRight; }
81
83 public function setHingeRight(bool $hingeRight) : self{
84 $this->hingeRight = $hingeRight;
85 return $this;
86 }
87
88 public function isOpen() : bool{ return $this->open; }
89
91 public function setOpen(bool $open) : self{
92 $this->open = $open;
93 return $this;
94 }
95
96 public function isSolid() : bool{
97 return false;
98 }
99
100 protected function recalculateCollisionBoxes() : array{
101 //TODO: doors are 0.1825 blocks thick, instead of 0.1875 like JE (https://bugs.mojang.com/browse/MCPE-19214)
102 return [AxisAlignedBB::one()->trimmedCopy($this->open ? Facing::rotateY($this->facing->toFacing(), !$this->hingeRight) : $this->facing->toFacing(), 327 / 400)];
103 }
104
105 public function getSupportType(Facing $facing) : SupportType{
106 return SupportType::NONE;
107 }
108
109 public function onNearbyBlockChange() : void{
110 if(!$this->canBeSupportedAt($this) && !$this->getSide(Facing::DOWN) instanceof Door){ //Replace with common break method
111 $this->position->getWorld()->useBreakOn($this->position); //this will delete both halves if they exist
112 }
113 }
114
115 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
116 if($face === Facing::UP){
117 $blockUp = $this->getSide(Facing::UP);
118 if(!$blockUp->canBeReplaced() || !$this->canBeSupportedAt($blockReplace)){
119 return false;
120 }
121
122 if($player !== null){
123 //TODO: not sure if entities should use HorizontalFacingOption too
124 $this->facing = HorizontalFacingOption::fromFacing($player->getHorizontalFacing());
125 }
126
127 $realFacing = $this->facing->toFacing();
128 $next = $this->getSide(Facing::rotateY($realFacing, false));
129 $next2 = $this->getSide(Facing::rotateY($realFacing, true));
130
131 if($next->hasSameTypeId($this) || (!$next2->isTransparent() && $next->isTransparent())){ //Door hinge
132 $this->hingeRight = true;
133 }
134
135 $topHalf = clone $this;
136 $topHalf->top = true;
137
138 $tx->addBlock($blockReplace->position, $this)->addBlock($blockUp->position, $topHalf);
139 return true;
140 }
141
142 return false;
143 }
144
145 public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
146 $this->open = !$this->open;
147
148 $other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
149 $world = $this->position->getWorld();
150 if($other instanceof Door && $other->hasSameTypeId($this)){
151 $other->open = $this->open;
152 $world->setBlock($other->position, $other);
153 }
154
155 $world->setBlock($this->position, $this);
156 $world->addSound($this->position, new DoorSound());
157
158 return true;
159 }
160
161 public function getDrops(Item $item) : array{
162 if(!$this->top){
163 return parent::getDrops($item);
164 }
165
166 return [];
167 }
168
169 public function getAffectedBlocks() : array{
170 $other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
171 if($other->hasSameTypeId($this)){
172 return [$this, $other];
173 }
174 return parent::getAffectedBlocks();
175 }
176
177 private function canBeSupportedAt(Block $block) : bool{
178 return $block->getAdjacentSupportType(Facing::DOWN)->hasEdgeSupport();
179 }
180}
hasSameTypeId(Block $other)
Definition Block.php:187
getDrops(Item $item)
Definition Door.php:161
setTop(bool $top)
Definition Door.php:75
setHingeRight(bool $hingeRight)
Definition Door.php:83
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
Definition Door.php:115
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Door.php:145
getSupportType(Facing $facing)
Definition Door.php:105
setOpen(bool $open)
Definition Door.php:91
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Door.php:46
addBlock(Vector3 $pos, Block $state)