37 public const TAG_PAGES =
"pages";
38 public const TAG_PAGE_TEXT =
"text";
39 public const TAG_PAGE_PHOTONAME =
"photoname";
45 private array $pages = [];
47 public function __construct(
ItemIdentifier $identifier,
string $name){
48 parent::__construct($identifier, $name);
55 return isset($this->pages[$pageId]);
64 return $this->pages[$pageId]->getText();
72 public function setPageText(
int $pageId,
string $pageText) : self{
73 if(!$this->pageExists($pageId)){
74 $this->addPage($pageId);
87 public function addPage(
int $pageId) : self{
89 throw new \InvalidArgumentException(
"Page number \"$pageId\" is out of range");
92 for($current = count($this->pages); $current <= $pageId; $current++){
104 unset($this->pages[$pageId]);
105 $this->pages = array_values($this->pages);
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");
118 $newPages = array_slice($this->pages, 0, $pageId);
120 array_push($newPages, ...array_slice($this->pages, $pageId));
121 $this->pages = $newPages;
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);
159 $this->pages = array_values($pages);
164 parent::deserializeCompoundTag($tag);
169 if($pages->getTagType() === NBT::TAG_Compound){
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,
""));
174 }elseif($pages->getTagType() === NBT::TAG_String){
176 foreach($pages as $page){
177 $this->pages[] =
new WritableBookPage(mb_scrub($page->getValue(),
'UTF-8'));
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())
193 $tag->
setTag(self::TAG_PAGES, $pages);