Death kill listener throwing error when you make suicide

Discussion in 'Spigot Plugin Development' started by mrdado243, Apr 21, 2017.

  1. So i'm making kill counter plugin and i made listener that listens for deaths and kills it looks something like this:
    Code (Text):
    @EventHandler
        public void killPlayer(EntityDeathEvent e) {
         
            Entity deadEntity = e.getEntity();
            Entity killer = e.getEntity().getKiller();
         
            if (killer instanceof Player && deadEntity instanceof Player) {
             
                Player player = (Player) killer;
                Player dead = (Player) deadEntity;
             
                int killcount = 0;
                int deathcount = 0;
                String killpath = "playerkills." + player.getName().toLowerCase();
                String deathpath = "playerdeaths." + dead.getName().toLowerCase();
             
             
                if (config.contains(killpath)) {
                    killcount = config.getInt(killpath);
                }
             
                if (config.contains(deathpath)) {
                    deathcount = config.getInt(deathpath);
                }
             
                config.set(killpath, killcount  +1);
                config.set(deathpath, deathcount  +1);
             
                plugin.saveConfig();
             
             
            }
    but when player make suicide bukkit does not give a killer so it throws an error but i tried adding this:

    Code (Text):
    else if (killer == null && deadEntity instanceof Player) {
                Player player = (Player) deadEntity;
                player.sendMessage("GJ!");
            }
    but it does not help me at all what should i do? i don't like errors spamming in console when players fall from world.. thanks for help guys!
     
  2. Can you please send us the error you get,so we can help you better?
     
  3. What about the PlayerDeathEvent first instead of EntityDeathEvent? :)
     
  4. here:
    http://prntscr.com/ez9btc
     
  5. entity.Player same stuff
     
  6. Actually its not really complicated, here's a simple answer on how to get the last damage cause

    Code (Text):


    @EventHandler
    public void playerDeath(PlayerDeathEvent e){
        Entity entity = e.getEntity();
        EntityDamageEvent entityDamageEvent = entity.getLastDamageCause();
        if(entityDamageEvent.getCause() == EntityDamageEvent.DamageCause.VOID){
            //Player died from void
        }
    }


     
     
    • Winner Winner x 1
  7. no still gives me error
     
  8. looks like the problem is that kill player event still counts this as a kill no mater what, ..
     
  9. It's because the killer is null, You should always check that your objects are not null before performing operations on them.
     
  10. i did but it did not work!
     
  11. In the stack trace it clearly says
    Code (Java):
    Caused by java.lang.NullPointerException
    Something is null and it's the killer, this is because there is no killer. That's why it's returning null. so you should do something like
    Code (Java):
    if(killer!=null){
        //Continue
    }
     
  12. still same error, ..
     
  13. You are casting before checking. Obviously it throws a npe.
     
  14. this helps me a lot. what should i do than?
     
  15. Obviously first check then cast.
     
  16. Use playerdeathevent and cast after checking if it is not null

    EDIT: @TheBlackTeddy those comments made me want to "crack" up
     
  17. Instance of checks are implicitly null checks, since null objects are practically 'type-less'.

    Regardless, the killer is always a Player by contract (the getKiller() method returns Player), so a null check is clearer anyway.

    @mrdado243 what line threw the NullPointerException?