0
\$\begingroup\$

Could any one tell me why I get the exception (and how to fix it):

Bukkit Plugin Couldn't pass Playerdeathevent to Plugin [org.bukkit.event.EventException]

with this code?

@EventHandler
public void ondeath(PlayerDeathEvent event) {
    int XP = event.getDroppedExp();
    event.setDeathMessage(ChatColor.YELLOW + event.getEntity().getPlayer().getDisplayName() + ChatColor.RED + " was killed by " + ChatColor.YELLOW + event.getEntity().getPlayer().getKiller().getName() + ChatColor.RED + " with a total of " + ChatColor.YELLOW + XP + ChatColor.RED + " XP!");
    event.setNewExp(0);
    event.setNewLevel(0);
    event.getEntity().setGameMode(GameMode.SPECTATOR);
    event.getEntity().getPlayer().kickPlayer(ChatColor.BOLD + "" + ChatColor.RED + "You Lost" + ChatColor.RESET + " (Reconnect to spectate, but don't help other Players!)");

    for (Player p : Bukkit.getOnlinePlayers()) {
        p.playSound(p.getLocation(), Sound.ENDERDRAGON_DEATH, 1, 0);
    }
}

EDIT: I know now what the cause was but I don't know what exactly is wrong.

The cause is the following line.

event.setDeathMessage(ChatColor.YELLOW + event.getEntity().getPlayer().getDisplayName() + ChatColor.RED + " was killed by " + ChatColor.YELLOW + event.getEntity().getKiller().getName() + ChatColor.RED + " using " + ChatColor.YELLOW + event.getEntity().getKiller().getPlayer().getItemInHand().getItemMeta().getDisplayName() + ChatColor.RED + " with a total of " + ChatColor.YELLOW + XP + ChatColor.RED + " XP!");

Here is my Error:

16:49:12 [ERROR] Could not pass event PlayerDeathEvent to UHC v0.1
16:49:12 org.bukkit.event.EventException
16:49:12 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:396) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityPlayer.die(EntityPlayer.java:417) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:812) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityLiving.e(EntityLiving.java:939) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityHuman.e(EntityHuman.java:1440) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.Block.fallOn(Block.java:640) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.Entity.a(Entity.java:811) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityLiving.a(EntityLiving.java:160) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.EntityPlayer.a(EntityPlayer.java:621) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:456) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]
16:49:12 at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
16:49:12 at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
16:49:12 Caused by: java.lang.NullPointerException
16:49:12 at com.gmail.apply55gx.UHC.Listener.ondeath(Listener.java:40) ~[?:?]
16:49:12 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
16:49:12 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_65]
16:49:12 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_65]
16:49:12 at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_65]
16:49:12 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[craftbukkit.jar:git-Spigot-db6de12-18fbb24]
16:49:12 ... 26 more
\$\endgroup\$
2
  • \$\begingroup\$ What is the exact error you are getting? \$\endgroup\$
    – user35344
    Jul 9, 2016 at 15:57
  • \$\begingroup\$ We need the part after (including) the line wich starts with "Caused by: ..." \$\endgroup\$
    – Bálint
    Jul 9, 2016 at 18:38

1 Answer 1

0
\$\begingroup\$

Without the exact error I can assume what the problem is.

Both Player#getItemInHand(), ItemStack#getItemMeta() and ItemMeta#getDisplayName() can return null and thus can throw a nullpointer exception.

You need to check if any of these is null before using them.

\$\endgroup\$
10
  • \$\begingroup\$ No that isn't the cause. I already tried to removet his part of the Code. \$\endgroup\$
    – Apply55gx
    Jul 9, 2016 at 19:16
  • \$\begingroup\$ @Apply55gx Wich one is the 40th line of the listener class? \$\endgroup\$
    – Bálint
    Jul 9, 2016 at 19:31
  • \$\begingroup\$ Well the 40th line was an empty line. But it doesn't matters I tested it And Im 100% sure that it is the message.If you are wondering how I tested it: I put after every line of code a System.out.print with a number. And the hole thing also worked if I removed the message line. \$\endgroup\$
    – Apply55gx
    Jul 10, 2016 at 11:25
  • \$\begingroup\$ @Apply55gx 1.: You don't need to do getEntity().getPlayer(), getEntity() already returns a player. 2.: Try inserting a break new line after every + on that line, run the plugin again, and if it errors, post the whole error here and don't change anything in the code \$\endgroup\$
    – Bálint
    Jul 10, 2016 at 11:40
  • \$\begingroup\$ Also, check if getKiller returns null, not every player is died because of another player \$\endgroup\$
    – Bálint
    Jul 10, 2016 at 11:41

Not the answer you're looking for? Browse other questions tagged .