diff --git a/src/pocketmine/item/WritableBook.php b/src/pocketmine/item/WritableBook.php index 16a27ba9d..746ea6a12 100644 --- a/src/pocketmine/item/WritableBook.php +++ b/src/pocketmine/item/WritableBook.php @@ -44,8 +44,7 @@ class WritableBook extends Item{ * @return bool */ public function pageExists(int $pageId) : bool{ - $pages = $this->getNamedTag()->getListTag(self::TAG_PAGES); - return $pages !== null and isset($pages[$pageId]); + return isset($this->getPages()[$pageId]); } /** @@ -105,13 +104,11 @@ class WritableBook extends Item{ if($pageId < 0){ throw new \InvalidArgumentException("Page number \"$pageId\" is out of range"); } - $namedTag = $this->getNamedTag(); - /** @var CompoundTag[]|ListTag $pages */ - $pages = $namedTag->getListTag(self::TAG_PAGES) ?? new ListTag(self::TAG_PAGES, [], NBT::TAG_Compound); + $pages = $this->getPages(); for($id = 0; $id <= $pageId; $id++){ - if(!$this->pageExists($id)){ + if(!isset($pages[$id])){ $pages[$id] = new CompoundTag("", [ new StringTag("text", ""), new StringTag("photoname", "") @@ -119,8 +116,7 @@ class WritableBook extends Item{ } } - $namedTag->setTag($pages); - $this->setNamedTag($namedTag); + $this->setPages($pages); } /** @@ -131,14 +127,10 @@ class WritableBook extends Item{ * @return bool indicating success */ public function deletePage(int $pageId) : bool{ - if(!$this->pageExists($pageId)){ - return false; - } + $pages = $this->getPages(); + unset($pages[$pageId]); - $namedTag = $this->getNamedTag(); - unset($namedTag->getListTag(self::TAG_PAGES)[$pageId]); - $this->pushPages($pageId, $namedTag); - $this->setNamedTag($namedTag); + $this->setPages(array_values($pages)); return true; } @@ -152,14 +144,17 @@ class WritableBook extends Item{ * @return bool indicating success */ public function insertPage(int $pageId, string $pageText = "") : bool{ - $namedTag = $this->getNamedTag(); - if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){ - $namedTag->pages = new ListTag("pages", []); - } - $this->pushPages($pageId, $namedTag, false); + $pages = $this->getPages(); + + $this->setPages(array_merge( + array_slice($pages, 0, $pageId), + [new CompoundTag("", [ + new StringTag("text", $pageText), + new StringTag("photoname", "") //TODO + ])], + array_slice($pages, $pageId) + )); - $namedTag->pages->{$pageId}->text->setValue($pageText); - $this->setNamedTag($namedTag); return true; } @@ -188,52 +183,27 @@ class WritableBook extends Item{ return 1; } - /** - * @param int $pageId - * @param CompoundTag $namedTag - * @param bool $downwards - * - * @return bool - */ - private function pushPages(int $pageId, CompoundTag $namedTag, bool $downwards = true) : bool{ - $pages = $this->getPages(); - if(empty($pages)){ - return false; - } - - $pagesTag = $namedTag->getListTag(self::TAG_PAGES); - assert($pagesTag !== null); - - $type = $downwards ? -1 : 1; - foreach($pages as $key => $page){ - if(($key <= $pageId and $downwards) or ($key < $pageId and !$downwards)){ - continue; - } - - if($downwards){ - unset($pagesTag[$key]); - } - $pagesTag[$key + $type] = new CompoundTag("", [ - new StringTag("text", $page->text->getValue()), - new StringTag("photoname", "") - ]); - } - return true; - } - /** * Returns an array containing all pages of this book. * * @return CompoundTag[] */ public function getPages() : array{ - $namedTag = $this->getNamedTag(); - if(!isset($namedTag->pages)){ + $pages = $this->getNamedTag()->getListTag(self::TAG_PAGES); + if($pages === null){ return []; } - return array_filter((array) $namedTag->pages, function(string $key){ - return is_numeric($key); - }, ARRAY_FILTER_USE_KEY); + return $pages->getValue(); + } + + /** + * + * @param CompoundTag[] $pages + */ + public function setPages(array $pages) : void{ + $nbt = $this->getNamedTag(); + $nbt->setTag(new ListTag(self::TAG_PAGES, $pages, NBT::TAG_Compound)); + $this->setNamedTag($nbt); } } \ No newline at end of file