mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 19:02:59 +00:00
Allow passing a Player source as last parameter on Inventory->addItem() and Inventory->removeItem()
This commit is contained in:
@ -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]);
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
Reference in New Issue
Block a user