Solved [HELP] PlayerDeathEvent not working.

Discussion in 'Spigot Plugin Development' started by Livaco, Apr 25, 2017.

  1. Hey! Im creating a plugin for my friend so that when u die, it insults you in the chat (He wanted it for his server idk why). So i began coding, done it, and when i done /kill, i got this:

    [21:14:25 INFO]: LivacoYT issued server command: /kill
    [21:14:25 ERROR]: Could not pass event PlayerDeathEvent to DeathInsult v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:412) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.EntityPlayer.die(EntityPlayer.java:419) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.EntityLiving.damageEntity(EntityLiving.java:933) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.EntityHuman.damageEntity(EntityHuman.java:782) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.EntityPlayer.damageEntity(EntityPlayer.java:504) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.EntityLiving.Q(EntityLiving.java:127) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.CommandKill.execute(SourceFile:35) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.craftbukkit.v1_11_R1.command.VanillaCommandWrapper.dispatchVanillaCommand(VanillaCommandWrapper.java:109) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.craftbukkit.v1_11_R1.command.VanillaCommandWrapper.execute(VanillaCommandWrapper.java:38) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1354) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1189) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-38ddb34-c19c293]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ClassCastException: org.bukkit.event.entity.PlayerDeathEvent cannot be cast to org.bukkit.entity.Player
    at plugin.deathinsult.MainListener.onPlayerDeath(MainListener.java:16) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-38ddb34-c19c293]
    ... 27 more
    [21:14:25 INFO]: LivacoYT fell out of the world
    [21:14:25 INFO]: [LivacoYT: Killed LivacoYT]

    Main.java Code:
    Code (Text):
    package plugin.deathinsult;

    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            getLogger().info("Death Insult By Livaco.");
           
            PluginManager pm= getServer().getPluginManager();
            MainListener listener = new MainListener(this);
            pm.registerEvents(listener, this);
           
            getLogger().info("Death Insult has been enabled.");
           
        }
       
        @Override
        public void onDisable() {
           
            getLogger().info("Death Insult has been disabled.");
            getLogger().info("Goodbye.");
           
        }
       
    }
     
    MainListener.java Code:
    Code (Text):
    package plugin.deathinsult;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;

    public class MainListener implements Listener {

        public MainListener(Main plugin) {
           
        }
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = ((Player) event).getPlayer();
           
            player.sendMessage("REMOVED BECUSE THE MESSAGE IS NOT ALLOWED ON THE FORUMS");
            player.sendMessage("REMOVED BECUSE THE MESSAGE IS NOT ALLOWED ON THE FORUMS ");
        }
    }
    Anyone know why? Thanks!

    Thanks
    Livaco
     
  2. Your error is coming from here:

    Code (Text):
     @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = ((Player) event).getPlayer();
           
    Try using this code:
    Code (Text):
    Player player = event.getPlayer();
    There is no need to cast Player onto event#getPlayer() since event#getPlayer() is already returning a player

    EDIT: You should read up on learning how to read Stacktraces since it literally tells you the issue right here:

    Code (Text):
    Caused by: java.lang.ClassCastException: org.bukkit.event.entity.PlayerDeathEvent cannot be cast to org.bukkit.entity.Player
      at plugin.deathinsult.MainListener.onPlayerDeath(MainListener.java:16) ~[?:?]
    This says that in the method onPlayerDeath, PlayerDeathEvent cannot be cast to a Player.

    It also tells you the class (MainListener) and what line number the code is having an issue (Line 16)
     
  3. It wont let me do it:
    The method getPlayer() is undefined for the type PlayerDeathEvent
     
  4. You need to first make sure that the entity that died was a player. All your code does is assumes that the entity was a player and moves on. Your code:
    Code (Text):
    public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = ((Player) event).getPlayer();
    You need to insert a check like this:
    Code (Text):
    public void onPlayerDeath(PlayerDeathEvent event) {
            if(e.getEntity instanceof Player) {
                    Player player = event.getPlayer();
                    // Your code
            }else return;
     
  5. My apologies I did that without using my IDE. Try using this:

    Code (Text):
    Player player = event.getEntity().getPlayer();
    EDIT: _diam's code will be more organized and conventional.
     
  6. WAS

    WAS

    We may point out that the PlayerDeathEvent is exclusive to players, and not the EntityDeathEvent
     
  7. and
    are unnecessary because of
    Code (Text):
    event.getEntity()
    is returning a player on PlayerDeathEvent.
    So you can just use:
    Code (Text):
    Player player = event.getEntity();
     
    • Agree Agree x 4
  8. getEntity() is what you are looking for.

    It returns a player even though that doesn't make sense and it should be called getPlayer
     
  9. WAS

    WAS

    Copy pasted class form EntityDeathEvent more than likely.
     
  10. Since it's open source someone could go change it
     
    • Funny Funny x 1
  11. Yeah, e.getEntity() is pretty much all you need.
    Code (Text):
    if(e.getEntity() instanceof Player) {
    }
     
  12. WAS

    WAS

    That sort of change would impact a lot of plugins though, for such a trivial name change.
     
  13. It could be marked as deprecated and left there and everyone encouraged to move to the new one that makes more sense.
     
  14. That's not how it works, open source means you can view its code, not edit.
     
    • Like Like x 1
  15. You don't need to check if it's a player; the method PlayerDeathEvent#getEntity returns a player
     
  16. The whole point of the project being open source is that you can edit it. That's why you make things open source.
     
  17. It's always good to double-check, just to prevent unnecessary bugs.

    Stop talking please.
     
    • Like Like x 1
  18. https://www.google.co.uk/search?q=what+does+open+source+mean?

    I'm pretty sure 'open source' doesn't mean anyone can edit it; if so I'm sure many servers would've been hacked by now haha. You can edit it for personal use not public
     
  19. I don't understand, is this like a joke?

    Like there's a wiki page and tons of posts explaining that they want people to make pull requests and help out and how to do it. That's literally why you make things open source. Why are you guys doing this to me? It freaks me out when tons of people just start jumping on me when I'm completely correct.
     
  20. Thanks everyone, i fixed it, heres what i ended up doing:

    Code (Text):
      @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
                    Player player = event.getEntity().getPlayer();
       //INSERT MY CODE HERE
        }
    }