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