Change or remove item when dropped

Discussion in 'Development' started by Zorlac, Sep 4, 2019.

  1. Zorlac

    Zorlac Spider

    Can't seem to find anything to either change an item dropped (manually by a player) to air, or just delete it when it hits the ground.

    Ideas? Scan entities around the player?

    Want to make it vanish in a puff of smoke.
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    The easiest way to do this is actually to cancel the PlayerDropItemEvent, and then drop the item yourself using
    then you got an ItemEntity which you can then kill (or change to air).
    This is what it would look like in a plugin:
    /** @var $player Player */

    $motion $player->getDirectionVector()->multiply(0.4);
    $itemEntity $player->level->dropItem($this->add(01.30), $item$motion40);

    //now you could start an delayed Task to kill this later with:
    Unfortunately this is duplicating some code (in Player.php), but still considerably less code than searching for the entity...
  3. Zorlac

    Zorlac Spider

    I finally figured it out after much digging around examples and PMMP code.

    The explosion effect wasn't exactly what I wanted, but otherwise it works! Drop item -> *POOF* item disappears, but only if the plugin was configured to disallow dropping of the item.

    public function onDropItem(PlayerDropItemEvent $event){
           if(!$this->canDropWand && $event->getItem()->getNamedTagEntry(self::WAND_TAG) !== null) {
               $explosion=new Explosion($event->getPlayer()->getPosition(),3,$this);
    What I failed miserably at realizing was that there was not YET an item on the ground to search for, since PlayerDropItemEvent is called before the item is actually dropped. Derppy noob misunderstanding, but it should have been obvious since $event->setCancelled() prevents the item from even leaving the inventory.

Share This Page

  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.