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

Help with my plugin

Discussion in 'Development' started by Levi, Aug 6, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I want to remove TNT from players when they have it inside their inventory

    What I have now is

    PHP:
    public function held(PlayerItemHeldEvent $event){
                
    $player $event->getPlayer();
                
    $item $player->getInventory()->getItemInHand()->getId();
                    
                if(
    $item === 46){
                    
    $player->getInventory()->remove(Item::get($item));
                }
            }
    and this only removes the item when player holds it
     
  2. Derpific

    Derpific Spider Jockey

    Messages:
    44
    GitHub:
    Derpific
    You are using PlayerItemHeldEvent. So the code only runs when the player is Holding the TNT.
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    what should i do?
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    nothing?
    it works as expected
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    EntityInventoryChangeEvent. I don't know why this event is soo underrated. People prefer using DataPacket* events for handling changing an item's name when a player receives an item while they could be using this event.
    PHP:
    /** @var EntityInventoryChangeEvent $event */
    $player $event->getEntity();
    if(
    $player instanceof Player){
        if(
    $event->getNewItem() === Item::TNT){
            
    $event->setCancelled();//or $event->setNewItem(Item::get(Item::AIR));
        
    }
    }
     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    if you're fast enough, you can place few blocks before the plugin removes it from your hand
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Exactly, that's why EntityInventoryChangeEvent is suitable here. It is called when an item from player's inventory is subtracted, added, removed, dropped etccitation needed
     
    Levi likes this.
  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.