PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
Sign.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\tile;
25
35use function implode;
36use function mb_scrub;
37use function rtrim;
38use function sprintf;
39
44class Sign extends Spawnable{
45 public const TAG_TEXT_BLOB = "Text";
46 public const TAG_TEXT_LINE = "Text%d"; //sprintf()able
47 public const TAG_TEXT_COLOR = "SignTextColor";
48 public const TAG_GLOWING_TEXT = "IgnoreLighting";
49 public const TAG_PERSIST_FORMATTING = "PersistFormatting"; //TAG_Byte
54 public const TAG_LEGACY_BUG_RESOLVE = "TextIgnoreLegacyBugResolved";
55
56 public const TAG_FRONT_TEXT = "FrontText"; //TAG_Compound
57 public const TAG_BACK_TEXT = "BackText"; //TAG_Compound
58 public const TAG_WAXED = "IsWaxed"; //TAG_Byte
59 public const TAG_LOCKED_FOR_EDITING_BY = "LockedForEditingBy"; //TAG_Long
60
61 protected SignText $text;
62 protected SignText $backText;
63 private bool $waxed = false;
64
65 protected ?int $editorEntityRuntimeId = null;
66
67 public function __construct(World $world, Vector3 $pos){
68 $this->text = new SignText();
69 $this->backText = new SignText();
70 parent::__construct($world, $pos);
71 }
72
73 private function readTextTag(CompoundTag $nbt, bool $lightingBugResolved) : SignText{
74 $baseColor = new Color(0, 0, 0);
75 $glowingText = false;
76 if(($baseColorTag = $nbt->getTag(self::TAG_TEXT_COLOR)) instanceof IntTag){
77 $baseColor = Color::fromARGB(Binary::unsignInt($baseColorTag->getValue()));
78 }
79 if($lightingBugResolved && ($glowingTextTag = $nbt->getTag(self::TAG_GLOWING_TEXT)) instanceof ByteTag){
80 //both of these must be 1 - if only one is set, it's a leftover from 1.16.210 experimental features
81 //see https://bugs.mojang.com/browse/MCPE-117835
82 $glowingText = $glowingTextTag->getValue() !== 0;
83 }
84 return SignText::fromBlob(mb_scrub($nbt->getString(self::TAG_TEXT_BLOB), 'UTF-8'), $baseColor, $glowingText);
85 }
86
87 private function writeTextTag(SignText $text) : CompoundTag{
88 return CompoundTag::create()
89 ->setString(self::TAG_TEXT_BLOB, rtrim(implode("\n", $text->getLines()), "\n"))
90 ->setInt(self::TAG_TEXT_COLOR, Binary::signInt($text->getBaseColor()->toARGB()))
91 ->setByte(self::TAG_GLOWING_TEXT, $text->isGlowing() ? 1 : 0)
92 ->setByte(self::TAG_PERSIST_FORMATTING, 1);
93 }
94
95 public function readSaveData(CompoundTag $nbt) : void{
96 $frontTextTag = $nbt->getTag(self::TAG_FRONT_TEXT);
97 if($frontTextTag instanceof CompoundTag){
98 $this->text = $this->readTextTag($frontTextTag, true);
99 }elseif($nbt->getTag(self::TAG_TEXT_BLOB) instanceof StringTag){ //MCPE 1.2 save format
100 $lightingBugResolved = false;
101 if(($lightingBugResolvedTag = $nbt->getTag(self::TAG_LEGACY_BUG_RESOLVE)) instanceof ByteTag){
102 $lightingBugResolved = $lightingBugResolvedTag->getValue() !== 0;
103 }
104 $this->text = $this->readTextTag($nbt, $lightingBugResolved);
105 }else{
106 $text = [];
107 for($i = 0; $i < SignText::LINE_COUNT; ++$i){
108 $textKey = sprintf(self::TAG_TEXT_LINE, $i + 1);
109 if(($lineTag = $nbt->getTag($textKey)) instanceof StringTag){
110 $text[$i] = mb_scrub($lineTag->getValue(), 'UTF-8');
111 }
112 }
113 $this->text = new SignText($text);
114 }
115 $backTextTag = $nbt->getTag(self::TAG_BACK_TEXT);
116 $this->backText = $backTextTag instanceof CompoundTag ? $this->readTextTag($backTextTag, true) : new SignText();
117 $this->waxed = $nbt->getByte(self::TAG_WAXED, 0) !== 0;
118 }
119
120 protected function writeSaveData(CompoundTag $nbt) : void{
121 $nbt->setTag(self::TAG_FRONT_TEXT, $this->writeTextTag($this->text));
122 $nbt->setTag(self::TAG_BACK_TEXT, $this->writeTextTag($this->backText));
123
124 $nbt->setByte(self::TAG_WAXED, $this->waxed ? 1 : 0);
125 }
126
127 public function getText() : SignText{
128 return $this->text;
129 }
130
131 public function setText(SignText $text) : void{
132 $this->text = $text;
133 }
134
135 public function getBackText() : SignText{ return $this->backText; }
136
137 public function setBackText(SignText $backText) : void{ $this->backText = $backText; }
138
139 public function isWaxed() : bool{ return $this->waxed; }
140
141 public function setWaxed(bool $waxed) : void{ $this->waxed = $waxed; }
142
153 public function getEditorEntityRuntimeId() : ?int{ return $this->editorEntityRuntimeId; }
154
155 public function setEditorEntityRuntimeId(?int $editorEntityRuntimeId) : void{
156 $this->editorEntityRuntimeId = $editorEntityRuntimeId;
157 }
158
159 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
160 $nbt->setTag(self::TAG_FRONT_TEXT, $this->writeTextTag($this->text));
161 $nbt->setTag(self::TAG_BACK_TEXT, $this->writeTextTag($this->backText));
162 $nbt->setByte(self::TAG_WAXED, $this->waxed ? 1 : 0);
163 $nbt->setLong(self::TAG_LOCKED_FOR_EDITING_BY, $this->editorEntityRuntimeId ?? -1);
164 }
165}
addAdditionalSpawnData(CompoundTag $nbt)
Definition Sign.php:159
writeSaveData(CompoundTag $nbt)
Definition Sign.php:120
static fromBlob(string $blob, ?Color $baseColor=null, bool $glowing=false)
Definition SignText.php:81
setByte(string $name, int $value)
setLong(string $name, int $value)
setTag(string $name, Tag $tag)