Solved EntityDeathEvent error

Discussion in 'Plugin Development' started by Aragone, Jul 19, 2019.

  1. Offline

    Aragone

    Hello !

    I have an error with that code (Player killer = e.getEntity().getKiller(); ) : https://pastebin.com/raw/LYwqu5hG

    I already tried to cast to Player, to change method to detect the killer (Player killer = (Player) e.getEntity().getLastDamageCause().getEntity(); ). Nothing to do, I have an error...

    Thanks for ur help
     
    Last edited: Jul 19, 2019
  2. Online

    KarimAKL

    @Aragone What is the error? I'm guessing 'getKiller()' returns null.
     
  3. Offline

    Aragone

    Oh yes sorry, I've forgotten to give it to you.

    upload_2019-7-20_0-35-10.png

    Yes it returns null
     
  4. Online

    KarimAKL

    @Aragone You can't use variable that's null, make a null check before using it.
     
  5. Offline

    Aragone

    @KarimAKL Yes but when I test the code the variable musn't be null.

    I don't understand. If I kill the armorstand, I'm a player so the variable musn't be null.
     
    Last edited: Jul 20, 2019
  6. Online

    KarimAKL

    @Aragone How did you kill the entity? Are you sure that's the line causing the error? The error says it's line 141, but your code only shows 16 lines.
     
  7. Offline

    Aragone

  8. Online

    KarimAKL

    @Aragone In the pastebin it seems line 141 is this:
    Code:Java
    1. if(target.hasArms() == true) {

    If that's truly line 141 then 'target' is null, but i don't think e.getEntity() returns null at any time, so maybe pastebin has formatted your code wrong or something?
     
  9. Offline

    Aragone

    Last edited: Jul 20, 2019
  10. Online

    KarimAKL

    @Aragone Try adding something like this before you check the gamemode:
    Code:Java
    1. if (killer == null) {
    2. System.out.println("killer is null");
    3. return;
    4. } else System.out.println("killer is not null");
     
  11. Offline

    Aragone

    @KarimAKL It returns player is null. I suspected it. That's very strange I tried a lot of ways to get the player but there is nothing to do.
     
  12. Online

    KarimAKL

    @Aragone I'm not sure if this gives the same result but have you tried this?
    Code:Java
    1. EntityDamageEvent event = e.getEntity().getLastDamageCause();
    2. if (!(event instanceof EntityDamageByEntityEvent)) return;
    3. EntityDamageByEntityEvent ev = (EntityDamageByEntityEvent) event;
    4. if (!(ev.getDamager() instanceof Player)) return;
    5. Player killer = (Player) ev.getDamager();
     
  13. Offline

    Aragone

    @KarimAKL It runs, Thanks but that's crazy. I really don't understand why this method was not running well. Maybe it's deprecated.
     
  14. Online

    KarimAKL

    @Aragone If you don't understand the code, then i'll try explaining it.
    Explanation (open)
    Code:Java
    1. EntityDamageEvent event = e.getEntity().getLastDamageCause();

    Here we get the entity from the EntityDeathEvent and then get it's last damage cause. (the damage that caused it's death)
    Code:Java
    1. if (!(event instanceof EntityDamageByEntityEvent)) return;

    We then check if the EntityDamageEvent gotten from the last damage cause is an instanceof EntityDamageByEntityEvent, we can do this because EntityDamageByEntityEvent extends EntityDamageEvent.
    We don't care about the damage if it wasn't caused by an entity (player).
    Code:Java
    1. EntityDamageByEntityEvent ev = (EntityDamageByEntityEvent) event;

    Now we know that the event is an instanceof EntityDamageByEntityEvent, so we cast it to that so we can use it's methods. (it's only method is 'getDamager()', the rest is inherited from EntityDamageEvent)
    Code:Java
    1. if (!(ev.getDamager() instanceof Player)) return;

    Again, we don't care if the damage wasn't caused by a player.
    Code:Java
    1. Player killer = (Player) ev.getDamager();

    Now we can be sure that the damage was caused by a player, so we cast the Entity gotten from 'getDamager()' to a Player. That's it, now you've gotten the player.

    Anyway, i'm glad it worked for you. :)
     
  15. Offline

    Aragone

    Thank you very much you're a very good guy @KarimAKL
     

Share This Page