1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Remove an item?

Discussion in 'Development' started by Junkdude, Nov 14, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    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);
            }
        }
     
    OnTheVerge likes this.
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Well, it does give 1 if you have
    PHP:
    Item::get(34001// 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..
     
    Last edited: Nov 14, 2016
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    doesnt work, as the item has a custom name
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Does this work?
    PHP:
    $item Item::get(34001);
    $p $ev->getPlayer()->getInventory()->getItemInHand();
    If(
    $p == $item and $p->getCustomName() == "name") {
    $p->removeItem($item)
    }
    This might be incorrect, this is what I remember
     
  5. Primus

    Primus Zombie Pigman

    Messages:
    749
    How about you handle that yourself, write your own removeItem function.
    PHP:
    # TODO: Write one when you get back home
     
    Matthew likes this.
  6. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Code:
    $i = $event->getItem;
    
    $p->getInventory()->removeItem($i);
    
    
    The issue with this is it will remove the whole stack not just 1 item
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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);
    }
     
    Jack Noordhuis and HimbeersaftLP like this.
  8. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Thank you! will test it later!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.