From 70e340086d9655ff8b0bf607b0df78e5512f4003 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 28 May 2014 15:08:07 +0200 Subject: [PATCH] Fixed Inventory::addItem() not adding items properly --- src/pocketmine/Player.php | 3 +- src/pocketmine/inventory/BaseInventory.php | 40 ++++++++++------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 1b1b2778f..3058cfcec 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1227,8 +1227,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; } - $this->inventory->addItem(clone $item); - $pk = new TakeItemEntityPacket; $pk->eid = 0; $pk->target = $entity->getID(); @@ -1238,6 +1236,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk->eid = $this->getID(); $pk->target = $entity->getID(); $this->server->broadcastPacket($entity->getViewers(), $pk); + $this->inventory->addItem(clone $item); $entity->kill(); } } diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 68bce1dbf..b5408bb29 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -228,33 +228,29 @@ abstract class BaseInventory implements Inventory{ /** @var Item[] $slots */ $slots = func_get_args(); foreach($slots as $i => $slot){ - while($slot->getCount() > $slot->getMaxStackSize()){ - $slots[] = Item::get($slot->getID(), $slot->getDamage(), $slot->getMaxStackSize()); - $slot->setCount($slot->getCount() - $slot->getMaxStackSize()); - if(count($slots) > $this->getSize()){ //Protect against large give commands - break; - } - } + $slots[$i] = clone $slot; } for($i = 0; $i < $this->getSize(); ++$i){ $item = $this->getItem($i); - if($item->getID() === Item::AIR){ - $this->setItem($i, array_shift($slots)); - $item = $this->getItem($i); - } - foreach($slots as $index => $slot){ - if($slot->equals($item, $slot->getDamage() === null ? false : true)){ - if($item->getCount() < $item->getMaxStackSize()){ - $amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize()); - $slot->setCount($slot->getCount() - $amount); - $old = clone $item; - $item->setCount($item->getCount() + $amount); - $this->onSlotChange($i, $old); - if($slot->getCount() <= 0){ - unset($slots[$index]); - } + if($item->getID() === Item::AIR){ + $amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize()); + $slot->setCount($slot->getCount() - $amount); + $item = clone $slot; + $item->setCount($amount); + $this->setItem($i, $item); + $item = $this->getItem($i); + if($slot->getCount() <= 0){ + unset($slots[$index]); + } + }elseif($slot->equals($item, true) and $item->getCount() < $item->getMaxStackSize()){ + $amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize()); + $slot->setCount($slot->getCount() - $amount); + $item->setCount($item->getCount() + $amount); + $this->setItem($i, $item); + if($slot->getCount() <= 0){ + unset($slots[$index]); } } }