Player Death Message

Discussion in 'Plugin Development' started by LavaBall, Mar 2, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    LavaBall

    Hi guys really need your help with a plugin where when a player is killed a message is sent in bold green to the killer saying: 'You Killed [Player Name]' but this is all i have and it docent work?
    Code:
    package me.bukkit.LoginWelcome;
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PlayerListener extends JavaPlugin{
       
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                Player.sendMessage(ChatColor.BOLD.GREEN + "You Got A Kill!");
                    }
                }
            }
     
  2. @LavaBall You are using the Player class. You need to class it to access the #sendMessage
    Use victim instead of Player.
     
  3. Offline

    LavaBall

    errrm... ok so like this?

    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                if(victim.getKiller() instanceof Player){
                    Player killer = (Player) victim.getKiller();
     
  4. @LavaBall Yes then send the message to either victim or killer.
     
  5. Offline

    LavaBall

    Ohhh ok 1 second ill post my new code:D

    Ok here what about this?

    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                if(victim.getKiller() instanceof Player){
                    Player killer = (Player) victim.getKiller();
                killer.sendMessage(ChatColor.BOLD.GREEN + "You Got A Kill!");
                }
            }
        }
    }
    
    ohh actually but where I've sent the message to player killer how scan i put the victims name so it would be "You Killed [Victims Name]!"
     
  6. @LavaBall killer.sendMessage("You killed " + victim.getName())

    Also it is Bold + Green not Bold.Green
     
  7. Offline

    1Rogue

    No, not like that. You don't need to use an instanceof check a second time, you already casted it.
     
  8. Offline

    LavaBall

    ahh ok cool thanks for your help man!:D So just to confirm this code should work?:

    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                if(victim.getKiller() instanceof Player){
                    Player killer = (Player) victim.getKiller();
                killer.sendMessage(ChatColor.BOLD + "YOU KILLED" + victim.getname());
                }
            }
        }
    }
    that victim.getname statement dosent work?
     
    Last edited by a moderator: Mar 2, 2015
  9. Offline

    LavaBall

    @bwfcwalshy ???:D

    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                if(victim.getKiller() instanceof Player){
                    Player killer = (Player) victim.getKiller();
                killer.sendMessage(ChatColor.BOLD + "YOU KILLED" + victim.#getname());
                }
            }
        }
    }
        
     
  10. Code:
    killer.sendMessage(ChatColor.BOLD + "You killed " + victim.getName());
     
  11. Offline

    LavaBall

    ahh ok lol I should've seen that the first time:D haha thanks when I get time I will add this to my code and test it to let ya know how it goes:D! Thanks
     
  12. Offline

    EpicCraft21

    To send the killer the message:
    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player killer = (Player) e.getKiller();
                killer.sendMessage(ChatColor.BOLD_GREEN + "You Got A Kill!");
                    }
                }
    To send the victim a message:
    Code:
        @EventHandler
        public void onDeath(EntityDeathEvent e){
            if(e.getEntity() instanceof Player){
                Player victim = (Player) e.getEntity();
                victim.sendMessage(ChatColor.BOLD_GREEN + "You Were Killed By " + killer);
                    }
                }
            
     
  13. Offline

    SuperOriginal

    @EpicCraft21
    • The EntityDeathEvent class doesn't have a getKiller() method
    • Your second example doesn't even have a killer variable defined
    Why aren't we just using the PlayerDeathEvent?
     
  14. Offline

    EpicCraft21

    I was adding on to his, and I don't use death events. I wasn't giving him the code, just what to do, like:
    1. I knew that, I just wanted him to do it on his own.
    2. I wasn't going to make the killer variable for him, I was just telling him to make one.
    I am sorry if I caused any confusion.
     
    LavaBall likes this.
  15. Offline

    SuperOriginal

    @EpicCraft21
    Well if you just want to tell people what to do without giving them code (which is a good idea), I would recommend just using psudeo code or an explanation. Providing actual code with incorrect syntax will just confuse the OP and people who read it.
     
  16. Offline

    1Rogue

    Remember to make sure that the killer isn't null. What if someone died from falling off a cliff?
     
  17. Offline

    LavaBall

    @1Rogue Then no-one would be sent a message?
     
  18. Offline

    1Rogue

    Nope, you'd get a null pointer exception, because someone still died, but the killer would be null.
     
  19. Offline

    LavaBall

    ahhh ok cool
     
Thread Status:
Not open for further replies.

Share This Page