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

Change or remove item when dropped

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

  1. Zorlac

    Zorlac Spider

    Messages:
    11
    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

    Messages:
    1,342
    GitHub:
    robske110
    The easiest way to do this is actually to cancel the PlayerDropItemEvent, and then drop the item yourself using https://github.com/pmmp/PocketMine-...801782c/src/pocketmine/Player.php#L2915-L2917
    then you got an ItemEntity which you can then kill (or change to air).
    This is what it would look like in a plugin:
    PHP:
    /** @var $player 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:
    $itemEntity->kil();
    Unfortunately this is duplicating some code (in Player.php), but still considerably less code than searching for the entity...
     
  3. Zorlac

    Zorlac Spider

    Messages:
    11
    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.


    Code:
     
    public function onDropItem(PlayerDropItemEvent $event){
           if(!$this->canDropWand && $event->getItem()->getNamedTagEntry(self::WAND_TAG) !== null) {
               $explosion=new Explosion($event->getPlayer()->getPosition(),3,$this);
               $explosion->explodeB();
               $playerInventory=$event->getPlayer()->getInventory();
               $playerInventory->removeItem($playerInventory->getItemInHand());
               $event->setCancelled();
           }
       }
    
    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.
     
  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.