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!
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 } }
It's because the killer is null, You should always check that your objects are not null before performing operations on them.
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 }
Use playerdeathevent and cast after checking if it is not null EDIT: @TheBlackTeddy those comments made me want to "crack" up
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?