Allow passing a Player source as last parameter on Inventory->addItem() and Inventory->removeItem()

This commit is contained in:
Shoghi Cervantes
2014-11-04 00:16:25 +01:00
parent a8c997d88a
commit 6b6222c09c
4 changed files with 29 additions and 12 deletions

View File

@ -225,6 +225,14 @@ abstract class BaseInventory implements Inventory{
}
public function addItem(...$slots){
if(isset($slots[$end = count($slots) - 1]) and $slots[$end] instanceof Player){
/** @var Player $source */
$source = $slots[$end];
unset($slots[$end]);
}else{
$source = null;
}
/** @var Item[] $slots */
foreach($slots as $i => $slot){
$slots[$i] = clone $slot;
@ -238,7 +246,7 @@ abstract class BaseInventory implements Inventory{
$slot->setCount($slot->getCount() - $amount);
$item = clone $slot;
$item->setCount($amount);
$this->setItem($i, $item);
$this->setItem($i, $item, $source);
$item = $this->getItem($i);
if($slot->getCount() <= 0){
unset($slots[$index]);
@ -247,7 +255,7 @@ abstract class BaseInventory implements Inventory{
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() + $amount);
$this->setItem($i, $item);
$this->setItem($i, $item, $source);
if($slot->getCount() <= 0){
unset($slots[$index]);
}
@ -263,6 +271,14 @@ abstract class BaseInventory implements Inventory{
}
public function removeItem(...$slots){
if(isset($slots[$end = count($slots) - 1]) and $slots[$end] instanceof Player){
/** @var Player $source */
$source = $slots[$end];
unset($slots[$end]);
}else{
$source = null;
}
/** @var Item[] $slots */
for($i = 0; $i < $this->getSize(); ++$i){
$item = $this->getItem($i);
@ -275,7 +291,7 @@ abstract class BaseInventory implements Inventory{
$amount = min($item->getCount(), $slot->getCount());
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() - $amount);
$this->setItem($i, $item);
$this->setItem($i, $item, $source);
if($slot->getCount() <= 0){
unset($slots[$index]);
}

View File

@ -67,7 +67,7 @@ interface Inventory{
* Stores the given Items in the inventory. This will try to fill
* existing stacks and empty slots as well as it can.
*
* Returns the Items that did not fit
* Returns the Items that did not fit. A Player source can be set at the end
*
* @param Item ...$item
*
@ -86,7 +86,7 @@ interface Inventory{
/**
* Removes the given Item from the inventory.
* It will return the Items that couldn't be removed.
* It will return the Items that couldn't be removed. A Player source can be set at the end
*
* @param Item ...$item
*