Error with EntityDeathEvent

Discussion in 'Plugin Development' started by Xisko, Oct 23, 2020.

  1. Offline


    Well, I'm learning Java and im making my firsts plugins, Im having an error while doing a plugin where its announc a message when someone death from a player or a mob. I have been looking for people with the same error, but it doesnt fix mines, dont know if its because they are normally very old but it doesnt work.

    Im having this error
    [10:23:27] [Server thread/INFO]: Xisko was slain by Drowned
    [10:23:40] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to DeathMessages v1.0
    org.bukkit.event.EventException: null
    at$1.execute( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.SimplePluginManager.fireEvent( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.SimplePluginManager.callEvent( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory.callPlayerDeathEvent( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityPlayer.die( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.damageEntity( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityHuman.damageEntity( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityPlayer.damageEntity( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.attackEntity( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityZombie.attackEntity( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.PathfinderGoalMeleeAttack.a(SourceFile:142) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.PathfinderGoalMeleeAttack.e(SourceFile:134) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.PathfinderGoalZombieAttack.e(SourceFile:28) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.PathfinderGoalWrapped.e(SourceFile:55) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at$ForEachOp$OfRef.accept(Unknown Source) ~[?:1.8.0_261]
    at$2$1.accept(Unknown Source) ~[?:1.8.0_261]
    at java.util.Iterator.forEachRemaining(Unknown Source) ~[?:1.8.0_261]
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) ~[?:1.8.0_261]
    at Source) ~[?:1.8.0_261]
    at Source) ~[?:1.8.0_261]
    at$ForEachOp.evaluateSequential(Unknown Source) ~[?:1.8.0_261]
    at$ForEachOp$OfRef.evaluateSequential(Unknown Source) ~[?:1.8.0_261]
    at Source) ~[?:1.8.0_261]
    at Source) ~[?:1.8.0_261]
    at net.minecraft.server.v1_16_R2.PathfinderGoalSelector.doTick(SourceFile:88) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.doTick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.movementTick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.movementTick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityMonster.movementTick(SourceFile:43) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityZombie.movementTick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.tick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.tick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityZombie.tick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.WorldServer.entityJoinedWorld( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.World.a( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.WorldServer.doTick( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.b( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.DedicatedServer.b( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.a( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at Source) [?:1.8.0_261]
    Caused by: java.lang.NullPointerException
    at org.popixisko.deathmessages.listeners.PlayerDeathListener.onDeath( ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_261]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_261]
    at$1.execute( ~[spigot.jar:git-Spigot-2740d5a-890130b]
    ... 42 more
    And this is my code.
    package org.popixisko.deathmessages.listeners;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Creature;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.popixisko.deathmessages.Main;
    import org.popixisko.deathmessages.utils.Utils;
    public class PlayerDeathListener implements Listener {
    private static Main plugin;
    public PlayerDeathListener (Main plugin) {
    this.plugin = plugin;
    Bukkit.getPluginManager().registerEvents(this, plugin);
    public void onDeath(EntityDeathEvent e) {
    if (e.getEntity().getKiller() instanceof Player) {
    Player killer = e.getEntity().getKiller();
    Player p = (Player) e.getEntity();
    if (killer != p) {
    killer.sendMessage("killer_message".replace("<player>", p.getName()))));
    ((PlayerDeathEvent) e).setDeathMessage(null);
    Bukkit.broadcastMessage("death_message").replace("<player>", p.getName())));
    else {
    Player p = (Player) e.getEntity();
    Creature killer = (Creature) e.getEntity().getKiller();
    if (p instanceof Player) {
    String pName = ((Player) p).getName();
    String mobKilledPlayerMessage = plugin.getConfig().getString("mobdeath_message");
    String killerName = killer.getType().getName();
    mobKilledPlayerMessage.replaceAll("<mob>", killerName);
    mobKilledPlayerMessage.replaceAll("<player", pName);
    I would like to find someone who can help me with me this error
    Last edited: Oct 23, 2020
  2. Offline


    String killerName killer.getType().getName();
    this seems to be causing your NullPointerException. Killer may be null.
  3. Offline


    And how can I fix it?
  4. Offline


    @Xisko by the looks of it your message gets sent, and after that it throws a NPE on line 47 (which is the line i quoted before), which is interesting.
    1. please confirm that line is actually line 47, as cited by your error
    2. if so, place a null check around killer before that line and if killer == null, exit the code (return)
  5. Offline


    Player p = (Player) e.getEntity();
    The entity may not be a Player. You checked if the killer is a player, but not the entity itself.

    if (killer != p) 
    killer is e.getEntity().getKiller() and p is e.getEntity(); that will always evaluate false. The killer isn't killing themselves. Throwing yourself off a cliff to your death wouldn't fire this event, and shooting a bow in the air straight up so it comes down and kills yourself wouldn't count as you killing yourself, the arrow would be the killer entity. So I can't think of a situation where killer could ever been the killed entity.

    killer.sendMessage("killer_message".replace("<player>", p.getName()))));
    You realize your code is doing replace on "kill_message" and not the actual config contents of that path, correct? Also you aren't passing a default value, so if the config doesn't have the path it will crash. Either pass a default or null check it first.

    It's also weird that you are casting the event. Just use the actual correct event, it's cleaner and what you really should be doing anyway.

    Player p = (Player) e.getEntity();
    Creature killer = (Creature) e.getEntity().getKiller();
    if (p instanceof Player) {
    You are checking if p is instanceof Player, but you yourself created it as a Player so if it reaches that point in the code, it will always evaluate true. What you should be checking is if the entity is a Player before casting it. Also you are not checking killer either. If it's an arrow that killed the player, it will crash when you try to cast it to Creature.

    String pName = ((Player) p).getName();
    You created p as a Player, so this cast is superfluous.

    String mobKilledPlayerMessage = plugin.getConfig().getString("mobdeath_message");
    More assumption about the config. Null check or pass default.

    String killerName = killer.getType().getName();
    getName() is deprecated, just use name.

    mobKilledPlayerMessage.replaceAll("<mob>", killerName);
    String#replaceAll is for regex. Use String#replace. Don't let the name fool you; replace will replace all as well. Also it doesn't modify the String it is called against, it returns the modified value so using it the way you are, it does nothing.

Share This Page