Inventory: Added includeEmpty parameter to getContents()

This commit is contained in:
Dylan K. Taylor 2018-01-22 21:08:40 +00:00
parent 2fb580db26
commit 1de7c5b114
3 changed files with 15 additions and 12 deletions

View File

@ -93,10 +93,20 @@ abstract class BaseInventory implements Inventory{
}
/**
* @param bool $includeEmpty
*
* @return Item[]
*/
public function getContents() : array{
return array_filter($this->slots->toArray(), function(Item $item = null){ return $item !== null; });
public function getContents(bool $includeEmpty = false) : array{
$contents = [];
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
$item = $this->getItem($i);
if($includeEmpty or !$item->isNull()){
$contents[$i] = $item;
}
}
return $contents;
}
/**

View File

@ -71,15 +71,6 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
return $index < $this->left->getSize() ? $this->left->clear($index, $send) : $this->right->clear($index - $this->right->getSize(), $send);
}
public function getContents() : array{
$contents = [];
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
$contents[$i] = $this->getItem($i);
}
return $contents;
}
/**
* @param Item[] $items
* @param bool $send

View File

@ -108,9 +108,11 @@ interface Inventory{
public function removeItem(Item ...$slots) : array;
/**
* @param bool $includeEmpty
*
* @return Item[]
*/
public function getContents() : array;
public function getContents(bool $includeEmpty = false) : array;
/**
* @param Item[] $items