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

InventoryPickupItemEvent

Discussion in 'Development' started by SergeyIvanov, Jul 24, 2017.

  1. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    Hi, i have a problem! Item is not remove and effect not add to player, what is problem?

    Code:
    public function PickUp(InventoryPickupItemEvent $e){
    if($e->getItem()->getId() == 360){
    $e->getInventory()->removeItem(Item::get(360, 0 ,1));
    $e->getInventory()->getHolder()->addEffect(Effect::getEffect(1)->setDuration(20*100)->setVisible(false));
    }
    }
    
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    You want to remove item from player's inventory not event
     
  3. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    Ohhh, help please.
     
  4. Palente

    Palente Slime

    Messages:
    75
    GitHub:
    palente
    See the error in the log
    i think that would working:
    PHP:
    public function PickUp(InventoryPickupItemEvent $e){
    $player $e->getPlayer();
    if(
    $e->getItem()->getId() == 360){
    $player->getInventory()->removeItem(Item::get(360,1));
    $player->getInventory()->getHolder()->addEffect(Effect::getEffect(1)->setDuration(20*100)->setVisible(false));
    }
    }
    I think.
     
  5. DeNight

    DeNight Spider

    Messages:
    9
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You cannot remove an item that isn't in the player's inventory. Once the event has been listened by all the listeners, the item, if exists, is added to the player's inventory. You'll need to cancel the item and close the item entity. InventoryPickupItemEvent::getItem() returns item entity and not \pocketmine\item\Item. So InventoryPickupItemEvent::getItem()->getId() will always return the entity runtime ID of the item entity.
    This is probably what you were trying to do:
    PHP:
    /**
     * @param InventoryPickupItemEvent $e
     * @priority HIGHEST
     */
    public function PickUp(InventoryPickupItemEvent $e){
        
    $itemEntity $e->getItem();
        
    $item $itemEntity->getItem();
        if(
    $item->getId() === 360){
            
    //Remove the item entity and do not let the player pick up the item.
            
    $itemEntity->kill();
            
    $event->setCancelled();

            
    //Add effect:
            
    $effect =
    Effect::getEffect(Effect::SPEED)->setDuration(20 100)->setVisible(false);
            
    $event->getPlayer()->addEffect($effect);
        }
    }
     
  7. DeNight

    DeNight Spider

    Messages:
    9
    The effect is not given to the player, because it does not exist in InventoryPickupItemEvent.
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What? What's an effect to do with an event?? Do you know what you're doing?
     
    jasonwynn10 likes this.
  9. DeNight

    DeNight Spider

    Messages:
    9
    My code, it's working
    public function PickUp(InventoryPickupItemEvent $e){
    $itemEntity = $e->getItem();
    $item = $itemEntity->getItem();
    if($item->getId() === 360){
    $itemEntity->kill();
    $e->setCancelled();

    $effect = Effect::getEffect(Effect::SPEED)->setDuration(20 * 100)->setVisible(false);
    foreach ($this->getServer()->getOnlinePlayers() as $p) {
    $player = $p->getPlayer();
    $player->addEffect($effect);
    }
    }
    }
     
    Palente likes this.
  10. DeNight

    DeNight Spider

    Messages:
    9
    thanks for the help
     
  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.