Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Improved Inventory->addItem(), fixed breaking containers duplicating …
Browse files Browse the repository at this point in the history
…the last slot, removed not necessary slot changes
  • Loading branch information
shoghicp committed Nov 27, 2014
1 parent a3b1d31 commit 6d09754
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
17 changes: 12 additions & 5 deletions src/pocketmine/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -1628,10 +1628,13 @@ public function handleDataPacket(DataPacket $packet){
$this->inventory->sendHeldItem($this);
}else{
$item = $this->inventory->getItemInHand();
$oldItem = clone $item;
//TODO: Implement adventure mode checks
if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
$this->inventory->setItemInHand($item, $this);
$this->inventory->sendHeldItem($this->hasSpawned);
if(!$item->equals($oldItem, true) or $item->getCount() !== $oldItem->getCount()){
$this->inventory->setItemInHand($item, $this);
$this->inventory->sendHeldItem($this->hasSpawned);
}
break;
}
}
Expand Down Expand Up @@ -1802,13 +1805,17 @@ public function handleDataPacket(DataPacket $packet){
if($this->isCreative()){
$item = $this->inventory->getItemInHand();
}else{
$item = clone $this->inventory->getItemInHand();
$item = $this->inventory->getItemInHand();
}

$oldItem = clone $item;

if($this->level->useBreakOn($vector, $item, $this) === true){
if($this->isSurvival()){
$this->inventory->setItemInHand($item, $this);
$this->inventory->sendHeldItem($this->hasSpawned);
if(!$item->equals($oldItem, true) or $item->getCount() !== $oldItem->getCount()){
$this->inventory->setItemInHand($item, $this);
$this->inventory->sendHeldItem($this->hasSpawned);
}
}
break;
}
Expand Down
75 changes: 50 additions & 25 deletions src/pocketmine/inventory/BaseInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,41 +233,58 @@ public function addItem(...$slots){
$source = null;
}

/** @var Item[] $itemSlots */
/** @var Item[] $slots */
foreach($slots as $i => $slot){
$slots[$i] = clone $slot;
$itemSlots = [];
foreach($slots as $slot){
if($slot->getId() !== 0 and $slot->getCount() > 0){
$itemSlots[] = clone $slot;
}
}

$emptySlots = [];

for($i = 0; $i < $this->getSize(); ++$i){
$item = $this->getItem($i);
foreach($slots as $index => $slot){
if($item->getID() === Item::AIR or $item->getCount() <= 0){
if($item->getId() === Item::AIR or $item->getCount() <= 0){
$emptySlots[] = $i;
}

foreach($itemSlots as $index => $slot){
if($slot->equals($item, true) and $item->getCount() < $item->getMaxStackSize()){
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
if($amount > 0){
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() + $amount);
$this->setItem($i, $item, $source);
if($slot->getCount() <= 0){
unset($itemSlots[$index]);
}
}
}
}

if(count($itemSlots) === 0){
break;
}
}

if(count($itemSlots) > 0 and count($emptySlots) > 0){
foreach($emptySlots as $slotIndex){
foreach($itemSlots as $index => $slot){
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
$slot->setCount($slot->getCount() - $amount);
$item = clone $slot;
$item->setCount($amount);
$this->setItem($i, $item, $source);
$item = $this->getItem($i);
$this->setItem($slotIndex, $item, $source);
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, $source);
if($slot->getCount() <= 0){
unset($slots[$index]);
unset($itemSlots[$index]);
}
}
}

if(count($slots) === 0){
break;
}
}

return $slots;
return $itemSlots;
}

public function removeItem(...$slots){
Expand All @@ -279,31 +296,39 @@ public function removeItem(...$slots){
$source = null;
}

/** @var Item[] $itemSlots */
/** @var Item[] $slots */
$itemSlots = [];
foreach($slots as $slot){
if($slot->getId() !== 0 and $slot->getCount() > 0){
$itemSlots[] = clone $slot;
}
}

for($i = 0; $i < $this->getSize(); ++$i){
$item = $this->getItem($i);
if($item->getID() === Item::AIR){
if($item->getId() === Item::AIR or $item->getCount() <= 0){
continue;
}

foreach($slots as $index => $slot){
foreach($itemSlots as $index => $slot){
if($slot->equals($item, $slot->getDamage() === null ? false : true)){
$amount = min($item->getCount(), $slot->getCount());
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() - $amount);
$this->setItem($i, $item, $source);
if($slot->getCount() <= 0){
unset($slots[$index]);
unset($itemSlots[$index]);
}
}
}

if(count($slots) === 0){
if(count($itemSlots) === 0){
break;
}
}

return $slots;
return $itemSlots;
}

public function clear($index, $source = null){
Expand Down
2 changes: 1 addition & 1 deletion src/pocketmine/item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public function isShears(){
}

final public function __toString(){
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")";
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x".$this->count;
}

public function getDestroySpeed(Block $block, Player $player){
Expand Down
4 changes: 2 additions & 2 deletions src/pocketmine/level/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ public function useBreakOn(Vector3 $vector, Item &$item = null, Player $player =
$tile->unpair();
}

foreach($tile->getInventory()->getContents() as $item){
$this->dropItem($target, $item);
foreach($tile->getInventory()->getContents() as $chestItem){
$this->dropItem($target, $chestItem);
}
}

Expand Down

0 comments on commit 6d09754

Please sign in to comment.