Inventory: added getAddableItemQuantity()

this mostly reuses the code from canAddItem().
This commit is contained in:
Dylan K. Taylor 2021-10-11 21:46:27 +01:00
parent d73ea8efe4
commit a5833327f0
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 11 additions and 2 deletions

View File

@ -169,6 +169,10 @@ abstract class BaseInventory implements Inventory{
}
public function canAddItem(Item $item) : bool{
return $this->getAddableItemQuantity($item) === $item->getCount();
}
public function getAddableItemQuantity(Item $item) : int{
$count = $item->getCount();
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
$slot = $this->getItem($i);
@ -181,11 +185,11 @@ abstract class BaseInventory implements Inventory{
}
if($count <= 0){
return true;
return $item->getCount();
}
}
return false;
return $item->getCount() - $count;
}
public function addItem(Item ...$slots) : array{

View File

@ -63,6 +63,11 @@ interface Inventory{
*/
public function canAddItem(Item $item) : bool;
/**
* Returns how many items from the given itemstack can be added to this inventory.
*/
public function getAddableItemQuantity(Item $item) : int;
/**
* Removes the given Item from the inventory.
* It will return the Items that couldn't be removed.