Added API method Item->pop()

This commit is contained in:
Dylan K. Taylor 2017-09-27 10:55:50 +01:00
parent 7a77bb0402
commit c47f1f572c
6 changed files with 27 additions and 35 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}