Death messages not working?

Discussion in 'Plugin Development' started by SkyLarkPvP, Aug 9, 2014.

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

    SkyLarkPvP

    Hey, so i have:
    @EventHandler
    public void death(PlayerDeathEvent e) {
    Player p = e.getEntity().getPlayer();
    Player k = e.getEntity().getKiller();
    e.setDeathMessage(null);
    k.sendMessage(ChatColor.AQUA + "You killed " + ChatColor.YELLOW + p.getName());
    p.sendMessage(ChatColor.AQUA + "You were killed by " + ChatColor.YELLOW + k.getName());

    in my plugin but the messages aren't working, any help?
     
  2. Offline

    tommyhoogstra

    Registered your events?
     
  3. Offline

    Necrodoom

    SkyLarkPvP casting player to player.. Not checking if killer is null before casting.. Make sure you also fix that.
     
    HeavyMine13 likes this.
  4. Offline

    SkyLarkPvP

    Necrodoom and tommyhoogstra sorry, but how would i do that? I'm kind of new in coding.

    would this help?
    @EventHandler
    public void death(PlayerDeathEvent e) {
    Player p = e.getEntity().getPlayer();
    Player k = e.getEntity().getKiller();
    if(k != null) e.setDeathMessage(null);
    e.setDeathMessage(null);
    k.sendMessage(ChatColor.AQUA + "You killed " + ChatColor.YELLOW + p.getName());
    p.sendMessage(ChatColor.AQUA + "You were killed by " + ChatColor.YELLOW + k.getName());

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    tommyhoogstra

    No, you dont need e.getEntity().getPlayer();
    You can do Player p = e.getPlayer();
    and Player k = p.getKiller();

    To register your events, you need this in your onEnable
    Code:
     Bukkit.getPluginManager().registerEvents(this, this);
    
     
  6. I'm sorry... I just couldn't let this go on...
    Code:java
    1. public void death(PlayerDeathEvent e) {
    2. if(e.getKiller() instance of Player {
    3. Player p = e.getPlayer();
    4. Player k = e.getKiller();
    5. k.sendMessage(ChatColor.AQUA + "You killed " + ChatColor.YELLOW + p.getName());
    6. p.sendMessage(ChatColor.AQUA + "You were killed by " + ChatColor.YELLOW + k.getName());
    7. }
    8. }
    9.  


    Oh, and you can remove the death message in there if you'd like, forgot to add that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    xTigerRebornx

    xYourFreindx You do realize that the killer can only be a Player, making the instanceof (not instance of) check useless. A null check would be more appropriate.
     
  8. xTigerRebornx Soz. And yes I know it's instanceof, my mac autocorrected that. Soz OP.
    But no I didn't know that the killer could only be a player. I assumed it could be any LivingEntity considering it is called onPlayerDeath event, and a player can die in more ways that just by a player. Obviously, if what you say is true, then the getKiller() method checks to see if there even was a killer, all by itself. I did not want to assume it did that, so as to avoid a possible NPE, I checked first. Just habit.

    Could you post the error that ^ gives you?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    SkyLarkPvP

    xYourFreindx
    I deleted the player check, now i'm getting an error "add cast to e":
    Player p = e.getPlayer();
    and i don't know what you mean by that
     
  10. The stack trace.

    Post the error message you get in console when you use the code I gave you.
     
  11. Offline

    SkyLarkPvP

    xYourFreindx
    Im not sure what you mean by that, but:
    insert ") Statement" To complete Block statements
    The method GetPlayer() is undefined for the type PlayerDeathEvent
    The method GetKiller() is undefined for the type PlayerDeathEvent
     
  12. Offline

    kps1796

  13. Offline

    SkyLarkPvP

    still getting the same error message
     
  14. Offline

    kps1796

    SkyLarkPvP can you post the whole startup log into pastebin.com ?
     
  15. A quick look at the java docs reveals that getPlayer() and getKiller() are not even valid methods for the PlayerDeath event...... :p

    To make things easier, would you know what to do if I asked you to use the EntityDamageByEntity event? Or would I have to write it out for you in order to get you to do that?
     
  16. Offline

    SkyLarkPvP

    xYourFreindx, to be honest, i would not know what to do :p
    sorry i'm new to coding.
     
  17. Offline

    Necrodoom

  18. Necrodoom Could you explain to me how e.getKiller and e.getPlayer work? I looked at the javadocs, and they aren't there.
     
  19. Offline

    Necrodoom

    xYourFreindx that's because they don't.
    The method is getEntity to get the player, and the getKiller is a livingentity method.
     
  20. SkyLarkPvP

    I'm just as confuse about the getKiller thing (if not more) than you are....
    So I threw this together....
    Code:java
    1.  
    2. ArrayList<UUID> death = new ArrayList<UUID>();
    3. @EventHandler
    4. public void death(PlayerDeathEvent e) {
    5. if(death.contains(e.getEntity().getUniqueId())){
    6. e.setDeathMessage(null);
    7. death.remove(e.getEntity().getUniqueId());
    8. }
    9. }
    10. @EventHandler
    11. public void death(EntityDamageByEntityEvent e){
    12. if(e.getEntity() instanceof Player && e.getDamage() >= ((Damageable) e.getEntity()).getHealth() && e.getDamager() instanceof Player){
    13. Player player = (Player) e.getEntity();
    14. Player killer = (Player) e.getDamager();
    15. death.add(player.getUniqueId());
    16. killer.sendMessage(ChatColor.AQUA + "You killed " + ChatColor.YELLOW + player.getName());
    17. player.sendMessage(ChatColor.AQUA + "You were killed by " + ChatColor.YELLOW + killer.getName());
    18. }
    19. }

    Sorry for any confusion I caused, I haven't tested this ^ so if it works, be sure to yell back to me. The only part I can see not working is the e.getDamage() >= ((Damageable) e.getEntity()).getHealth() part... But let's hope for the best.

    Necrodoom So.... You'd need to cast living entity to the player that was killed in order to use that meathod?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. Offline

    Necrodoom

    xYourFreindx please stop spoonfeeding useless code.
    Post #5 was good enough with tommyhoogstra suggestions.
    Also, player is already a livingentity...
     
  22. Necrodoom Useless code? I worked a good 3 minutes on that. Have you even tested it? What about it is useless? I must admit I'm insulted. Have you even looked at it?

    e.getEntity().getKiller() does not work. Not in my eclipse. And getEntity is supposed to return a player. If I'm not mistaken. So where have I gone wrong?
     
  23. Offline

    DevilMental

    What I use to get the killer rom the death event :
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Player p = (Player) e.getEntity();
    4. EntityDamageEvent deathCause = p.getLastDamageCause();
    5. if (deathCause.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
    6. Entity entity = ((EntityDamageByEntityEvent) deathCause)
    7. .getDamager();
    8. if (entity.getType() == EntityType.PLAYER) {
    9. // Do what you gotta do
    10. }
    11. }
    12. }

    Hope that helps!
     
  24. Offline

    tommyhoogstra

     
  25. Offline

    Necrodoom

  26. Necrodoom Yes yes, I've seen that already. It is not helpful to me. My problem resides in the fact that I have taken no java courses and use bukkit as a crutch to get around java related problems. Since I'm self taught, I'm always looking for ways to better understand a situation so that the crutch isn't needed, and I can take that knowledge with me into my coding and to other people.
    If you don't want to give me a detailed answer, then I don't blame you... But I will never understand with what limited information you're giving me.
     
  27. Offline

    _LB

    xYourFreindx The point being made was that Entity does not have #getKiller() but LivingEntity does - in response to "e.getEntity().getKiller() does not work. Not in my eclipse."
     
  28. _LB
    I this event... e.getEntity() is supposed to return a player, which, is already a living entity. My confusion is why .getKiller does not work for THAT living entity.

    Would I have to first cast e.getEntity() to living entity and then get the killer is the question I asked a few posts ago.

    EDIT: Oh, and Why? Why is the most important question.
     
  29. Offline

    Necrodoom

  30. Offline

    SkyLarkPvP

    There appears to be a big fight while i was gone...
    I managed to get it working with
    my onEnable: Bukkit.getPluginManager().registerEvents(this, this);

    and this
    @EventHandler
    public void kill(PlayerDeathEvent event) {
    Player p = event.getEntity();
    Player k = p.getKiller();
    event.setDeathMessage(null);
    k.sendMessage(ChatColor.AQUA + "You killed " + ChatColor.YELLOW + p.getName() + "!");
    p.sendMessage(ChatColor.AQUA + "You were killed by " + ChatColor.YELLOW + k.getName() + "!");

    The problem is, when ever someone dies from something else, e.g: a spider, the server console has a big error report, any suggestions on how to fix that?
     
Thread Status:
Not open for further replies.

Share This Page