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

Arrow damage check

Discussion in 'Development' started by Matthew, May 14, 2017.

  1. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    Hello

    How do I check if the player gets hit by an Arrow?

    I couldn't find anything on the interwebz so yeah.
     
  2. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    ProjectileHitEvent!
    Check if entity is instanceof Arrow
     
  3. Defications2po

    Defications2po Witch

    Messages:
    51
    PHP:
      public function onHit(EntityDamageEvent $ev){
                 
    $player $ev->getEntity();
                  
    $cause $player->getLastDamageCause();
                  if(
    $cause instanceof EntityDamageByEntityEvent && $ev->getCause() === EntityDamageEvent::CAUSE_PROJECTILE){
                                   
    $damager $cause->getDamager();
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Why use last damage cause? It is the current damage cause. Check if $ev is EntityDamageByEntityEvent, not the $cause (last damage cause)!
     
    Matthew, corytortoise and jasonwynn10 like this.
  5. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Not necessary for all the other checks (checking the damage cause).
    PHP:
    public function onEntityDamage(EntityDamageEvent $ev//pocketmine\event\entity\EntityDamageEvent
    {
        if((
    $dm $ev->getDamager()) && $dm instanceof Arrow && ($p $ev->getEntity()) && $p instanceof Player//pocketmine\Player & pocketmine\entity\Arrow
        
    {
           
    $dm $dm->shootingEntity//this returns a LIVING entity, not just a player
           //tada
        
    }
    }
     
    Matthew and corytortoise like this.
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    removed redundency
    PHP:
    public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $ev)
    {
        if((
    $dm $ev->getDamager())  instanceof \pocketmine\entity\Arrow && ($p $ev->getEntity()) instanceof \pocketmine\Player)
        {
           
    /** @var $dm \pocketmine\entity\Living */
           
    $dm $dm->shootingEntity;
           
    //tada
        
    }
    }
     
    Matthew and 0x15f like this.
  7. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    No, you still need to check if it is an instance of an EntityDamageByEntityEvent or EntityDamageByChildEntityEvent(method is EntityDamageByChildEntityEvent::getChild), otherwise you'll have errors every time something is damaged.
     
    SOFe, Matthew and Lowkey like this.
  8. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Whoopsies
     
    Matthew likes this.
  9. Lowkey

    Lowkey Slime

    Messages:
    94
    Yeah, check if it's by child. Child list are on the documentation.
     
    Matthew 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.