PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
WritableBookBase.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\item;
25
30use function array_push;
31use function array_slice;
32use function array_values;
33use function count;
34use function mb_scrub;
35
36abstract class WritableBookBase extends Item{
37 public const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
38 public const TAG_PAGE_TEXT = "text"; //TAG_String
39 public const TAG_PAGE_PHOTONAME = "photoname"; //TAG_String - TODO
40
45 private array $pages = [];
46
47 public function __construct(ItemIdentifier $identifier, string $name){
48 parent::__construct($identifier, $name);
49 }
50
54 public function pageExists(int $pageId) : bool{
55 return isset($this->pages[$pageId]);
56 }
57
63 public function getPageText(int $pageId) : string{
64 return $this->pages[$pageId]->getText();
65 }
66
72 public function setPageText(int $pageId, string $pageText) : self{
73 if(!$this->pageExists($pageId)){
74 $this->addPage($pageId);
75 }
76
77 $this->pages[$pageId] = new WritableBookPage($pageText);
78 return $this;
79 }
80
87 public function addPage(int $pageId) : self{
88 if($pageId < 0){
89 throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
90 }
91
92 for($current = count($this->pages); $current <= $pageId; $current++){
93 $this->pages[] = new WritableBookPage("");
94 }
95 return $this;
96 }
97
103 public function deletePage(int $pageId) : self{
104 unset($this->pages[$pageId]);
105 $this->pages = array_values($this->pages);
106 return $this;
107 }
108
114 public function insertPage(int $pageId, string $pageText = "") : self{
115 if($pageId < 0 || $pageId > count($this->pages)){
116 throw new \InvalidArgumentException("Page ID must not be negative");
117 }
118 $newPages = array_slice($this->pages, 0, $pageId);
119 $newPages[] = new WritableBookPage($pageText);
120 array_push($newPages, ...array_slice($this->pages, $pageId));
121 $this->pages = $newPages;
122 return $this;
123 }
124
131 public function swapPages(int $pageId1, int $pageId2) : bool{
132 $pageContents1 = $this->getPageText($pageId1);
133 $pageContents2 = $this->getPageText($pageId2);
134 $this->setPageText($pageId1, $pageContents2);
135 $this->setPageText($pageId2, $pageContents1);
136
137 return true;
138 }
139
140 public function getMaxStackSize() : int{
141 return 1;
142 }
143
149 public function getPages() : array{
150 return $this->pages;
151 }
152
158 public function setPages(array $pages) : self{
159 $this->pages = array_values($pages);
160 return $this;
161 }
162
163 protected function deserializeCompoundTag(CompoundTag $tag) : void{
164 parent::deserializeCompoundTag($tag);
165 $this->pages = [];
166
167 $pages = $tag->getListTag(self::TAG_PAGES);
168 if($pages !== null){
169 if($pages->getTagType() === NBT::TAG_Compound){ //PE format
171 foreach($pages as $page){
172 $this->pages[] = new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT), 'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME, ""));
173 }
174 }elseif($pages->getTagType() === NBT::TAG_String){ //PC format
176 foreach($pages as $page){
177 $this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8'));
178 }
179 }
180 }
181 }
182
183 protected function serializeCompoundTag(CompoundTag $tag) : void{
184 parent::serializeCompoundTag($tag);
185 if(count($this->pages) > 0){
186 $pages = new ListTag();
187 foreach($this->pages as $page){
188 $pages->push(CompoundTag::create()
189 ->setString(self::TAG_PAGE_TEXT, $page->getText())
190 ->setString(self::TAG_PAGE_PHOTONAME, $page->getPhotoName())
191 );
192 }
193 $tag->setTag(self::TAG_PAGES, $pages);
194 }else{
195 $tag->removeTag(self::TAG_PAGES);
196 }
197 }
198}
insertPage(int $pageId, string $pageText="")
swapPages(int $pageId1, int $pageId2)
setPageText(int $pageId, string $pageText)
setTag(string $name, Tag $tag)