From a6039ad7338c439531c58504b10e5edab2006254 Mon Sep 17 00:00:00 2001 From: aieuo <35859665+aieuo@users.noreply.github.com> Date: Sun, 27 Jun 2021 00:48:53 +0900 Subject: [PATCH] Fixed InventoryHelpersTrait::addItem() cannot add items with a count greater than maxstack (#4283) --- src/inventory/InventoryHelpersTrait.php | 4 +++- tests/phpunit/inventory/BaseInventoryTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/inventory/InventoryHelpersTrait.php b/src/inventory/InventoryHelpersTrait.php index 881a65d73..00bec34c7 100644 --- a/src/inventory/InventoryHelpersTrait.php +++ b/src/inventory/InventoryHelpersTrait.php @@ -186,7 +186,9 @@ trait InventoryHelpersTrait{ $item = clone $slot; $item->setCount($amount); $this->setItem($slotIndex, $item); - break; + if($slot->getCount() <= 0){ + break; + } } } diff --git a/tests/phpunit/inventory/BaseInventoryTest.php b/tests/phpunit/inventory/BaseInventoryTest.php index 89c65c813..8fd7f43ea 100644 --- a/tests/phpunit/inventory/BaseInventoryTest.php +++ b/tests/phpunit/inventory/BaseInventoryTest.php @@ -85,4 +85,19 @@ class BaseInventoryTest extends TestCase{ } self::assertSame(20, $leftoverCount); } + + public function testAddItemWithOversizedCount() : void{ + $inventory = new class(10) extends SimpleInventory{ + + }; + $leftover = $inventory->addItem(VanillaItems::APPLE()->setCount(100)); + self::assertCount(0, $leftover); + + $count = 0; + foreach($inventory->getContents() as $item){ + self::assertTrue($item->equals(VanillaItems::APPLE())); + $count += $item->getCount(); + } + self::assertSame(100, $count); + } }