Entity Death Event Killer = null????

Discussion in 'Plugin Development' started by paxi, Jun 21, 2014.

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

    paxi

    Hello everybody,

    i have a small problem with the EntityDeathEvent:
    Code:java
    1. public void onKillEntity(EntityDeathEvent event){
    2. Player killer = event.getEntity().getKiller();
    3. Entity victim = event.getEntity();
    4.  
    5. double live= ((LivingEntity) victim).getMaxHealth();
    6. String playername = killer.getName();
    7. System.out.print(playername);


    Can someone say me why the killer is null because i always get a NullpointerException at the line where I want to get the playername because the killer is null?
     
  2. Offline

    Heirteir

    paxi
    event.getEntity().getLastDamageCause().getEntity()
    Will return the killer
    You also might want to check if the killer is instanceof Player
     
    paxi likes this.
  3. Offline

    The Fancy Whale

    Killers only return players and if the killer was not a player (for example fall damage) there is no killer. Just add a simple null check to see if the killer is null or not.
     
    paxi likes this.
  4. Offline

    paxi

    Okay i write now:
    Code:
      Entity victim = event.getEntity();
                    if(event.getEntity().getLastDamageCause().getCause()==DamageCause.ENTITY_ATTACK){
                    Entity killerE= event.getEntity().getLastDamageCause().getEntity();
                    if (killerE instanceof Player){
                        Player killer = (Player) killerE;
               
                double live= ((LivingEntity) victim).getMaxHealth();
            String playername = killer.getName();
            System.out.print(playername);
    But now i get a null pointer exception at this line:
    if(event.getEntity().getLastDamageCause().getCause()==DamageCause.ENTITY_ATTACK){

    When i kill as a Player an Entity!

    Why? :(

    I think the Problem is the Damagecause.Entity_attack because when i give all DamageCause of Mobs back i can get all back without the Entity_attack...

    Okay i tried a llitle bit....
    i can't compare the DamageCauses when i write:

    Entity deadE = event.getEntity();
    EntityDamageEvent deathCause = deadE.getLastDamageCause();
    if(deathCause.getCause() == DamageCause.something

    i always get an Error, did somebody know why? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    The Fancy Whale

    just do if (event.getEntity().getKiller() == null) return;
     
Thread Status:
Not open for further replies.

Share This Page