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

Solved NPC Damage

Discussion in 'Development' started by dbcooper, Sep 23, 2018.

  1. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Earlier today, I had the help of another dev and they helped me spawn an NPC.

    My main issue now is that the npc can be killed. I’ve tried the $npc->setMaxHealth(9999); and $npc->setHealth(9999); it works for the first join but once the server reboots, it resets it back down to 20hp.

    Is there anyway to make it so that the NPC cannot be hit/hurt.
     
  2. Angel

    Angel Spider Jockey

    Messages:
    44
    GitHub:
    aimjel
    PHP:
        public function damage(EntityDamageEvent $event) : void {

            if(
    $event->getEntity()::NETWORK_ID == Entity::NPC) {
                
    $event->setCancelled(true);
            }
        }
     
    RyanShaw likes this.
  3. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Tried this, it still takes damage...
    I’m using this to spawn it in. https://github.com/xXNiceYT/NPC-Test
     
    xXNiceAssassinlo YT likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You could $npc->setInvulnerable(true), then cancel EntityDamageEvent when $event->getEntity()->isInvulnerable()?
    I'm not sure why pocketmine doesn't do the isInvulnerable() check itself, perhaps #blameshoghi.
     
    dbcooper likes this.
  5. dbcooper

    dbcooper Spider Jockey

    Messages:
    32
    GitHub:
    dbcooper
    Thanks, i’ve figure it out. I put it into the EntityDamageEvent function and I called for its nametag. I can now have it not take damage and also execute a command.
     
  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.