Error with EntityDeathEvent

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

  1. Offline

    Xisko

    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
    Code:
    [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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:782) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityPlayer.die(EntityPlayer.java:563) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.damageEntity(EntityLiving.java:1235) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityHuman.damageEntity(EntityHuman.java:745) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityPlayer.damageEntity(EntityPlayer.java:716) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.attackEntity(EntityInsentient.java:1482) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityZombie.attackEntity(EntityZombie.java:281) ~[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 java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.ReferencePipeline$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 java.util.stream.AbstractPipeline.copyInto(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source) ~[?:1.8.0_261]
    at java.util.stream.ReferencePipeline.forEach(Unknown 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(EntityInsentient.java:729) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.movementTick(EntityLiving.java:2669) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.movementTick(EntityInsentient.java:530) ~[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(EntityZombie.java:201) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityLiving.tick(EntityLiving.java:2398) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityInsentient.tick(EntityInsentient.java:293) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.EntityZombie.tick(EntityZombie.java:171) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.WorldServer.entityJoinedWorld(WorldServer.java:659) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.World.a(World.java:718) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.WorldServer.doTick(WorldServer.java:394) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.b(MinecraftServer.java:1104) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.DedicatedServer.b(DedicatedServer.java:349) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.a(MinecraftServer.java:1007) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:846) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.NullPointerException
    at org.popixisko.deathmessages.listeners.PlayerDeathListener.onDeath(PlayerDeathListener.java:47) ~[?:?]
    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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot.jar:git-Spigot-2740d5a-890130b]
    ... 42 more
    And this is my code.
    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);
    
    }
    
    @EventHandler
    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(Utils.chat(plugin.getConfig().getString("killer_message".replace("<player>", p.getName()))));
    p.sendMessage(Utils.chat(plugin.getConfig().getString("playerdeath_message").replace("<killer>",killer.getName())));
    }
    
    ((PlayerDeathEvent) e).setDeathMessage(null);
    Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("death_message").replace("<player>", p.getName())));
    return;
    
    }
    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);
    
    plugin.getServer().broadcastMessage(mobKilledPlayerMessage);
    }
    }
    
    }
    I would like to find someone who can help me with me this error
     
    Last edited: Oct 23, 2020
  2. Offline

    Kars

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

    Xisko

    And how can I fix it?
     
  4. Offline

    Kars

    @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

    Strahan

    Also
    Code:
    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.

    Code:
    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.

    Code:
    killer.sendMessage(Utils.chat(plugin.getConfig().getString("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.

    Code:
    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.

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

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

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

    Code:
    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