Added API method Item->pop()

This commit is contained in:
Dylan K. Taylor
2017-09-27 10:55:50 +01:00
parent 7a77bb0402
commit c47f1f572c
6 changed files with 27 additions and 35 deletions

View File

@ -643,6 +643,25 @@ class Item implements ItemIds, \JsonSerializable{
return $this;
}
/**
* Pops an item from the stack and returns it, decreasing the stack count of this item stack by one.
* @return Item
*
* @throws \InvalidStateException if the count is less than or equal to zero, or if the stack is air.
*/
public function pop() : Item{
if($this->isNull()){
throw new \InvalidStateException("Cannot pop an item from a null stack");
}
$item = clone $this;
$item->setCount(1);
$this->count--;
return $item;
}
public function isNull() : bool{
return $this->count <= 0 or $this->id === Item::AIR;
}