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

How do I check if someone is wearing something?

Discussion in 'Development' started by DiamondGamer30, Nov 26, 2017.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    How do I check if someone is wearing something?
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Muqsit likes this.
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    EntityArmorChangeEvent?
     
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    To check if a player is wearing something, e.g. Diamond Helmet, you can use:
    PHP:
    /**
     * @var Player $player
     */

    if($player->getInventory()->getHelmet()->getId() === Item::DIAMOND_HELMET) {
        
    // do something
    }
    You don’t need to use an event if you just want to check if a player is wearing something.
    If you want to run some code after the player put on something, use the EntityArmorChangeEvent:
    PHP:
    public function onArmorChange(EntityArmorChangeEvent $event) {
        
    $entity $event->getEntity();
        if(
    $entity instanceof Player) { // if entity is a player
            // do something
            // use getNewItem() to get the item which will be in the slot after the event.
        
    }
    }
     
    QuiverlyRivalry likes this.
  6. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Really depends where you want to use it, example a command.
     
    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.