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