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

How to get the damager in EntityDamageEvent

Discussion in 'Development' started by TwistedAsylumMC, May 14, 2018.

  1. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    How do I get the damager in EntityDamageEvent?
    I tried:
    PHP:
    if($entity->getLastDamageCause() instanceof EntityDamageByEntityEvent){
        
    $damager $entity->getLastDamageCause()->getDamager();
        if(
    $damager instanceof Player){
            
    $damager->sendMessage("Hi");
        }
    }
    but it didnt work, no errors or anything. But PhpStorm says getDamager() isn't a thing, but it is in EntityDamageByEntityEvent. Can someone help me out?
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What have you tried in debugging this? Have you tried to verify if either of the if loops executes?

    Also, you don't need to use getLastDamageCause() if you are currently handling a EntityDamageEvent.
     
  3. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    PHP:
    if($event instanceof EntityDamageByEntityEvent){
        
    $damager $event->getDamager();
        if(
    $damager instanceof Player){
            
    $damager->sendMessage("Hi");
        }
    }
     
  4. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    already tried, didn't work
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Have you imported the EntityDamageByEntityEvent and Player classes?
     
    xXNiceAssassinlo YT 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.