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

How to get killer from PlayerDeathEvent?

Discussion in 'Development' started by Zuruki, Apr 8, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I looked at the API and there is no getDamager(), it's only for EntityDamageEvent. If I want to define $killer as the killer of someone, how do I do it?

    Thankyou :)
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You can do it by using EntityDeathEvent::getEntity()->getLastDamageCause()->getDamager(), HOWEVER, it will only work if the last damage is an instance of EntityDamageByEntityEvent, so you should perform a check for that.
     
    Zuruki likes this.
  3. Jonas

    Jonas Baby Zombie

    Messages:
    192
    Ich denke das funktioniert
    PHP:
     $lastDmg $spieler-> getLastDamageCause ();
       If (
    $lastDmg instanceof EntityDamageEvent) {
          If (
    $lastDmg Instanz von EntityDamageByEntityEvent) {
          
    $Killer $lastDmg->getDamager (); [/ PHP] [/ PHP] [/ PHP]
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You don't need to check for both EntityDamageEvent and EntityDamageByEntityEvent in this case. An event can only be an instance of EntityDamageByEntityEvent if it is also an instance of EntityDamageEvent, because the first extends the latter. So you only need to perform the second check.
     
  5. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I'm still confused Cory. Please can you write some code man?
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I could, but I'll just redirect you to a good example somebody else already made here. You can use PlayerDeathEvent or EntityDeathEvent, although PlayerDeathEvent removes the need for some checks on what $event->getEntity() may be. This has already been asked in at least two or three other places, too, so you can look around if you are still unclear.
     
  7. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
  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.