PocketMine-MP 5.39.3 git-66148f13a91e4af6778ba9f200ca25ad8a04a584
Loading...
Searching...
No Matches
BaseSign.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\Sign as TileSign;
27use pocketmine\block\utils\DyeColor;
29use pocketmine\block\utils\SupportType;
31use pocketmine\block\utils\WoodType;
32use pocketmine\block\utils\WoodTypeTrait;
45use function abs;
46use function array_map;
47use function assert;
48use function atan2;
49use function fmod;
50use function rad2deg;
51use function strlen;
52
53abstract class BaseSign extends Transparent implements WoodMaterial{
54 use WoodTypeTrait;
55
56 protected SignText $frontText;
57 protected SignText $backText;
58 private bool $waxed = false;
59
60 protected ?int $editorEntityRuntimeId = null;
61
63 private \Closure $asItemCallback;
64
68 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback){
69 $this->woodType = $woodType;
70 parent::__construct($idInfo, $name, $typeInfo);
71 $this->frontText = new SignText();
72 $this->backText = new SignText();
73 $this->asItemCallback = $asItemCallback;
74 }
75
76 public function readStateFromWorld() : Block{
77 parent::readStateFromWorld();
78 $tile = $this->position->getWorld()->getTile($this->position);
79 if($tile instanceof TileSign){
80 $this->frontText = $tile->getFrontText();
81 $this->backText = $tile->getBackText();
82 $this->waxed = $tile->isWaxed();
83 $this->editorEntityRuntimeId = $tile->getEditorEntityRuntimeId();
84 }
85
86 return $this;
87 }
88
89 public function writeStateToWorld() : void{
90 parent::writeStateToWorld();
91 $tile = $this->position->getWorld()->getTile($this->position);
92 assert($tile instanceof TileSign);
93 $tile->setFrontText($this->frontText);
94 $tile->setBackText($this->backText);
95 $tile->setWaxed($this->waxed);
96 $tile->setEditorEntityRuntimeId($this->editorEntityRuntimeId);
97 }
98
99 public function isSolid() : bool{
100 return false;
101 }
102
103 public function getMaxStackSize() : int{
104 return 16;
105 }
106
107 protected function recalculateCollisionBoxes() : array{
108 return [];
109 }
110
111 public function getSupportType(Facing $facing) : SupportType{
112 return SupportType::NONE;
113 }
114
115 abstract protected function getSupportingFace() : Facing;
116
117 public function onNearbyBlockChange() : void{
118 if($this->getSide($this->getSupportingFace())->getTypeId() === BlockTypeIds::AIR){
119 $this->position->getWorld()->useBreakOn($this->position);
120 }
121 }
122
123 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
124 if($player !== null){
125 $this->editorEntityRuntimeId = $player->getId();
126 }
127 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
128 }
129
130 public function onPostPlace() : void{
131 $player = $this->editorEntityRuntimeId !== null ?
132 $this->position->getWorld()->getEntity($this->editorEntityRuntimeId) :
133 null;
134 //TODO: HACK! We really shouldn't be keeping disconnected players (and generally flagged-for-despawn entities)
135 //in the world's entity table, but changing that is too risky for a hotfix. This workaround will do for now.
136 if($player instanceof Player && $player->isConnected()){
137 $player->openSignEditor($this->position);
138 }
139 }
140
141 private function doSignChange(SignText $newText, Player $player, Item $item, bool $frontFace) : bool{
142 $ev = new SignChangeEvent($this, $player, $newText, $frontFace);
143 $ev->call();
144 if(!$ev->isCancelled()){
145 $this->setFaceText($frontFace, $ev->getNewText());
146 $this->position->getWorld()->setBlock($this->position, $this);
147 $item->pop();
148 return true;
149 }
150
151 return false;
152 }
153
154 private function changeSignGlowingState(bool $glowing, Player $player, Item $item, bool $frontFace) : bool{
155 $text = $this->getFaceText($frontFace);
156 if($text->isGlowing() !== $glowing && $this->doSignChange(new SignText($text->getLines(), $text->getBaseColor(), $glowing), $player, $item, $frontFace)){
157 $this->position->getWorld()->addSound($this->position, new InkSacUseSound());
158 return true;
159 }
160 return false;
161 }
162
163 private function wax(Player $player, Item $item) : bool{
164 if($this->waxed){
165 return false;
166 }
167
168 $this->waxed = true;
169 $this->position->getWorld()->setBlock($this->position, $this);
170 $item->pop();
171
172 return true;
173 }
174
175 public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
176 if($player === null){
177 return false;
178 }
179 if($this->waxed){
180 return true;
181 }
182
183 $frontFace = $this->interactsFront($this->getHitboxCenter(), $player->getPosition(), $this->getFacingDegrees());
184
185 $dyeColor = $item instanceof Dye ? $item->getColor() : match($item->getTypeId()){
186 ItemTypeIds::BONE_MEAL => DyeColor::WHITE,
187 ItemTypeIds::LAPIS_LAZULI => DyeColor::BLUE,
188 ItemTypeIds::COCOA_BEANS => DyeColor::BROWN,
189 default => null
190 };
191 if($dyeColor !== null){
192 $color = $dyeColor === DyeColor::BLACK ? new Color(0, 0, 0) : $dyeColor->getRgbValue();
193 $text = $this->getFaceText($frontFace);
194 if(
195 $color->toARGB() !== $text->getBaseColor()->toARGB() &&
196 $this->doSignChange(new SignText($text->getLines(), $color, $text->isGlowing()), $player, $item, $frontFace)
197 ){
198 $this->position->getWorld()->addSound($this->position, new DyeUseSound());
199 return true;
200 }
201 }elseif(match($item->getTypeId()){
202 ItemTypeIds::INK_SAC => $this->changeSignGlowingState(false, $player, $item, $frontFace),
203 ItemTypeIds::GLOW_INK_SAC => $this->changeSignGlowingState(true, $player, $item, $frontFace),
204 ItemTypeIds::HONEYCOMB => $this->wax($player, $item),
205 default => false
206 }){
207 return true;
208 }
209
210 $player->openSignEditor($this->position, $frontFace);
211
212 return true;
213 }
214
215 private function interactsFront(Vector3 $hitboxCenter, Vector3 $playerPosition, float $signFacingDegrees) : bool{
216 $playerCenterDiffX = $playerPosition->x - $hitboxCenter->x;
217 $playerCenterDiffZ = $playerPosition->z - $hitboxCenter->z;
218
219 $f1 = rad2deg(atan2($playerCenterDiffZ, $playerCenterDiffX)) - 90.0;
220
221 $rotationDiff = $signFacingDegrees - $f1;
222 $rotation = fmod($rotationDiff + 180.0, 360.0) - 180.0; // Normalize to [-180, 180]
223 return abs($rotation) <= 90.0;
224 }
225
229 protected function getHitboxCenter() : Vector3{
230 return $this->position->add(0.5, 0.5, 0.5);
231 }
232
233 abstract protected function getFacingDegrees() : float;
234
235 public function getFaceText(bool $frontFace) : SignText{
236 return $frontFace ? $this->frontText : $this->backText;
237 }
238
240 public function setFaceText(bool $frontFace, SignText $text) : self{
241 $frontFace ? $this->frontText = $text : $this->backText = $text;
242 return $this;
243 }
244
248 public function isWaxed() : bool{ return $this->waxed; }
249
251 public function setWaxed(bool $waxed) : self{
252 $this->waxed = $waxed;
253 return $this;
254 }
255
261 public function getEditorEntityRuntimeId() : ?int{ return $this->editorEntityRuntimeId; }
262
264 public function setEditorEntityRuntimeId(?int $editorEntityRuntimeId) : self{
265 $this->editorEntityRuntimeId = $editorEntityRuntimeId;
266 return $this;
267 }
268
275 public function updateFaceText(Player $author, bool $frontFace, SignText $text) : bool{
276 $size = 0;
277 foreach($text->getLines() as $line){
278 $size += strlen($line);
279 }
280 if($size > 1000){
281 throw new \UnexpectedValueException($author->getName() . " tried to write $size bytes of text onto a sign (bigger than max 1000)");
282 }
283 $oldText = $this->getFaceText($frontFace);
284 $ev = new SignChangeEvent($this, $author, new SignText(array_map(function(string $line) : string{
285 return TextFormat::clean($line, false);
286 }, $text->getLines()), $oldText->getBaseColor(), $oldText->isGlowing()), $frontFace);
287 if($this->waxed || $this->editorEntityRuntimeId !== $author->getId()){
288 $ev->cancel();
289 }
290 $ev->call();
291 if(!$ev->isCancelled()){
292 $this->setFaceText($frontFace, $ev->getNewText());
293 $this->setEditorEntityRuntimeId(null);
294 $this->position->getWorld()->setBlock($this->position, $this);
295 return true;
296 }
297
298 return false;
299 }
300
301 public function asItem() : Item{
302 return ($this->asItemCallback)();
303 }
304
305 public function getFuelTime() : int{
306 return $this->woodType->isFlammable() ? 200 : 0;
307 }
308}
setEditorEntityRuntimeId(?int $editorEntityRuntimeId)
Definition BaseSign.php:264
getSupportType(Facing $facing)
Definition BaseSign.php:111
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition BaseSign.php:175
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
Definition BaseSign.php:123
updateFaceText(Player $author, bool $frontFace, SignText $text)
Definition BaseSign.php:275
setFaceText(bool $frontFace, SignText $text)
Definition BaseSign.php:240
__construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback)
Definition BaseSign.php:68
openSignEditor(Vector3 $position, bool $frontFace=true)
Definition Player.php:2931