PocketMine-MP 5.35.1 git-05a71d8cc5185aa9e46ef5f9754bb862464c13e0
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;
44use function abs;
45use function array_map;
46use function assert;
47use function atan2;
48use function fmod;
49use function rad2deg;
50use function strlen;
51
52abstract class BaseSign extends Transparent implements WoodMaterial{
53 use WoodTypeTrait;
54
55 protected SignText $text; //TODO: rename this (BC break)
56 protected SignText $backText;
57 private bool $waxed = false;
58
59 protected ?int $editorEntityRuntimeId = null;
60
62 private \Closure $asItemCallback;
63
67 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback){
68 $this->woodType = $woodType;
69 parent::__construct($idInfo, $name, $typeInfo);
70 $this->text = new SignText();
71 $this->backText = new SignText();
72 $this->asItemCallback = $asItemCallback;
73 }
74
75 public function readStateFromWorld() : Block{
76 parent::readStateFromWorld();
77 $tile = $this->position->getWorld()->getTile($this->position);
78 if($tile instanceof TileSign){
79 $this->text = $tile->getText();
80 $this->backText = $tile->getBackText();
81 $this->waxed = $tile->isWaxed();
82 $this->editorEntityRuntimeId = $tile->getEditorEntityRuntimeId();
83 }
84
85 return $this;
86 }
87
88 public function writeStateToWorld() : void{
89 parent::writeStateToWorld();
90 $tile = $this->position->getWorld()->getTile($this->position);
91 assert($tile instanceof TileSign);
92 $tile->setText($this->text);
93 $tile->setBackText($this->backText);
94 $tile->setWaxed($this->waxed);
95 $tile->setEditorEntityRuntimeId($this->editorEntityRuntimeId);
96 }
97
98 public function isSolid() : bool{
99 return false;
100 }
101
102 public function getMaxStackSize() : int{
103 return 16;
104 }
105
106 protected function recalculateCollisionBoxes() : array{
107 return [];
108 }
109
110 public function getSupportType(int $facing) : SupportType{
111 return SupportType::NONE;
112 }
113
114 abstract protected function getSupportingFace() : int;
115
116 public function onNearbyBlockChange() : void{
117 if($this->getSide($this->getSupportingFace())->getTypeId() === BlockTypeIds::AIR){
118 $this->position->getWorld()->useBreakOn($this->position);
119 }
120 }
121
122 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
123 if($player !== null){
124 $this->editorEntityRuntimeId = $player->getId();
125 }
126 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
127 }
128
129 public function onPostPlace() : void{
130 $player = $this->editorEntityRuntimeId !== null ?
131 $this->position->getWorld()->getEntity($this->editorEntityRuntimeId) :
132 null;
133 if($player instanceof Player){
134 $player->openSignEditor($this->position);
135 }
136 }
137
138 private function doSignChange(SignText $newText, Player $player, Item $item, bool $frontFace) : bool{
139 $ev = new SignChangeEvent($this, $player, $newText, $frontFace);
140 $ev->call();
141 if(!$ev->isCancelled()){
142 $this->setFaceText($frontFace, $ev->getNewText());
143 $this->position->getWorld()->setBlock($this->position, $this);
144 $item->pop();
145 return true;
146 }
147
148 return false;
149 }
150
151 private function changeSignGlowingState(bool $glowing, Player $player, Item $item, bool $frontFace) : bool{
152 $text = $this->getFaceText($frontFace);
153 if($text->isGlowing() !== $glowing && $this->doSignChange(new SignText($text->getLines(), $text->getBaseColor(), $glowing), $player, $item, $frontFace)){
154 $this->position->getWorld()->addSound($this->position, new InkSacUseSound());
155 return true;
156 }
157 return false;
158 }
159
160 private function wax(Player $player, Item $item) : bool{
161 if($this->waxed){
162 return false;
163 }
164
165 $this->waxed = true;
166 $this->position->getWorld()->setBlock($this->position, $this);
167 $item->pop();
168
169 return true;
170 }
171
172 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
173 if($player === null){
174 return false;
175 }
176 if($this->waxed){
177 return true;
178 }
179
180 $frontFace = $this->interactsFront($this->getHitboxCenter(), $player->getPosition(), $this->getFacingDegrees());
181
182 $dyeColor = $item instanceof Dye ? $item->getColor() : match($item->getTypeId()){
183 ItemTypeIds::BONE_MEAL => DyeColor::WHITE,
184 ItemTypeIds::LAPIS_LAZULI => DyeColor::BLUE,
185 ItemTypeIds::COCOA_BEANS => DyeColor::BROWN,
186 default => null
187 };
188 if($dyeColor !== null){
189 $color = $dyeColor === DyeColor::BLACK ? new Color(0, 0, 0) : $dyeColor->getRgbValue();
190 $text = $this->getFaceText($frontFace);
191 if(
192 $color->toARGB() !== $text->getBaseColor()->toARGB() &&
193 $this->doSignChange(new SignText($text->getLines(), $color, $text->isGlowing()), $player, $item, $frontFace)
194 ){
195 $this->position->getWorld()->addSound($this->position, new DyeUseSound());
196 return true;
197 }
198 }elseif(match($item->getTypeId()){
199 ItemTypeIds::INK_SAC => $this->changeSignGlowingState(false, $player, $item, $frontFace),
200 ItemTypeIds::GLOW_INK_SAC => $this->changeSignGlowingState(true, $player, $item, $frontFace),
201 ItemTypeIds::HONEYCOMB => $this->wax($player, $item),
202 default => false
203 }){
204 return true;
205 }
206
207 $player->openSignEditor($this->position, $frontFace);
208
209 return true;
210 }
211
212 private function interactsFront(Vector3 $hitboxCenter, Vector3 $playerPosition, float $signFacingDegrees) : bool{
213 $playerCenterDiffX = $playerPosition->x - $hitboxCenter->x;
214 $playerCenterDiffZ = $playerPosition->z - $hitboxCenter->z;
215
216 $f1 = rad2deg(atan2($playerCenterDiffZ, $playerCenterDiffX)) - 90.0;
217
218 $rotationDiff = $signFacingDegrees - $f1;
219 $rotation = fmod($rotationDiff + 180.0, 360.0) - 180.0; // Normalize to [-180, 180]
220 return abs($rotation) <= 90.0;
221 }
222
226 protected function getHitboxCenter() : Vector3{
227 return $this->position->add(0.5, 0.5, 0.5);
228 }
229
233 protected function getFacingDegrees() : float{
234 return 0;
235 }
236
242 public function getText() : SignText{
243 return $this->text;
244 }
245
251 public function setText(SignText $text) : self{
252 $this->text = $text;
253 return $this;
254 }
255
256 public function getFaceText(bool $frontFace) : SignText{
257 return $frontFace ? $this->text : $this->backText;
258 }
259
261 public function setFaceText(bool $frontFace, SignText $text) : self{
262 $frontFace ? $this->text = $text : $this->backText = $text;
263 return $this;
264 }
265
269 public function isWaxed() : bool{ return $this->waxed; }
270
272 public function setWaxed(bool $waxed) : self{
273 $this->waxed = $waxed;
274 return $this;
275 }
276
282 public function getEditorEntityRuntimeId() : ?int{ return $this->editorEntityRuntimeId; }
283
285 public function setEditorEntityRuntimeId(?int $editorEntityRuntimeId) : self{
286 $this->editorEntityRuntimeId = $editorEntityRuntimeId;
287 return $this;
288 }
289
294 public function updateText(Player $author, SignText $text) : bool{
295 return $this->updateFaceText($author, true, $text);
296 }
297
304 public function updateFaceText(Player $author, bool $frontFace, SignText $text) : bool{
305 $size = 0;
306 foreach($text->getLines() as $line){
307 $size += strlen($line);
308 }
309 if($size > 1000){
310 throw new \UnexpectedValueException($author->getName() . " tried to write $size bytes of text onto a sign (bigger than max 1000)");
311 }
312 $oldText = $this->getFaceText($frontFace);
313 $ev = new SignChangeEvent($this, $author, new SignText(array_map(function(string $line) : string{
314 return TextFormat::clean($line, false);
315 }, $text->getLines()), $oldText->getBaseColor(), $oldText->isGlowing()), $frontFace);
316 if($this->waxed || $this->editorEntityRuntimeId !== $author->getId()){
317 $ev->cancel();
318 }
319 $ev->call();
320 if(!$ev->isCancelled()){
321 $this->setFaceText($frontFace, $ev->getNewText());
322 $this->setEditorEntityRuntimeId(null);
323 $this->position->getWorld()->setBlock($this->position, $this);
324 return true;
325 }
326
327 return false;
328 }
329
330 public function asItem() : Item{
331 return ($this->asItemCallback)();
332 }
333
334 public function getFuelTime() : int{
335 return $this->woodType->isFlammable() ? 200 : 0;
336 }
337}
setEditorEntityRuntimeId(?int $editorEntityRuntimeId)
Definition BaseSign.php:285
updateText(Player $author, SignText $text)
Definition BaseSign.php:294
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition BaseSign.php:172
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition BaseSign.php:122
updateFaceText(Player $author, bool $frontFace, SignText $text)
Definition BaseSign.php:304
setFaceText(bool $frontFace, SignText $text)
Definition BaseSign.php:261
getSupportType(int $facing)
Definition BaseSign.php:110
setText(SignText $text)
Definition BaseSign.php:251
__construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback)
Definition BaseSign.php:67