PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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;
30use pocketmine\block\utils\WoodType;
31use pocketmine\block\utils\WoodTypeTrait;
44use function array_map;
45use function assert;
46use function strlen;
47
48abstract class BaseSign extends Transparent{
49 use WoodTypeTrait;
50
51 protected SignText $text;
52 private bool $waxed = false;
53
54 protected ?int $editorEntityRuntimeId = null;
55
57 private \Closure $asItemCallback;
58
62 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback){
63 $this->woodType = $woodType;
64 parent::__construct($idInfo, $name, $typeInfo);
65 $this->text = new SignText();
66 $this->asItemCallback = $asItemCallback;
67 }
68
69 public function readStateFromWorld() : Block{
70 parent::readStateFromWorld();
71 $tile = $this->position->getWorld()->getTile($this->position);
72 if($tile instanceof TileSign){
73 $this->text = $tile->getText();
74 $this->waxed = $tile->isWaxed();
75 $this->editorEntityRuntimeId = $tile->getEditorEntityRuntimeId();
76 }
77
78 return $this;
79 }
80
81 public function writeStateToWorld() : void{
82 parent::writeStateToWorld();
83 $tile = $this->position->getWorld()->getTile($this->position);
84 assert($tile instanceof TileSign);
85 $tile->setText($this->text);
86 $tile->setWaxed($this->waxed);
87 $tile->setEditorEntityRuntimeId($this->editorEntityRuntimeId);
88 }
89
90 public function isSolid() : bool{
91 return false;
92 }
93
94 public function getMaxStackSize() : int{
95 return 16;
96 }
97
101 protected function recalculateCollisionBoxes() : array{
102 return [];
103 }
104
105 public function getSupportType(int $facing) : SupportType{
106 return SupportType::NONE;
107 }
108
109 abstract protected function getSupportingFace() : int;
110
111 public function onNearbyBlockChange() : void{
112 if($this->getSide($this->getSupportingFace())->getTypeId() === BlockTypeIds::AIR){
113 $this->position->getWorld()->useBreakOn($this->position);
114 }
115 }
116
117 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
118 if($player !== null){
119 $this->editorEntityRuntimeId = $player->getId();
120 }
121 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
122 }
123
124 public function onPostPlace() : void{
125 $player = $this->editorEntityRuntimeId !== null ?
126 $this->position->getWorld()->getEntity($this->editorEntityRuntimeId) :
127 null;
128 if($player instanceof Player){
129 $player->openSignEditor($this->position);
130 }
131 }
132
133 private function doSignChange(SignText $newText, Player $player, Item $item) : bool{
134 $ev = new SignChangeEvent($this, $player, $newText);
135 $ev->call();
136 if(!$ev->isCancelled()){
137 $this->text = $ev->getNewText();
138 $this->position->getWorld()->setBlock($this->position, $this);
139 $item->pop();
140 return true;
141 }
142
143 return false;
144 }
145
146 private function changeSignGlowingState(bool $glowing, Player $player, Item $item) : bool{
147 if($this->text->isGlowing() !== $glowing && $this->doSignChange(new SignText($this->text->getLines(), $this->text->getBaseColor(), $glowing), $player, $item)){
148 $this->position->getWorld()->addSound($this->position, new InkSacUseSound());
149 return true;
150 }
151 return false;
152 }
153
154 private function wax(Player $player, Item $item) : bool{
155 if($this->waxed){
156 return false;
157 }
158
159 $this->waxed = true;
160 $this->position->getWorld()->setBlock($this->position, $this);
161 $item->pop();
162
163 return true;
164 }
165
166 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
167 if($player === null){
168 return false;
169 }
170 if($this->waxed){
171 return true;
172 }
173
174 $dyeColor = $item instanceof Dye ? $item->getColor() : match($item->getTypeId()){
175 ItemTypeIds::BONE_MEAL => DyeColor::WHITE,
176 ItemTypeIds::LAPIS_LAZULI => DyeColor::BLUE,
177 ItemTypeIds::COCOA_BEANS => DyeColor::BROWN,
178 default => null
179 };
180 if($dyeColor !== null){
181 $color = $dyeColor === DyeColor::BLACK ? new Color(0, 0, 0) : $dyeColor->getRgbValue();
182 if(
183 $color->toARGB() !== $this->text->getBaseColor()->toARGB() &&
184 $this->doSignChange(new SignText($this->text->getLines(), $color, $this->text->isGlowing()), $player, $item)
185 ){
186 $this->position->getWorld()->addSound($this->position, new DyeUseSound());
187 return true;
188 }
189 }elseif(match($item->getTypeId()){
190 ItemTypeIds::INK_SAC => $this->changeSignGlowingState(false, $player, $item),
191 ItemTypeIds::GLOW_INK_SAC => $this->changeSignGlowingState(true, $player, $item),
192 ItemTypeIds::HONEYCOMB => $this->wax($player, $item),
193 default => false
194 }){
195 return true;
196 }
197
198 $player->openSignEditor($this->position);
199
200 return true;
201 }
202
206 public function getText() : SignText{
207 return $this->text;
208 }
209
211 public function setText(SignText $text) : self{
212 $this->text = $text;
213 return $this;
214 }
215
219 public function isWaxed() : bool{ return $this->waxed; }
220
222 public function setWaxed(bool $waxed) : self{
223 $this->waxed = $waxed;
224 return $this;
225 }
226
232 public function getEditorEntityRuntimeId() : ?int{ return $this->editorEntityRuntimeId; }
233
235 public function setEditorEntityRuntimeId(?int $editorEntityRuntimeId) : self{
236 $this->editorEntityRuntimeId = $editorEntityRuntimeId;
237 return $this;
238 }
239
246 public function updateText(Player $author, SignText $text) : bool{
247 $size = 0;
248 foreach($text->getLines() as $line){
249 $size += strlen($line);
250 }
251 if($size > 1000){
252 throw new \UnexpectedValueException($author->getName() . " tried to write $size bytes of text onto a sign (bigger than max 1000)");
253 }
254 $ev = new SignChangeEvent($this, $author, new SignText(array_map(function(string $line) : string{
255 return TextFormat::clean($line, false);
256 }, $text->getLines()), $this->text->getBaseColor(), $this->text->isGlowing()));
257 if($this->waxed || $this->editorEntityRuntimeId !== $author->getId()){
258 $ev->cancel();
259 }
260 $ev->call();
261 if(!$ev->isCancelled()){
262 $this->setText($ev->getNewText());
263 $this->setEditorEntityRuntimeId(null);
264 $this->position->getWorld()->setBlock($this->position, $this);
265 return true;
266 }
267
268 return false;
269 }
270
271 public function asItem() : Item{
272 return ($this->asItemCallback)();
273 }
274
275 public function getFuelTime() : int{
276 return $this->woodType->isFlammable() ? 200 : 0;
277 }
278}
setEditorEntityRuntimeId(?int $editorEntityRuntimeId)
Definition BaseSign.php:235
updateText(Player $author, SignText $text)
Definition BaseSign.php:246
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition BaseSign.php:166
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition BaseSign.php:117
getSupportType(int $facing)
Definition BaseSign.php:105
setText(SignText $text)
Definition BaseSign.php:211
__construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, WoodType $woodType, \Closure $asItemCallback)
Definition BaseSign.php:62