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)); } }
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, 0 ,1));$player->getInventory()->getHolder()->addEffect(Effect::getEffect(1)->setDuration(20*100)->setVisible(false));}} I think.
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); }}
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); } } }