Solved PlayerDeathEvent DamageCause

Discussion in 'Plugin Development' started by duker02, Jun 22, 2013.

  1. Offline

    duker02

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hello, my code is here:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Entity ent = e.getEntity();
    4. String weapon = e.getEntity().getKiller().getItemInHand().getType().name().toLowerCase().replaceAll("_", " ").replaceAll("air", "their fist");
    5. EntityDamageEvent ede = ent.getLastDamageCause();
    6. DamageCause dc = ede.getCause();
    7. if (ent instanceof Player && dc == DamageCause.FALL) {
    8. Player p = (Player)ent;
    9. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + "'s guts are all over the ground now.");
    10. e.getDrops().clear();
    11. p.getInventory().clear();
    12. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    13. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    14. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    15. p.getInventory().setBoots(new ItemStack(Material.AIR));
    16. if(usedkit.contains(p.getName()));
    17. usedkit.remove(p.getName());
    18. }else{
    19. if (ent instanceof Player && dc == DamageCause.CONTACT) {
    20. Player p = (Player)ent;
    21. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " hugged a cactus.");
    22. e.getDrops().clear();
    23. p.getInventory().clear();
    24. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    25. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    26. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    27. p.getInventory().setBoots(new ItemStack(Material.AIR));
    28. if(usedkit.contains(p.getName()));
    29. usedkit.remove(p.getName());
    30. }else{
    31. if (ent instanceof Player && dc == DamageCause.FIRE_TICK) {
    32. Player p = (Player)ent;
    33. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " got a sunburn.");
    34. e.getDrops().clear();
    35. p.getInventory().clear();
    36. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    37. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    38. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    39. p.getInventory().setBoots(new ItemStack(Material.AIR));
    40. if(usedkit.contains(p.getName()));
    41. usedkit.remove(p.getName());
    42. }else{
    43. if (ent instanceof Player && dc == DamageCause.FIRE) {
    44. Player p = (Player)ent;
    45. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " tried to see if they were fireproof.");
    46. e.getDrops().clear();
    47. p.getInventory().clear();
    48. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    49. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    50. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    51. p.getInventory().setBoots(new ItemStack(Material.AIR));
    52. if(usedkit.contains(p.getName()));
    53. usedkit.remove(p.getName());
    54. }else{
    55. if (ent instanceof Player && dc == DamageCause.LAVA) {
    56. Player p = (Player)ent;
    57. e.setDeathMessage(ChatColor.GOLD + "Unfortunately, " + ChatColor.GRAY + p.getName() + ChatColor.GOLD + " can't swim in lava.");
    58. e.getDrops().clear();
    59. p.getInventory().clear();
    60. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    61. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    62. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    63. p.getInventory().setBoots(new ItemStack(Material.AIR));
    64. if(usedkit.contains(p.getName()));
    65. usedkit.remove(p.getName());
    66. }else{
    67. if (ent instanceof Player && dc == DamageCause.STARVATION) {
    68. Player p = (Player)ent;
    69. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " couldn't find some food to eat.");
    70. e.getDrops().clear();
    71. p.getInventory().clear();
    72. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    73. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    74. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    75. p.getInventory().setBoots(new ItemStack(Material.AIR));
    76. if(usedkit.contains(p.getName()));
    77. usedkit.remove(p.getName());
    78. }else{
    79. if (ent instanceof Player && dc == DamageCause.SUICIDE) {
    80. Player p = (Player)ent;
    81. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " just wrote a suicide note and said goodbye.");
    82. e.getDrops().clear();
    83. p.getInventory().clear();
    84. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    85. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    86. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    87. p.getInventory().setBoots(new ItemStack(Material.AIR));
    88. if(usedkit.contains(p.getName()));
    89. usedkit.remove(p.getName());
    90. }else{
    91. if (ent instanceof Player && dc == DamageCause.ENTITY_ATTACK) {
    92. Player p = (Player)ent;
    93. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " was killed by " + ChatColor.GRAY + p.getKiller().getName() + ChatColor.GOLD + " using " + weapon + ".");
    94. e.getDrops().clear();
    95. p.getInventory().clear();
    96. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    97. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    98. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    99. p.getInventory().setBoots(new ItemStack(Material.AIR));
    100. if(usedkit.contains(p.getName()));
    101. usedkit.remove(p.getName());
    102. }
    103. }
    104. }
    105. }
    106. }
    107. }
    108. }
    109. }
    110. }

    The problem is that when something other than ENTITY_ATTACK happens, it doesn't do anything. The ENTITY_ATTACK works fine, but nothing else does. I even moved ENTITY_ATTACK from the top to the bottom to see if it would work, but nope, although ENTITY_ATTACK worked either way.

    If you're wondering, this is a KitPVP plugin that I am coding for my server and usedkit is an ArrayList that they are added to when they get a kit, stopping them from getting another kit until death. This is a huge problem because if they die any other way than PVP, they can't get another kit until the plugin is reloaded (full server reload, which sometimes crashes the server).

    If the fix is a very obvious fix, I apologize because I only have about 6 months of Java knowledge. This is one of the errors I've run into SO many times and it's just killing me. I finally decided to post it here.

    Thanks,
    - Alex // duker02.

    This post has been edited 4 times. It was last edited by duker02 Jun 22, 2013.
  2. Offline

    Shzylo

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Change:
    Player ent = e.getEntity();

    To:
    Entity ent = e.getEntity();

    Then put all your if statements that are getting the damagecause, put them all in a if statment that states 'ent instanceof Player'
  3. Offline

    duker02

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Had that the first time I tried this, didn't work.

    EDIT: Retried that and got this error:
    21.06 19:21:47 [Multicraft] Skipped 8 lines due to rate limit (30/s)
    21.06 19:21:45 [Server] INFO Caused by: java.lang.NullPointerException
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:115)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.Packet10Flying.handle(SourceFile:136)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:354)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityPlayer.g(EntityPlayer.java:224)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityHuman.l_(EntityHuman.java:158)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityLiving.l_(EntityLiving.java:500)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.Entity.l_(Entity.java:247)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityLiving.x(EntityLiving.java:318)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.Entity.x(Entity.java:335)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.Entity.z(Entity.java:371)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityPlayer.damageEntity(EntityPlayer.java:358)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityHuman.damageEntity(EntityHuman.java:684)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityLiving.damageEntity(EntityLiving.java:750)
    21.06 19:21:45 [Server] INFO at net.minecraft.server.v1_5_R3.EntityPlayer.die(EntityPlayer.java:293)
    21.06 19:21:45 [Server] INFO at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:344)
    21.06 19:21:45 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    21.06 19:21:45 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    21.06 19:21:45 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    21.06 19:21:45 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    21.06 19:21:45 [Server] INFO org.bukkit.event.EventException
    21.06 19:21:45 [Server] SEVERE Could not pass event PlayerDeathEvent to KitPVP v1.0

    Current code:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Entity ent = e.getEntity();
    4. String weapon = e.getEntity().getKiller().getItemInHand().getType().name().toLowerCase().replaceAll("_", " ").replaceAll("air", "their fist");
    5. EntityDamageEvent ede = ent.getLastDamageCause();
    6. DamageCause dc = ede.getCause();
    7. if (ent instanceof Player && dc == DamageCause.FALL) {
    8. Player p = (Player)ent;
    9. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + "'s guts are all over the ground now.");
    10. e.getDrops().clear();
    11. p.getInventory().clear();
    12. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    13. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    14. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    15. p.getInventory().setBoots(new ItemStack(Material.AIR));
    16. if(usedkit.contains(p.getName()));
    17. usedkit.remove(p.getName());
    18. }else{
    19. if (ent instanceof Player && dc == DamageCause.CONTACT) {
    20. Player p = (Player)ent;
    21. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " hugged a cactus.");
    22. e.getDrops().clear();
    23. p.getInventory().clear();
    24. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    25. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    26. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    27. p.getInventory().setBoots(new ItemStack(Material.AIR));
    28. if(usedkit.contains(p.getName()));
    29. usedkit.remove(p.getName());
    30. }else{
    31. if (ent instanceof Player && dc == DamageCause.FIRE_TICK) {
    32. Player p = (Player)ent;
    33. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " got a sunburn.");
    34. e.getDrops().clear();
    35. p.getInventory().clear();
    36. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    37. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    38. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    39. p.getInventory().setBoots(new ItemStack(Material.AIR));
    40. if(usedkit.contains(p.getName()));
    41. usedkit.remove(p.getName());
    42. }else{
    43. if (ent instanceof Player && dc == DamageCause.FIRE) {
    44. Player p = (Player)ent;
    45. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " tried to see if they were fireproof.");
    46. e.getDrops().clear();
    47. p.getInventory().clear();
    48. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    49. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    50. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    51. p.getInventory().setBoots(new ItemStack(Material.AIR));
    52. if(usedkit.contains(p.getName()));
    53. usedkit.remove(p.getName());
    54. }else{
    55. if (ent instanceof Player && dc == DamageCause.LAVA) {
    56. Player p = (Player)ent;
    57. e.setDeathMessage(ChatColor.GOLD + "Unfortunately, " + ChatColor.GRAY + p.getName() + ChatColor.GOLD + " can't swim in lava.");
    58. e.getDrops().clear();
    59. p.getInventory().clear();
    60. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    61. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    62. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    63. p.getInventory().setBoots(new ItemStack(Material.AIR));
    64. if(usedkit.contains(p.getName()));
    65. usedkit.remove(p.getName());
    66. }else{
    67. if (ent instanceof Player && dc == DamageCause.STARVATION) {
    68. Player p = (Player)ent;
    69. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " couldn't find some food to eat.");
    70. e.getDrops().clear();
    71. p.getInventory().clear();
    72. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    73. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    74. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    75. p.getInventory().setBoots(new ItemStack(Material.AIR));
    76. if(usedkit.contains(p.getName()));
    77. usedkit.remove(p.getName());
    78. }else{
    79. if (ent instanceof Player && dc == DamageCause.SUICIDE) {
    80. Player p = (Player)ent;
    81. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " just wrote a suicide note and said goodbye.");
    82. e.getDrops().clear();
    83. p.getInventory().clear();
    84. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    85. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    86. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    87. p.getInventory().setBoots(new ItemStack(Material.AIR));
    88. if(usedkit.contains(p.getName()));
    89. usedkit.remove(p.getName());
    90. }else{
    91. if (ent instanceof Player && dc == DamageCause.ENTITY_ATTACK) {
    92. Player p = (Player)ent;
    93. e.setDeathMessage(ChatColor.GRAY + p.getName() + ChatColor.GOLD + " was killed by " + ChatColor.GRAY + p.getKiller().getName() + ChatColor.GOLD + " using " + weapon + ".");
    94. e.getDrops().clear();
    95. p.getInventory().clear();
    96. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    97. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    98. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    99. p.getInventory().setBoots(new ItemStack(Material.AIR));
    100. if(usedkit.contains(p.getName()));
    101. usedkit.remove(p.getName());
    102. }
    103. }
    104. }
    105. }
    106. }
    107. }
    108. }
    109. }
    110. }


    This post has been edited 2 times. It was last edited by duker02 Jun 22, 2013.
  4. Offline

    Shzylo

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I have no idea on how to work with Multicraft but I think you were doing something faster than it allowed you.
  5. Offline

    Shzylo

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  6. Offline

    Shzylo

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I went ahead and tagged chasechocolate to see if he knows how to fix it.
  7. Offline

    duker02

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Just to let whoever looks at this next know, I've done some extensive testing and found out that if you hit someone and they die from something else right after, it works. Otherwise, it doesn't.
  8. Online

    chasechocolate

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    1. event.getEntity() (although the name doesn't entail) returns a Player, not an Entity. Just change it to a Player variable and remove all your instanceof checks where you see if it is a player.

    2. Your weapon variable can throw a NullPointerException if the player died from anything other than ENTITY_ATTACK. Put that variable inside your check where you see if the damage cause is ENTITY_ATTACK.

    3. All your if-statements are nested inside each other, it's better to just use else-ifs. Example:
    Code:java
    1. if(dc == DamageCause.DROWNING){
    2.  
    3. } else if(dc == DamageCause.LAVA){
    4.  
    5. } else if(dc == DamageCause.ENTITY_ATTACK){
    6.  
    7. }
  9. Offline

    duker02

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    -snip-

    Sorry, I'm tired and didn't catch what you said about putting it in the check. I will try your suggestions and see if it works!

    EDIT: It works! Thank you so much!

    This post has been edited 2 times. It was last edited by duker02 Jun 23, 2013.

Share This Page