Solved Get the cause of death

Discussion in 'Plugin Development' started by Xp10d3, Apr 19, 2021.

  1. Offline

    Xp10d3

    Dumb question; how would I get the cause of a player's death via PlayerDeathEvent? I know that you can only get the death cause using EntityDeathEvent, however I won't be able to set the death message from that. If I were to use EntityDeathEvent, I don't necessarily know how I'd cancel the death message from EntityDeathEvent and then broadcast a message. Anyways here's my code:
    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            Player killer = player.getKiller();
            if (causeOfDeath.equals(DamageCause.VOID)) {
                event.setDeathMessage(player.getName() + " fought to the edge with " + killer.getName() + ".");
            }
        }
    
    Wait never mind I'm dumb.
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR)
    3. public void onPlayerDeath(final EntityDeathEvent e) {
    4. Player ent = (Player) e.getEntity();
    5. EntityDamageEvent ede = ent.getLastDamageCause();
    6. DamageCause dc = ede.getCause();
    7.  
    8. if (ent instanceof Player && dc == DamageCause.VOID) {
    9. Player p = (Player)ent;
    10. Player killer = p.getKiller();
    11. if (PlayerListeners.blue.contains(p.getName())) {
    12. p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " tried to block extend running from " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    13. killer.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " tried to block extend running from " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    14. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    15. } else if (PlayerListeners.red.contains(p.getName())) {
    16. p.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " tried to block extend running from " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    17. killer.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " tried to block extend running from " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    18. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    19. }
    20. } else if (ent instanceof Player && dc == DamageCause.ENTITY_ATTACK) {
    21. Player p = (Player)ent;
    22. Player killer = p.getKiller();
    23. if (PlayerListeners.blue.contains(p.getName())) {
    24. p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " was no match for " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    25. killer.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " was no match for " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    26. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    27. } else if (PlayerListeners.red.contains(p.getName())) {
    28. p.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " was no match for " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    29. killer.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " was no match for " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    30. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    31. }
    32. } else if (ent instanceof Player && dc == DamageCause.PROJECTILE) {
    33. Player p = (Player)ent;
    34. Player killer = p.getKiller();
    35. if (PlayerListeners.blue.contains(p.getName())) {
    36. p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " fell to " + ChatColor.RED + killer + ChatColor.GRAY + "'s arrow.");
    37. killer.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " fell to " + ChatColor.RED + killer + ChatColor.GRAY + "'s arrow.");
    38. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    39. } else if (PlayerListeners.red.contains(p.getName())) {
    40. p.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " fell to " + ChatColor.BLUE + killer + ChatColor.GRAY + "'s arrow.");
    41. killer.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + " fell to " + ChatColor.BLUE + killer + ChatColor.GRAY + "'s arrow.");
    42. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    43. }
    44. } else if (ent instanceof Player && dc == DamageCause.SUFFOCATION) {
    45. Player p = (Player)ent;
    46. Player killer = p.getKiller();
    47. if (PlayerListeners.blue.contains(p.getName())) {
    48. p.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + "'s ping caused him to get stuck in a block running from " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    49. killer.sendMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + "'s ping caused him to get stuck in a block running from " + ChatColor.RED + killer + ChatColor.GRAY + ".");
    50. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    51. } else if (PlayerListeners.red.contains(p.getName())) {
    52. p.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + "'s ping caused him to get stuck in a block running from " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    53. killer.sendMessage(ChatColor.RED + p.getName() + ChatColor.GRAY + "'s ping caused him to get stuck in a block running from " + ChatColor.BLUE + killer + ChatColor.GRAY + ".");
    54. killer.playSound(killer.getLocation(), Sound.ORB_PICKUP, 1, 11);
    55. }
    56. } else {
    57. Player p = (Player)ent;
    58. Bukkit.broadcastMessage(ChatColor.BLUE + p.getName() + ChatColor.GRAY + " died.");
    59. }
    60. }
    61.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 19, 2021

Share This Page