More consistent fluency in Item API

This commit is contained in:
Dylan K. Taylor 2019-03-23 12:20:35 +00:00
parent 1045088668
commit d4fe004375
5 changed files with 41 additions and 20 deletions

View File

@ -83,9 +83,12 @@ abstract class Armor extends Durable{
* Sets the dyed colour of this armour piece. This generally only applies to leather armour. * Sets the dyed colour of this armour piece. This generally only applies to leather armour.
* *
* @param Color $color * @param Color $color
*
* @return $this
*/ */
public function setCustomColor(Color $color) : void{ public function setCustomColor(Color $color) : self{
$this->getNamedTag()->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($color->toARGB())); $this->getNamedTag()->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($color->toARGB()));
return $this;
} }
/** /**

View File

@ -77,8 +77,10 @@ class Banner extends Item{
/** /**
* @param Deque|BannerPattern[] $patterns * @param Deque|BannerPattern[] $patterns
*
* @return $this
*/ */
public function setPatterns(Deque $patterns) : void{ public function setPatterns(Deque $patterns) : self{
$tag = new ListTag(); $tag = new ListTag();
/** @var BannerPattern $pattern */ /** @var BannerPattern $pattern */
foreach($patterns as $pattern){ foreach($patterns as $pattern){
@ -88,6 +90,7 @@ class Banner extends Item{
); );
} }
$this->getNamedTag()->setTag(self::TAG_PATTERNS, $tag); $this->getNamedTag()->setTag(self::TAG_PATTERNS, $tag);
return $this;
} }
public function getFuelTime() : int{ public function getFuelTime() : int{

View File

@ -48,9 +48,12 @@ abstract class Durable extends Item{
* Sets whether the item will take damage when used. * Sets whether the item will take damage when used.
* *
* @param bool $value * @param bool $value
*
* @return $this
*/ */
public function setUnbreakable(bool $value = true) : void{ public function setUnbreakable(bool $value = true) : self{
$this->getNamedTag()->setByte("Unbreakable", $value ? 1 : 0); $this->getNamedTag()->setByte("Unbreakable", $value ? 1 : 0);
return $this;
} }
/** /**

View File

@ -75,13 +75,11 @@ class WritableBook extends Item{
* @param int $pageId * @param int $pageId
* @param string $pageText * @param string $pageText
* *
* @return bool indicating whether the page was created or not. * @return $this
*/ */
public function setPageText(int $pageId, string $pageText) : bool{ public function setPageText(int $pageId, string $pageText) : self{
$created = false;
if(!$this->pageExists($pageId)){ if(!$this->pageExists($pageId)){
$this->addPage($pageId); $this->addPage($pageId);
$created = true;
} }
/** @var CompoundTag[]|ListTag $pagesTag */ /** @var CompoundTag[]|ListTag $pagesTag */
@ -92,7 +90,7 @@ class WritableBook extends Item{
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag); $this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $created; return $this;
} }
/** /**
@ -100,8 +98,10 @@ class WritableBook extends Item{
* Creates a new page for every page between the given ID and existing pages that doesn't yet exist. * Creates a new page for every page between the given ID and existing pages that doesn't yet exist.
* *
* @param int $pageId * @param int $pageId
*
* @return $this
*/ */
public function addPage(int $pageId) : void{ public function addPage(int $pageId) : self{
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");
} }
@ -116,6 +116,7 @@ class WritableBook extends Item{
} }
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag); $this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return $this;
} }
/** /**
@ -123,13 +124,13 @@ class WritableBook extends Item{
* *
* @param int $pageId * @param int $pageId
* *
* @return bool indicating success * @return $this
*/ */
public function deletePage(int $pageId) : bool{ public function deletePage(int $pageId) : self{
$pagesTag = $this->getPagesTag(); $pagesTag = $this->getPagesTag();
$pagesTag->remove($pageId); $pagesTag->remove($pageId);
return true; return $this;
} }
/** /**
@ -138,9 +139,9 @@ class WritableBook extends Item{
* @param int $pageId * @param int $pageId
* @param string $pageText * @param string $pageText
* *
* @return bool indicating success * @return $this
*/ */
public function insertPage(int $pageId, string $pageText = "") : bool{ public function insertPage(int $pageId, string $pageText = "") : self{
$pagesTag = $this->getPagesTag(); $pagesTag = $this->getPagesTag();
$pagesTag->insert($pageId, CompoundTag::create() $pagesTag->insert($pageId, CompoundTag::create()
@ -150,7 +151,7 @@ class WritableBook extends Item{
$this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag); $this->getNamedTag()->setTag(self::TAG_PAGES, $pagesTag);
return true; return $this;
} }
/** /**
@ -197,12 +198,14 @@ class WritableBook extends Item{
} }
/** /**
*
* @param CompoundTag[] $pages * @param CompoundTag[] $pages
*
* @return $this
*/ */
public function setPages(array $pages) : void{ public function setPages(array $pages) : self{
$nbt = $this->getNamedTag(); $nbt = $this->getNamedTag();
$nbt->setTag(self::TAG_PAGES, new ListTag($pages, NBT::TAG_Compound)); $nbt->setTag(self::TAG_PAGES, new ListTag($pages, NBT::TAG_Compound));
$this->setNamedTag($nbt); $this->setNamedTag($nbt);
return $this;
} }
} }

View File

@ -56,14 +56,17 @@ class WrittenBook extends WritableBook{
* Sets the generation of a book. * Sets the generation of a book.
* *
* @param int $generation * @param int $generation
*
* @return $this
*/ */
public function setGeneration(int $generation) : void{ public function setGeneration(int $generation) : self{
if($generation < 0 or $generation > 3){ if($generation < 0 or $generation > 3){
throw new \InvalidArgumentException("Generation \"$generation\" is out of range"); throw new \InvalidArgumentException("Generation \"$generation\" is out of range");
} }
$namedTag = $this->getNamedTag(); $namedTag = $this->getNamedTag();
$namedTag->setInt(self::TAG_GENERATION, $generation); $namedTag->setInt(self::TAG_GENERATION, $generation);
$this->setNamedTag($namedTag); $this->setNamedTag($namedTag);
return $this;
} }
/** /**
@ -81,11 +84,14 @@ class WrittenBook extends WritableBook{
* Sets the author of this book. * Sets the author of this book.
* *
* @param string $authorName * @param string $authorName
*
* @return $this
*/ */
public function setAuthor(string $authorName) : void{ public function setAuthor(string $authorName) : self{
$namedTag = $this->getNamedTag(); $namedTag = $this->getNamedTag();
$namedTag->setString(self::TAG_AUTHOR, $authorName); $namedTag->setString(self::TAG_AUTHOR, $authorName);
$this->setNamedTag($namedTag); $this->setNamedTag($namedTag);
return $this;
} }
/** /**
@ -101,10 +107,13 @@ class WrittenBook extends WritableBook{
* Sets the author of this book. * Sets the author of this book.
* *
* @param string $title * @param string $title
*
* @return $this
*/ */
public function setTitle(string $title) : void{ public function setTitle(string $title) : self{
$namedTag = $this->getNamedTag(); $namedTag = $this->getNamedTag();
$namedTag->setString(self::TAG_TITLE, $title); $namedTag->setString(self::TAG_TITLE, $title);
$this->setNamedTag($namedTag); $this->setNamedTag($namedTag);
return $this;
} }
} }