PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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
29use function array_push;
30use function array_slice;
31use function array_values;
32use function count;
33use function mb_scrub;
34
35abstract class WritableBookBase extends Item{
36 public const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
37 public const TAG_PAGE_TEXT = "text"; //TAG_String
38 public const TAG_PAGE_PHOTONAME = "photoname"; //TAG_String - TODO
39
44 private array $pages = [];
45
46 public function __construct(ItemIdentifier $identifier, string $name){
47 parent::__construct($identifier, $name);
48 }
49
53 public function pageExists(int $pageId) : bool{
54 return isset($this->pages[$pageId]);
55 }
56
62 public function getPageText(int $pageId) : string{
63 return $this->pages[$pageId]->getText();
64 }
65
71 public function setPageText(int $pageId, string $pageText) : self{
72 if(!$this->pageExists($pageId)){
73 $this->addPage($pageId);
74 }
75
76 $this->pages[$pageId] = new WritableBookPage($pageText);
77 return $this;
78 }
79
86 public function addPage(int $pageId) : self{
87 if($pageId < 0){
88 throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
89 }
90
91 for($current = count($this->pages); $current <= $pageId; $current++){
92 $this->pages[] = new WritableBookPage("");
93 }
94 return $this;
95 }
96
102 public function deletePage(int $pageId) : self{
103 $newPages = $this->pages;
104 unset($newPages[$pageId]);
105 $this->pages = array_values($newPages);
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(($compoundPages = $pages->cast(CompoundTag::class)) !== null){ //PE format
170 foreach($compoundPages as $page){
171 $this->pages[] = new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT), 'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME, ""));
172 }
173 }elseif(($stringPages = $pages->cast(StringTag::class)) !== null){ //PC format
174 foreach($stringPages as $page){
175 $this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8'));
176 }
177 }
178 }
179 }
180
181 protected function serializeCompoundTag(CompoundTag $tag) : void{
182 parent::serializeCompoundTag($tag);
183 if(count($this->pages) > 0){
184 $pages = new ListTag();
185 foreach($this->pages as $page){
186 $pages->push(CompoundTag::create()
187 ->setString(self::TAG_PAGE_TEXT, $page->getText())
188 ->setString(self::TAG_PAGE_PHOTONAME, $page->getPhotoName())
189 );
190 }
191 $tag->setTag(self::TAG_PAGES, $pages);
192 }else{
193 $tag->removeTag(self::TAG_PAGES);
194 }
195 }
196}
insertPage(int $pageId, string $pageText="")
swapPages(int $pageId1, int $pageId2)
setPageText(int $pageId, string $pageText)
setTag(string $name, Tag $tag)
getListTag(string $name, string $tagClass=Tag::class)