Hello How do I check if the player gets hit by an Arrow? I couldn't find anything on the interwebz so yeah.
PHP: public function onHit(EntityDamageEvent $ev){ $player = $ev->getEntity(); $cause = $player->getLastDamageCause(); if($cause instanceof EntityDamageByEntityEvent && $ev->getCause() === EntityDamageEvent::CAUSE_PROJECTILE){ $damager = $cause->getDamager();
Why use last damage cause? It is the current damage cause. Check if $ev is EntityDamageByEntityEvent, not the $cause (last damage cause)!
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 }}
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 }}
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.