From c47f1f572c3b46b197e91533843e4618ddc3a94f Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 27 Sep 2017 10:55:50 +0100 Subject: [PATCH] Added API method Item->pop() --- src/pocketmine/block/FlowerPot.php | 8 +------- src/pocketmine/block/ItemFrame.php | 12 ++---------- src/pocketmine/inventory/ShapelessRecipe.php | 8 ++------ src/pocketmine/item/Item.php | 19 +++++++++++++++++++ src/pocketmine/level/Level.php | 5 +---- src/pocketmine/tile/Furnace.php | 10 ++-------- 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/pocketmine/block/FlowerPot.php b/src/pocketmine/block/FlowerPot.php index d7e6f771f..ef26b73fb 100644 --- a/src/pocketmine/block/FlowerPot.php +++ b/src/pocketmine/block/FlowerPot.php @@ -112,14 +112,8 @@ class FlowerPot extends Flowable{ $this->setDamage(self::STATE_FULL); //specific damage value is unnecessary, it just needs to be non-zero to show an item. $this->getLevel()->setBlock($this, $this, true, false); - $pot->setItem($item); + $pot->setItem($item->pop()); - if($player instanceof Player){ - if($player->isSurvival()){ - $item->setCount($item->getCount() - 1); - $player->getInventory()->setItemInHand($item->getCount() > 0 ? $item : ItemFactory::get(Item::AIR)); - } - } return true; } diff --git a/src/pocketmine/block/ItemFrame.php b/src/pocketmine/block/ItemFrame.php index 23e8fc7f2..6c6f3195e 100644 --- a/src/pocketmine/block/ItemFrame.php +++ b/src/pocketmine/block/ItemFrame.php @@ -63,16 +63,8 @@ class ItemFrame extends Flowable{ if($tile->hasItem()){ $tile->setItemRotation(($tile->getItemRotation() + 1) % 8); - }else{ - if($item->getCount() > 0){ - $frameItem = clone $item; - $frameItem->setCount(1); - $item->setCount($item->getCount() - 1); - $tile->setItem($frameItem); - if($player instanceof Player and $player->isSurvival()){ - $player->getInventory()->setItemInHand($item->getCount() <= 0 ? ItemFactory::get(Item::AIR) : $item); - } - } + }elseif(!$item->isNull()){ + $tile->setItem($item->pop()); } return true; diff --git a/src/pocketmine/inventory/ShapelessRecipe.php b/src/pocketmine/inventory/ShapelessRecipe.php index b33445d52..99034fc78 100644 --- a/src/pocketmine/inventory/ShapelessRecipe.php +++ b/src/pocketmine/inventory/ShapelessRecipe.php @@ -82,12 +82,8 @@ class ShapelessRecipe implements CraftingRecipe{ throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients"); } - $it = clone $item; - $it->setCount(1); - while($item->getCount() > 0){ - $this->ingredients[] = clone $it; - $item->setCount($item->getCount() - 1); + $this->ingredients[] = $item->pop(); } return $this; @@ -105,7 +101,7 @@ class ShapelessRecipe implements CraftingRecipe{ } if($ingredient->equals($item, !$item->hasAnyDamageValue(), $item->hasCompoundTag())){ unset($this->ingredients[$index]); - $item->setCount($item->getCount() - 1); + $item->pop(); } } diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 25d881c33..7759a2860 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -643,6 +643,25 @@ class Item implements ItemIds, \JsonSerializable{ return $this; } + /** + * Pops an item from the stack and returns it, decreasing the stack count of this item stack by one. + * @return Item + * + * @throws \InvalidStateException if the count is less than or equal to zero, or if the stack is air. + */ + public function pop() : Item{ + if($this->isNull()){ + throw new \InvalidStateException("Cannot pop an item from a null stack"); + } + + $item = clone $this; + $item->setCount(1); + + $this->count--; + + return $item; + } + public function isNull() : bool{ return $this->count <= 0 or $this->id === Item::AIR; } diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 0268d83f8..bce059ee0 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1823,10 +1823,7 @@ class Level implements ChunkManager, Metadatable{ $this->broadcastLevelSoundEvent($hand, LevelSoundEventPacket::SOUND_PLACE, 1, $hand->getId()); } - $item->setCount($item->getCount() - 1); - if($item->getCount() <= 0){ - $item = ItemFactory::get(Item::AIR, 0, 0); - } + $item->pop(); return true; } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 8c130d27d..54725edda 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -201,10 +201,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ } if($this->namedtag->BurnTime->getValue() > 0 and $ev->isBurning()){ - $fuel->setCount($fuel->getCount() - 1); - if($fuel->getCount() === 0){ - $fuel = ItemFactory::get(Item::AIR, 0, 0); - } + $fuel->pop(); $this->inventory->setFuel($fuel); } } @@ -241,10 +238,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ if(!$ev->isCancelled()){ $this->inventory->setResult($ev->getResult()); - $raw->setCount($raw->getCount() - 1); - if($raw->getCount() === 0){ - $raw = ItemFactory::get(Item::AIR, 0, 0); - } + $raw->pop(); $this->inventory->setSmelting($raw); }