Command on death

Discussion in 'Spigot Plugin Development' started by Hangar555, Jun 3, 2016.

  1. Hello, I'm having an issue: I want when a player gets killed/died do a command.

    Code (Java):
    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
        if (event instanceof PlayerDeathEvent) {
            Player player = (Player) event.getEntity();
            player.performCommand("Command");
    But it doesn't do the command (F.E. "help), what am I doing wrong?

    Thanks for the help :D
     
  2. Choco

    Moderator

    Well for one, why are you not just listening directly for a PlayerDeathEvent? It'll save you the instanceof check. Lol
     
  3. like this?
    Code (Java):
    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
        if (event instanceof PlayerDeathEvent) {
            player.performCommand("Command");
    Sorry, I'm noob :C
     
  4. nope, PlayerDeathEvent instead EntityDeathEvent
     
    • Agree Agree x 1
  5. You are trying to link an EntityDeathEvent to a PlayerDeathEvent. That's the same as trying to directly cast a string to an integer.
    All you have to do is create a PlayerDeathEvent. From there you can call the command.
     
  6. very funny, when I'm still learning Java.
    Thanks :)

    Now the code looks like this:
    Code (Java):
    package eu.hangar.seconds;
    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;
    public class Death implements Listener {

           
       
    @EventHandler
    public void onEntityDeath(PlayerDeathEvent event) {
        if (event instanceof PlayerDeathEvent) {
           
            Player.performCommand("Help");
     
  7. Dead players can't run commands as far as I know. I'd suggest running the command as console or waiting for the PlayerRespawnEvent before performing the command.
     
  8. No, it isn't the same. He's checking if the EntityDeathEvent object implements PlayerDeathEvent, which it can't.

    Here you're using the Player class to perform the command, not the actual player object. You can't do this, as you have no reference to the player.

    Also, you shouldn't be checking if the PlayerDeathEvent is an instance of PlayerDeathEvent, you already know that it is.

    You can get the player by calling PlayerDeathEvent#getEntity, and then you can perform the command using the Player object that it returns.

    Code (Text):
    @EventHandler
    public void onEntityDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        player.performCommand("randomCommand");
    }
     
  9. Use pls the PlayerRespawnEvent. And if you want the players respawn instantly do this in PlayerDeathEvent e.getPlayer().spigot().respawn();
     
  10. ?
    You should really read the thread first.
     
  11. You should know, which method can be called bugs or not. And the PlayerRespawnEvent is the better way for perfoming commands.
     
  12. He doesn't want them to instant respawn, he just wants to force a command. As long as that command isn't interacting with the player, there shouldn't be any issues.
     
  13. thanks for the help. This is really helpful for my Java Learning.