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

@ -1205,7 +1205,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->target = $entity->getID(); $pk->target = $entity->getID();
Server::broadcastPacket($entity->getViewers(), $pk); Server::broadcastPacket($entity->getViewers(), $pk);
$this->inventory->addItem(clone $item); $this->inventory->addItem(clone $item, $this);
$entity->kill(); $entity->kill();
} }
}elseif($entity instanceof DroppedItem){ }elseif($entity instanceof DroppedItem){
@ -1239,7 +1239,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->target = $entity->getID(); $pk->target = $entity->getID();
Server::broadcastPacket($entity->getViewers(), $pk); Server::broadcastPacket($entity->getViewers(), $pk);
$this->inventory->addItem(clone $item); $this->inventory->addItem(clone $item, $this);
$entity->kill(); $entity->kill();
} }
} }
@ -1648,7 +1648,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$snowball = Entity::createEntity("Snowball", $this->chunk, $nbt, $this); $snowball = Entity::createEntity("Snowball", $this->chunk, $nbt, $this);
$snowball->setMotion($snowball->getMotion()->multiply($f)); $snowball->setMotion($snowball->getMotion()->multiply($f));
if($this->isSurvival()){ if($this->isSurvival()){
$this->inventory->removeItem(Item::get(Item::SNOWBALL, 0, 1)); $this->inventory->removeItem(Item::get(Item::SNOWBALL, 0, 1), $this);
} }
if($snowball instanceof Projectile){ if($snowball instanceof Projectile){
$this->server->getPluginManager()->callEvent($projectileEv = new ProjectileLaunchEvent($snowball)); $this->server->getPluginManager()->callEvent($projectileEv = new ProjectileLaunchEvent($snowball));
@ -1713,7 +1713,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
}else{ }else{
$ev->getProjectile()->setMotion($ev->getProjectile()->getMotion()->multiply($ev->getForce())); $ev->getProjectile()->setMotion($ev->getProjectile()->getMotion()->multiply($ev->getForce()));
if($this->isSurvival()){ if($this->isSurvival()){
$this->inventory->removeItem(Item::get(Item::ARROW, 0, 1)); $this->inventory->removeItem(Item::get(Item::ARROW, 0, 1), $this);
$bow->setDamage($bow->getDamage() + 1); $bow->setDamage($bow->getDamage() + 1);
$this->inventory->setItemInHand($bow, $this); $this->inventory->setItemInHand($bow, $this);
if($bow->getDamage() >= 385){ if($bow->getDamage() >= 385){
@ -2008,7 +2008,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
--$slot->count; --$slot->count;
$this->inventory->setItemInHand($slot, $this); $this->inventory->setItemInHand($slot, $this);
if($slot->getID() === Item::MUSHROOM_STEW or $slot->getID() === Item::BEETROOT_SOUP){ if($slot->getID() === Item::MUSHROOM_STEW or $slot->getID() === Item::BEETROOT_SOUP){
$this->inventory->addItem(Item::get(Item::BOWL, 0, 1)); $this->inventory->addItem(Item::get(Item::BOWL, 0, 1), $this);
} }
} }
break; break;
@ -2178,7 +2178,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
case Item::CAKE: case Item::CAKE:
//TODO: detect complex recipes like cake that leave remains //TODO: detect complex recipes like cake that leave remains
$this->awardAchievement("bakeCake"); $this->awardAchievement("bakeCake");
$this->inventory->addItem(Item::get(Item::BUCKET, 0, 3)); $this->inventory->addItem(Item::get(Item::BUCKET, 0, 3), $this);
break; break;
case Item::STONE_PICKAXE: case Item::STONE_PICKAXE:
case Item::GOLD_PICKAXE: case Item::GOLD_PICKAXE:

View File

@ -22,6 +22,7 @@
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\Tool;
class Stone extends Solid{ class Stone extends Solid{
const NORMAL = 0; const NORMAL = 0;
@ -68,7 +69,7 @@ class Stone extends Solid{
} }
public function getDrops(Item $item){ public function getDrops(Item $item){
if($item->isPickaxe() >= 1){ if($item->isPickaxe() >= Tool::TIER_WOODEN){
return [ return [
[Item::COBBLESTONE, 0, 1], [Item::COBBLESTONE, 0, 1],
]; ];

View File

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

View File

@ -67,7 +67,7 @@ interface Inventory{
* Stores the given Items in the inventory. This will try to fill * Stores the given Items in the inventory. This will try to fill
* existing stacks and empty slots as well as it can. * 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 * @param Item ...$item
* *
@ -86,7 +86,7 @@ interface Inventory{
/** /**
* Removes the given Item from the 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 * @param Item ...$item
* *