Solved PlayerDeathEvent how to get killer? (if killer is Zombie or other monster)

Discussion in 'Plugin Development' started by bfgbfggf, Jan 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bfgbfggf

    How to do that?
    Code:
            Player pKiller = e.getEntity().getKiller();
            if (pKiller != null) {
                // some other code
            } else {
                DamageCause dmgCause = e.getEntity().getLastDamageCause().getCause();
                if (dmgCause.equals(DamageCause.ENTITY_ATTACK)) {
                            //And I want check what type of monster killed that player
                }
    That possible using that event?
     
  2. Offline

    Minecrell

    bfgbfggf
    Use something like that: (Source)
    Code:java
    1. public static Entity getKiller(EntityDeathEvent event) {
    2. EntityDamageEvent entityDamageEvent = event.getEntity().getLastDamageCause();
    3. if ((entityDamageEvent != null) && !entityDamageEvent.isCancelled() && (entityDamageEvent instanceof EntityDamageByEntityEvent)) {
    4. Entity damager = ((EntityDamageByEntityEvent) entityDamageEvent).getDamager();
    5.  
    6. if (damager instanceof Projectile) {
    7. LivingEntity shooter = ((Projectile) damager).getShooter();
    8. if (shooter != null) return shooter;
    9. }
    10.  
    11. return damager;
    12. }
    13.  
    14. return null;
    15. }

    You need to check if the damager is a projectile, because you will get an arrow as the killer of an entity instead of the skeleton for example if you don't check the shooter.
     
    GameplayJDK and bfgbfggf like this.
  3. Offline

    bfgbfggf

    Thanks! :)
     
Thread Status:
Not open for further replies.

Share This Page