migrate to new CompoundTag API (#1515)

This commit is contained in:
Dylan K. Taylor
2017-11-10 15:38:21 +00:00
committed by GitHub
parent d4494687d1
commit aa399a1109
20 changed files with 305 additions and 370 deletions

View File

@@ -481,9 +481,7 @@ class Item implements ItemIds, \JsonSerializable{
public function getLore() : array{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if($display instanceof CompoundTag and ($lore = $display->getListTag(self::TAG_DISPLAY_LORE)) !== null){
return array_map(function(StringTag $tag) : string{
return $tag->getValue();
}, $lore->getValue());
return $lore->getAllValues();
}
return [];

View File

@@ -23,12 +23,15 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
class WritableBook extends Item{
const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
public function __construct(int $meta = 0){
parent::__construct(self::WRITABLE_BOOK, $meta, "Book & Quill");
}
@@ -41,7 +44,8 @@ class WritableBook extends Item{
* @return bool
*/
public function pageExists(int $pageId) : bool{
return isset($this->getNamedTag()->pages->{$pageId});
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
return $pages !== null and isset($pages[$pageId]);
}
/**
@@ -52,10 +56,17 @@ class WritableBook extends Item{
* @return string|null
*/
public function getPageText(int $pageId) : ?string{
if(!$this->pageExists($pageId)){
$pages = $this->getNamedTag()->getListTag(self::TAG_PAGES);
if($pages === null){
return null;
}
return $this->getNamedTag()->pages->{$pageId}->text->getValue();
$page = $pages[$pageId] ?? null;
if($page instanceof CompoundTag){
return $page->getString("text", "");
}
return null;
}
/**
@@ -74,7 +85,11 @@ class WritableBook extends Item{
}
$namedTag = $this->getNamedTag();
$namedTag->pages->{$pageId}->text->setValue($pageText);
/** @var CompoundTag[]|ListTag $pages */
$pages = $namedTag->getListTag(self::TAG_PAGES);
assert($pages instanceof ListTag);
$pages[$pageId]->setString("text", $pageText);
$this->setNamedTag($namedTag);
return $created;
@@ -92,19 +107,19 @@ class WritableBook extends Item{
}
$namedTag = $this->getNamedTag();
if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){
$namedTag->pages = new ListTag("pages", []);
}
/** @var CompoundTag[]|ListTag $pages */
$pages = $namedTag->getListTag(self::TAG_PAGES) ?? new ListTag(self::TAG_PAGES, [], NBT::TAG_Compound);
for($id = 0; $id <= $pageId; $id++){
if(!$this->pageExists($id)){
$namedTag->pages->{$id} = new CompoundTag("", [
$pages[$id] = new CompoundTag("", [
new StringTag("text", ""),
new StringTag("photoname", "")
]);
}
}
$namedTag->setTag($pages);
$this->setNamedTag($namedTag);
}
@@ -121,7 +136,7 @@ class WritableBook extends Item{
}
$namedTag = $this->getNamedTag();
unset($namedTag->pages->{$pageId});
unset($namedTag->getListTag(self::TAG_PAGES)[$pageId]);
$this->pushPages($pageId, $namedTag);
$this->setNamedTag($namedTag);
@@ -186,6 +201,9 @@ class WritableBook extends Item{
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)){
@@ -193,9 +211,9 @@ class WritableBook extends Item{
}
if($downwards){
unset($namedTag->pages->{$key});
unset($pagesTag[$key]);
}
$namedTag->pages->{$key + $type} = new CompoundTag("", [
$pagesTag[$key + $type] = new CompoundTag("", [
new StringTag("text", $page->text->getValue()),
new StringTag("photoname", "")
]);

View File

@@ -23,9 +23,6 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
class WrittenBook extends WritableBook{
const GENERATION_ORIGINAL = 0;
@@ -33,6 +30,10 @@ class WrittenBook extends WritableBook{
const GENERATION_COPY_OF_COPY = 2;
const GENERATION_TATTERED = 3;
const TAG_GENERATION = "generation"; //TAG_Int
const TAG_AUTHOR = "author"; //TAG_String
const TAG_TITLE = "title"; //TAG_String
public function __construct(int $meta = 0){
Item::__construct(self::WRITTEN_BOOK, $meta, "Written Book");
}
@@ -48,10 +49,7 @@ class WrittenBook extends WritableBook{
* @return int
*/
public function getGeneration() : int{
if(!isset($this->getNamedTag()->generation)) {
return -1;
}
return $this->getNamedTag()->generation->getValue();
return $this->getNamedTag()->getInt(self::TAG_GENERATION, -1);
}
/**
@@ -64,12 +62,7 @@ class WrittenBook extends WritableBook{
throw new \InvalidArgumentException("Generation \"$generation\" is out of range");
}
$namedTag = $this->getNamedTag();
if(isset($namedTag->generation)){
$namedTag->generation->setValue($generation);
}else{
$namedTag->generation = new IntTag("generation", $generation);
}
$namedTag->setInt(self::TAG_GENERATION, $generation);
$this->setNamedTag($namedTag);
}
@@ -81,10 +74,7 @@ class WrittenBook extends WritableBook{
* @return string
*/
public function getAuthor() : string{
if(!isset($this->getNamedTag()->author)){
return "";
}
return $this->getNamedTag()->author->getValue();
return $this->getNamedTag()->getString(self::TAG_AUTHOR, "");
}
/**
@@ -94,11 +84,7 @@ class WrittenBook extends WritableBook{
*/
public function setAuthor(string $authorName) : void{
$namedTag = $this->getNamedTag();
if(isset($namedTag->author)){
$namedTag->author->setValue($authorName);
}else{
$namedTag->author = new StringTag("author", $authorName);
}
$namedTag->setString(self::TAG_AUTHOR, $authorName);
$this->setNamedTag($namedTag);
}
@@ -108,10 +94,7 @@ class WrittenBook extends WritableBook{
* @return string
*/
public function getTitle() : string{
if(!isset($this->getNamedTag()->title)){
return "";
}
return $this->getNamedTag()->title->getValue();
return $this->getNamedTag()->getString(self::TAG_TITLE, "");
}
/**
@@ -121,11 +104,7 @@ class WrittenBook extends WritableBook{
*/
public function setTitle(string $title) : void{
$namedTag = $this->getNamedTag();
if(isset($namedTag->title)){
$namedTag->title->setValue($title);
}else{
$namedTag->title = new StringTag("title", $title);
}
$namedTag->setString(self::TAG_TITLE, $title);
$this->setNamedTag($namedTag);
}
}