[SOLVED] Error again on Death

Discussion in 'Spigot Plugin Development' started by haggishunter2001, Jul 11, 2015.

  1. Heyy,

    a few days before i had an error when a player died.

    Now its so, that the error comes again!

    Error:
    Code (Text):
    [13:04:37] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to FunMessages v2.2
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:382) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityPlayer.die(EntityPlayer.java:417) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:811) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityLiving.e(EntityLiving.java:938) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityHuman.e(EntityHuman.java:1440) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.Block.a(Block.java:640) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.Entity.a(Entity.java:810) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityLiving.a(EntityLiving.java:160) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.EntityPlayer.a(EntityPlayer.java:621) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:455) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
        at de.GameCubeMC.www.Events.onPlayerTod(Events.java:49) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-f928e7a-e91aed8]
        ... 26 more
    Code:
    Code (Text):
    package de.GameCubeMC.www;



    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.enchantment.EnchantItemEvent;
    import org.bukkit.event.entity.EntityTameEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerAchievementAwardedEvent;
    import org.bukkit.event.player.PlayerBedEnterEvent;
    import org.bukkit.event.player.PlayerBedLeaveEvent;
    import org.bukkit.event.player.PlayerBucketEmptyEvent;
    import org.bukkit.event.player.PlayerBucketFillEvent;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerEggThrowEvent;
    import org.bukkit.event.player.PlayerFishEvent;
    import org.bukkit.event.player.PlayerGameModeChangeEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerPortalEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Events implements Listener {
           
      public static Main plugin;
      public Events(Main instance) {
          plugin = instance;
         
          }
      @EventHandler
      public void onPlayerTod(PlayerDeathEvent pdw) {
        String Player_Death_Global = plugin.getConfig().getString("Config.Player_Death_Global");
        String Player_Death_Victim = plugin.getConfig().getString("Config.Player_Death_Victim");
        String Player_Death_Killer = plugin.getConfig().getString("Config.Player_Death_Killer");
        String Player_Death_Normal = plugin.getConfig().getString("Config.Player_Death_Normal");
        Player p = pdw.getEntity();
        Player_Death_Global = Player_Death_Global.replace("[PLAYER]", p.getName());
        Player_Death_Global = Player_Death_Global.replace("[KILLER]", p.getKiller().getName());
        Player_Death_Victim = Player_Death_Victim.replace("[PLAYER]", p.getName());
        Player_Death_Victim = Player_Death_Victim.replace("[KILLER]", p.getKiller().getName());
        Player_Death_Killer = Player_Death_Killer.replace("[PLAYER]", p.getName());
        Player_Death_Killer = Player_Death_Killer.replace("[KILLER]", p.getKiller().getName());
        Player_Death_Normal = Player_Death_Normal.replace("[PLAYER]", p.getName());
        Player_Death_Normal = Player_Death_Normal.replace("[NORMAL]", pdw.getDeathMessage());
        pdw.setDeathMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Normal));
        if(pdw.getEntity().getKiller() instanceof Player) {
        pdw.setDeathMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Global));
        p.sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Victim) + " §7|§2 " + p.getKiller().getName() + " health §8>> §5" + p.getKiller().getHealth());
        p.getKiller().sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Killer));
        }
      }
    }
     
  2. This is just a guess, but I suppose Player#getKiller would return null if the player for example died from fall damage. Try adding a null check before the lines that use Player#getKiller and you should be good to go. If that doesn't help, post your entire Events class on pastebin so that we can see which line is causing the exception to be thrown.
     
    • Agree Agree x 1
  3. hello fellow members I hope you could help him in a better or simple way . he is german not a english person
     
  4. I'll suggest you to add
    Code (Text):
    if(!(e.getEntity().getKiller() instanceof Player)) return;
     
  5. I just tried it, but the error comes again!


    I hav tried this:
    Code (Text):
        if (pdw.getEntity().getKiller() == null) {
        pdw.setDeathMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Normal));
        }
     
  6. Inkzzz

    Resource Staff

    Change:
    Code (Text):
      p.getKiller().sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Killer));
     
    To:
    Code (Text):
      e.getEntity().getKiller().sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Killer));
     
  7. Doesn't work :(
     
  8. Inkzzz

    Resource Staff

    Could you provide us with the updated print stack, and code?
     
  9. Code (Text):

      @EventHandler
       public void onPlayerTod(PlayerDeathEvent pdw) {
         Player player = pdw.getEntity();
         Player killer = player.getKiller();

         if (killer == null) {
           return; // player was not killed by another player.
         }

         String Player_Death_Global = plugin.getConfig().getString("Config.Player_Death_Global");
         String Player_Death_Victim = plugin.getConfig().getString("Config.Player_Death_Victim");
         String Player_Death_Killer = plugin.getConfig().getString("Config.Player_Death_Killer");
         String Player_Death_Normal = plugin.getConfig().getString("Config.Player_Death_Normal");

         Player_Death_Global = Player_Death_Global.replace("[PLAYER]", player.getName());
         Player_Death_Global = Player_Death_Global.replace("[KILLER]", killer.getName());
         Player_Death_Victim = Player_Death_Victim.replace("[PLAYER]", player.getName());
         Player_Death_Victim = Player_Death_Victim.replace("[KILLER]", killer.getName());
         Player_Death_Killer = Player_Death_Killer.replace("[PLAYER]", player.getName());
         Player_Death_Killer = Player_Death_Killer.replace("[KILLER]", killer.getName());
         Player_Death_Normal = Player_Death_Normal.replace("[PLAYER]", player.getName());
         Player_Death_Normal = Player_Death_Normal.replace("[NORMAL]", pdw.getDeathMessage());

         pdw.setDeathMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Normal));

         if (killer instanceof Player) { // this is ALWAYS true since player.getKiller() will always return a Player or null...
           pdw.setDeathMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Global));
           player.sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Victim) + " §7|§2 " + killer.getName() + " health §8>> §5" + killer.getHealth());
           killer.sendMessage(ChatColor.translateAlternateColorCodes('&', Player_Death_Killer));
         }
       }
     
    Few things to take note on.

    1) Always null check objects before you try using methods from them. You are getting an object for the killer, and then using that object's .getName() method. If the object is null, then trying to use that method will throw an NPE.

    2) Player.getKiller() will ALWAYS return a Player object, or null. You cannot get the mob that killed a player like that, etc. So, to cover those cases you will have to change up your design here.

    3) Dont be afraid to store objects in variables (like the killer). Its bad habit to always call a getSomething() method to get the same object over and over again. If you are going to use an object more than once, store it in a variable and use it that way.
     
    • Winner Winner x 1
  10. IT WORKS!

    <3
     
  11. I know it works. That doesnt matter. What matters is if you learned from this. ;)
     
    • Agree Agree x 1
  12. Thats a good start man ! hope you learn more