PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
Bed.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\tile\Bed as TileBed;
27use pocketmine\block\utils\ColoredTrait;
28use pocketmine\block\utils\DyeColor;
29use pocketmine\block\utils\HorizontalFacingTrait;
30use pocketmine\block\utils\SupportType;
43
44class Bed extends Transparent{
45 use ColoredTrait;
46 use HorizontalFacingTrait;
47
48 protected bool $occupied = false;
49 protected bool $head = false;
50
51 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
52 $w->horizontalFacing($this->facing);
53 $w->bool($this->occupied);
54 $w->bool($this->head);
55 }
56
57 public function readStateFromWorld() : Block{
58 parent::readStateFromWorld();
59 //read extra state information from the tile - this is an ugly hack
60 $tile = $this->position->getWorld()->getTile($this->position);
61 if($tile instanceof TileBed){
62 $this->color = $tile->getColor();
63 }else{
64 $this->color = DyeColor::RED; //legacy pre-1.1 beds don't have tiles
65 }
66
67 return $this;
68 }
69
70 public function writeStateToWorld() : void{
71 parent::writeStateToWorld();
72 //extra block properties storage hack
73 $tile = $this->position->getWorld()->getTile($this->position);
74 if($tile instanceof TileBed){
75 $tile->setColor($this->color);
76 }
77 }
78
79 protected function recalculateCollisionBoxes() : array{
80 return [AxisAlignedBB::one()->trim(Facing::UP, 7 / 16)];
81 }
82
83 public function getSupportType(int $facing) : SupportType{
84 return SupportType::NONE;
85 }
86
87 public function isHeadPart() : bool{
88 return $this->head;
89 }
90
92 public function setHead(bool $head) : self{
93 $this->head = $head;
94 return $this;
95 }
96
97 public function isOccupied() : bool{
98 return $this->occupied;
99 }
100
102 public function setOccupied(bool $occupied = true) : self{
103 $this->occupied = $occupied;
104 return $this;
105 }
106
107 private function getOtherHalfSide() : int{
108 return $this->head ? Facing::opposite($this->facing) : $this->facing;
109 }
110
111 public function getOtherHalf() : ?Bed{
112 $other = $this->getSide($this->getOtherHalfSide());
113 if($other instanceof Bed && $other->head !== $this->head && $other->facing === $this->facing){
114 return $other;
115 }
116
117 return null;
118 }
119
120 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
121 if($player !== null){
122 $other = $this->getOtherHalf();
123 $playerPos = $player->getPosition();
124 if($other === null){
125 $player->sendMessage(TextFormat::GRAY . "This bed is incomplete");
126
127 return true;
128 }elseif($playerPos->distanceSquared($this->position) > 4 && $playerPos->distanceSquared($other->position) > 4){
129 $player->sendMessage(KnownTranslationFactory::tile_bed_tooFar()->prefix(TextFormat::GRAY));
130 return true;
131 }
132
133 $time = $this->position->getWorld()->getTimeOfDay();
134
135 $isNight = ($time >= World::TIME_NIGHT && $time < World::TIME_SUNRISE);
136
137 if(!$isNight){
138 $player->sendMessage(KnownTranslationFactory::tile_bed_noSleep()->prefix(TextFormat::GRAY));
139
140 return true;
141 }
142
143 $b = ($this->isHeadPart() ? $this : $other);
144
145 if($b->occupied){
146 $player->sendMessage(KnownTranslationFactory::tile_bed_occupied()->prefix(TextFormat::GRAY));
147
148 return true;
149 }
150
151 $player->sleepOn($b->position);
152 }
153
154 return true;
155
156 }
157
158 public function onNearbyBlockChange() : void{
159 if(!$this->head && ($other = $this->getOtherHalf()) !== null && $other->occupied !== $this->occupied){
160 $this->occupied = $other->occupied;
161 $this->position->getWorld()->setBlock($this->position, $this);
162 }
163 }
164
165 public function onEntityLand(Entity $entity) : ?float{
166 if($entity instanceof Living && $entity->isSneaking()){
167 return null;
168 }
169 $entity->fallDistance *= 0.5;
170 return $entity->getMotion()->y * -3 / 4; // 2/3 in Java, according to the wiki
171 }
172
173 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
174 if($this->canBeSupportedAt($blockReplace)){
175 $this->facing = $player !== null ? $player->getHorizontalFacing() : Facing::NORTH;
176
177 $next = $this->getSide($this->getOtherHalfSide());
178 if($next->canBeReplaced() && $this->canBeSupportedAt($next)){
179 $nextState = clone $this;
180 $nextState->head = true;
181 $tx->addBlock($blockReplace->position, $this)->addBlock($next->position, $nextState);
182 return true;
183 }
184 }
185
186 return false;
187 }
188
189 public function getDrops(Item $item) : array{
190 if($this->head){
191 return parent::getDrops($item);
192 }
193
194 return [];
195 }
196
197 public function getAffectedBlocks() : array{
198 if(($other = $this->getOtherHalf()) !== null){
199 return [$this, $other];
200 }
201
202 return parent::getAffectedBlocks();
203 }
204
205 private function canBeSupportedAt(Block $block) : bool{
206 return $block->getAdjacentSupportType(Facing::DOWN) !== SupportType::NONE;
207 }
208
209 public function getMaxStackSize() : int{ return 1; }
210}
onEntityLand(Entity $entity)
Definition Bed.php:165
getDrops(Item $item)
Definition Bed.php:189
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Bed.php:51
setHead(bool $head)
Definition Bed.php:92
recalculateCollisionBoxes()
Definition Bed.php:79
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition Bed.php:173
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Bed.php:120
getSupportType(int $facing)
Definition Bed.php:83
setOccupied(bool $occupied=true)
Definition Bed.php:102
addBlock(Vector3 $pos, Block $state)