So I have a customname item called XP Book. Im trying to remove 1 from a stack, but it doesnt work. Any help? PHP: public function onInteract(PlayerInteractEvent $event){ $p = $event->getPlayer(); $x = $p->getX(); $y = $p->getY(); $z = $p->getZ(); $level = $p->getLevel(); $thing = $p->getInventory()->getItemInHand(); $book = Item::get(340,0,1,"{display:{Name:XP Book}}"); if ($thing->getId() === 340 and $thing->getCustomName() === "XP Book"){ #1 $p->addXpLevel(25); $p->sendMessage(c::GREEN . "25 Xp Levels Recieved!"); $level->addSound(new ExpPickupSound(new Vector3($x, $y + 1, $z))); $p->getInventory()->removeItem($book); } }
Well, it does give 1 if you have PHP: Item::get(340, 0, 1) // That 1 is the amount instead of a stack If you have CustomName. The server will find for that item that you've added, not remove 1 from it..
Does this work? PHP: $item = Item::get(340, 0, 1);$p = $ev->getPlayer()->getInventory()->getItemInHand();If($p == $item and $p->getCustomName() == "name") {$p->removeItem($item)} This might be incorrect, this is what I remember
How about you handle that yourself, write your own removeItem function. PHP: # TODO: Write one when you get back home
Code: $i = $event->getItem; $p->getInventory()->removeItem($i); The issue with this is it will remove the whole stack not just 1 item
To decrement item in hand: PHP: $item = $inventory->getItemInHand();if(isWhatYouAreLookingFor($item)){ // so this asserts that $item->count > 0 $item->count--; $inventory->setItemInHand($item);} To decrement an item wherever it is in the inventory: PHP: $index = $inventory->first(Item::get($id, $damage_or_null));if($index !== -1){ $item = $inventory->getItem($index); $item->count--; $inventory->setItem($index, $item);}