Clean up WritableBook hierarchy

This commit is contained in:
Dylan K. Taylor 2019-03-23 14:36:03 +00:00
parent a74a4b579d
commit 034bd716c8
5 changed files with 221 additions and 194 deletions

View File

@ -2333,7 +2333,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
public function handleBookEdit(BookEditPacket $packet) : bool{
/** @var WritableBook $oldBook */
$oldBook = $this->inventory->getItem($packet->inventorySlot);
if($oldBook->getId() !== Item::WRITABLE_BOOK){
if(!($oldBook instanceof WritableBook)){
return false;
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\event\player;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\item\WritableBook;
use pocketmine\item\WritableBookBase;
use pocketmine\Player;
class PlayerEditBookEvent extends PlayerEvent implements Cancellable{
@ -37,16 +37,16 @@ class PlayerEditBookEvent extends PlayerEvent implements Cancellable{
public const ACTION_SWAP_PAGES = 3;
public const ACTION_SIGN_BOOK = 4;
/** @var WritableBook */
/** @var WritableBookBase */
private $oldBook;
/** @var int */
private $action;
/** @var WritableBook */
/** @var WritableBookBase */
private $newBook;
/** @var int[] */
private $modifiedPages;
public function __construct(Player $player, WritableBook $oldBook, WritableBook $newBook, int $action, array $modifiedPages){
public function __construct(Player $player, WritableBookBase $oldBook, WritableBookBase $newBook, int $action, array $modifiedPages){
$this->player = $player;
$this->oldBook = $oldBook;
$this->newBook = $newBook;
@ -66,9 +66,9 @@ class PlayerEditBookEvent extends PlayerEvent implements Cancellable{
/**
* Returns the book before it was modified.
*
* @return WritableBook
* @return WritableBookBase
*/
public function getOldBook() : WritableBook{
public function getOldBook() : WritableBookBase{
return $this->oldBook;
}
@ -76,18 +76,18 @@ class PlayerEditBookEvent extends PlayerEvent implements Cancellable{
* Returns the book after it was modified.
* The new book may be a written book, if the book was signed.
*
* @return WritableBook
* @return WritableBookBase
*/
public function getNewBook() : WritableBook{
public function getNewBook() : WritableBookBase{
return $this->newBook;
}
/**
* Sets the new book as the given instance.
*
* @param WritableBook $book
* @param WritableBookBase $book
*/
public function setNewBook(WritableBook $book) : void{
public function setNewBook(WritableBookBase $book) : void{
$this->newBook = $book;
}

View File

@ -23,189 +23,9 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
class WritableBook extends Item{
public const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
public const TAG_PAGE_TEXT = "text"; //TAG_String
public const TAG_PAGE_PHOTONAME = "photoname"; //TAG_String - TODO
class WritableBook extends WritableBookBase{
public function __construct(){
parent::__construct(self::WRITABLE_BOOK, 0, "Book & Quill");
}
/**
* Returns whether the given page exists in this book.
*
* @param int $pageId
*
* @return bool
*/
public function pageExists(int $pageId) : bool{
return $this->getPagesTag()->isset($pageId);
}
/**
* Returns a string containing the content of a page (which could be empty), or null if the page doesn't exist.
*
* @param int $pageId
*
* @return string|null
*/
public function getPageText(int $pageId) : ?string{
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if($pages === null){
return null;
}
$page = $pages->get($pageId);
if($page instanceof CompoundTag){
return $page->getString(self::TAG_PAGE_TEXT, "");
}
return null;
}
/**
* Sets the text of a page in the book. Adds the page if the page does not yet exist.
*
* @param int $pageId
* @param string $pageText
*
* @return $this
*/
public function setPageText(int $pageId, string $pageText) : self{
if(!$this->pageExists($pageId)){
$this->addPage($pageId);
}
/** @var CompoundTag[]|ListTag $pagesTag */
$pagesTag = $this->getPagesTag();
/** @var CompoundTag $page */
$page = $pagesTag->get($pageId);
$page->setString(self::TAG_PAGE_TEXT, $pageText);
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Adds a new page with the given page ID.
* Creates a new page for every page between the given ID and existing pages that doesn't yet exist.
*
* @param int $pageId
*
* @return $this
*/
public function addPage(int $pageId) : self{
if($pageId < 0){
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
}
$pagesTag = $this->getPagesTag();
for($current = $pagesTag->count(); $current <= $pageId; $current++){
$pagesTag->push(CompoundTag::create()
->setString(self::TAG_PAGE_TEXT, "")
->setString(self::TAG_PAGE_PHOTONAME, "")
);
}
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Deletes an existing page with the given page ID.
*
* @param int $pageId
*
* @return $this
*/
public function deletePage(int $pageId) : self{
$pagesTag = $this->getPagesTag();
$pagesTag->remove($pageId);
return $this;
}
/**
* Inserts a new page with the given text and moves other pages upwards.
*
* @param int $pageId
* @param string $pageText
*
* @return $this
*/
public function insertPage(int $pageId, string $pageText = "") : self{
$pagesTag = $this->getPagesTag();
$pagesTag->insert($pageId, CompoundTag::create()
->setString(self::TAG_PAGE_TEXT, $pageText)
->setString(self::TAG_PAGE_PHOTONAME, "")
);
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Switches the text of two pages with each other.
*
* @param int $pageId1
* @param int $pageId2
*
* @return bool indicating success
*/
public function swapPages(int $pageId1, int $pageId2) : bool{
if(!$this->pageExists($pageId1) or !$this->pageExists($pageId2)){
return false;
}
$pageContents1 = $this->getPageText($pageId1);
$pageContents2 = $this->getPageText($pageId2);
$this->setPageText($pageId1, $pageContents2);
$this->setPageText($pageId2, $pageContents1);
return true;
}
public function getMaxStackSize() : int{
return 1;
}
/**
* Returns an array containing all pages of this book.
*
* @return CompoundTag[]
*/
public function getPages() : array{
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if($pages === null){
return [];
}
return $pages->getValue();
}
protected function getPagesTag() : ListTag{
return $this->getNamedTag()->getListTag(self::TAG_PAGES) ?? new ListTag([], NBT::TAG_Compound);
}
/**
* @param CompoundTag[] $pages
*
* @return $this
*/
public function setPages(array $pages) : self{
$nbt = $this->getNamedTag();
$nbt->setTag(self::TAG_PAGES, new ListTag($pages, NBT::TAG_Compound));
$this->setNamedTag($nbt);
return $this;
}
}

View File

@ -0,0 +1,207 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
abstract class WritableBookBase extends Item{
public const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
public const TAG_PAGE_TEXT = "text"; //TAG_String
public const TAG_PAGE_PHOTONAME = "photoname"; //TAG_String - TODO
/**
* Returns whether the given page exists in this book.
*
* @param int $pageId
*
* @return bool
*/
public function pageExists(int $pageId) : bool{
return $this->getPagesTag()->isset($pageId);
}
/**
* Returns a string containing the content of a page (which could be empty), or null if the page doesn't exist.
*
* @param int $pageId
*
* @return string|null
*/
public function getPageText(int $pageId) : ?string{
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if($pages === null){
return null;
}
$page = $pages->get($pageId);
if($page instanceof CompoundTag){
return $page->getString(self::TAG_PAGE_TEXT, "");
}
return null;
}
/**
* Sets the text of a page in the book. Adds the page if the page does not yet exist.
*
* @param int $pageId
* @param string $pageText
*
* @return $this
*/
public function setPageText(int $pageId, string $pageText) : self{
if(!$this->pageExists($pageId)){
$this->addPage($pageId);
}
/** @var CompoundTag[]|ListTag $pagesTag */
$pagesTag = $this->getPagesTag();
/** @var CompoundTag $page */
$page = $pagesTag->get($pageId);
$page->setString(self::TAG_PAGE_TEXT, $pageText);
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Adds a new page with the given page ID.
* Creates a new page for every page between the given ID and existing pages that doesn't yet exist.
*
* @param int $pageId
*
* @return $this
*/
public function addPage(int $pageId) : self{
if($pageId < 0){
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
}
$pagesTag = $this->getPagesTag();
for($current = $pagesTag->count(); $current <= $pageId; $current++){
$pagesTag->push(CompoundTag::create()
->setString(self::TAG_PAGE_TEXT, "")
->setString(self::TAG_PAGE_PHOTONAME, "")
);
}
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Deletes an existing page with the given page ID.
*
* @param int $pageId
*
* @return $this
*/
public function deletePage(int $pageId) : self{
$pagesTag = $this->getPagesTag();
$pagesTag->remove($pageId);
return $this;
}
/**
* Inserts a new page with the given text and moves other pages upwards.
*
* @param int $pageId
* @param string $pageText
*
* @return $this
*/
public function insertPage(int $pageId, string $pageText = "") : self{
$pagesTag = $this->getPagesTag();
$pagesTag->insert($pageId, CompoundTag::create()
->setString(self::TAG_PAGE_TEXT, $pageText)
->setString(self::TAG_PAGE_PHOTONAME, "")
);
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
}
/**
* Switches the text of two pages with each other.
*
* @param int $pageId1
* @param int $pageId2
*
* @return bool indicating success
*/
public function swapPages(int $pageId1, int $pageId2) : bool{
if(!$this->pageExists($pageId1) or !$this->pageExists($pageId2)){
return false;
}
$pageContents1 = $this->getPageText($pageId1);
$pageContents2 = $this->getPageText($pageId2);
$this->setPageText($pageId1, $pageContents2);
$this->setPageText($pageId2, $pageContents1);
return true;
}
public function getMaxStackSize() : int{
return 1;
}
/**
* Returns an array containing all pages of this book.
*
* @return CompoundTag[]
*/
public function getPages() : array{
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if($pages === null){
return [];
}
return $pages->getValue();
}
protected function getPagesTag() : ListTag{
return $this->getNamedTag()->getListTag(self::TAG_PAGES) ?? new ListTag([], NBT::TAG_Compound);
}
/**
* @param CompoundTag[] $pages
*
* @return $this
*/
public function setPages(array $pages) : self{
$nbt = $this->getNamedTag();
$nbt->setTag(self::TAG_PAGES, new ListTag($pages, NBT::TAG_Compound));
$this->setNamedTag($nbt);
return $this;
}
}

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\item;
class WrittenBook extends WritableBook{
class WrittenBook extends WritableBookBase{
public const GENERATION_ORIGINAL = 0;
public const GENERATION_COPY = 1;
@ -35,7 +35,7 @@ class WrittenBook extends WritableBook{
public const TAG_TITLE = "title"; //TAG_String
public function __construct(){
Item::__construct(self::WRITTEN_BOOK, 0, "Written Book");
parent::__construct(self::WRITTEN_BOOK, 0, "Written Book");
}
public function getMaxStackSize() : int{