Some cleanup on WritableBook code, get rid of some CompoundTag magic

This commit is contained in:
Dylan K. Taylor 2017-11-17 10:42:46 +00:00
parent f3ee605cd3
commit 19315dfd06

View File

@ -44,8 +44,7 @@ class WritableBook extends Item{
* @return bool * @return bool
*/ */
public function pageExists(int $pageId) : bool{ public function pageExists(int $pageId) : bool{
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES); return isset($this->getPages()[$pageId]);
return $pages !== null and isset($pages[$pageId]);
} }
/** /**
@ -105,13 +104,11 @@ class WritableBook extends Item{
if($pageId < 0){ if($pageId < 0){
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range"); throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
} }
$namedTag = $this->getNamedTag();
/** @var CompoundTag[]|ListTag $pages */ $pages = $this->getPages();
$pages = $namedTag->getListTag(self::TAG_PAGES) ?? new ListTag(self::TAG_PAGES, [], NBT::TAG_Compound);
for($id = 0; $id <= $pageId; $id++){ for($id = 0; $id <= $pageId; $id++){
if(!$this->pageExists($id)){ if(!isset($pages[$id])){
$pages[$id] = new CompoundTag("", [ $pages[$id] = new CompoundTag("", [
new StringTag("text", ""), new StringTag("text", ""),
new StringTag("photoname", "") new StringTag("photoname", "")
@ -119,8 +116,7 @@ class WritableBook extends Item{
} }
} }
$namedTag->setTag($pages); $this->setPages($pages);
$this->setNamedTag($namedTag);
} }
/** /**
@ -131,14 +127,10 @@ class WritableBook extends Item{
* @return bool indicating success * @return bool indicating success
*/ */
public function deletePage(int $pageId) : bool{ public function deletePage(int $pageId) : bool{
if(!$this->pageExists($pageId)){ $pages = $this->getPages();
return false; unset($pages[$pageId]);
}
$namedTag = $this->getNamedTag(); $this->setPages(array_values($pages));
unset($namedTag->getListTag(self::TAG_PAGES)[$pageId]);
$this->pushPages($pageId, $namedTag);
$this->setNamedTag($namedTag);
return true; return true;
} }
@ -152,14 +144,17 @@ class WritableBook extends Item{
* @return bool indicating success * @return bool indicating success
*/ */
public function insertPage(int $pageId, string $pageText = "") : bool{ public function insertPage(int $pageId, string $pageText = "") : bool{
$namedTag = $this->getNamedTag(); $pages = $this->getPages();
if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){
$namedTag->pages = new ListTag("pages", []); $this->setPages(array_merge(
} array_slice($pages, 0, $pageId),
$this->pushPages($pageId, $namedTag, false); [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; return true;
} }
@ -188,52 +183,27 @@ class WritableBook extends Item{
return 1; 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. * Returns an array containing all pages of this book.
* *
* @return CompoundTag[] * @return CompoundTag[]
*/ */
public function getPages() : array{ public function getPages() : array{
$namedTag = $this->getNamedTag(); $pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if(!isset($namedTag->pages)){ if($pages === null){
return []; return [];
} }
return array_filter((array) $namedTag->pages, function(string $key){ return $pages->getValue();
return is_numeric($key); }
}, ARRAY_FILTER_USE_KEY);
/**
*
* @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);
} }
} }