Player Damager from EntityDamageEvent

Discussion in 'Plugin Development' started by FatAussieFatBoy, Dec 12, 2013.

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

    FatAussieFatBoy

    Hey everyone I need to get a Players Damager using the EntityDamageEvent but as player.getDamager() only works in EntityDamageByEntityEvent I am forced to ask this Question... I need to see if the Attacking Entity is a Player, I have tried if(player.getLastDamageCause().getEntity() instanceof Player) but it doesn't work... so my Question is :

    "How would I make it so that the Code checks the Players Damager and to see if it is a Player"

    I need this Urgently as I have a Personal dead-line on this Plugin. Any Help would be much appreciated.

    NOTE : Please Tahg me if you respond so I am notified when I come on the Bukkit Forums.

    Cheers -
    FatAussieFatBoy
     
  2. Offline

    DCDJ

    Code:text
    1. player.getLastDamageCause().getEntity() instanceof Player


    How comes this doesn't work?

    Code:text
    1.  
    2. Entity e = player.getLastDamageCause().getEntity();
    3. if(e!=null) {
    4. if(e instanceof Player) {
    5. Player p = (Player) e;
    6. // Do more stuff
    7. }
    8. }
    9.  
     
  3. Offline

    FatAussieFatBoy

    DCDJ
    Not to sure why, that's why I am asking the Question...
    But on another Note just to add to the Question, how do I get the DamageCause from a Player
    DamageCause.ENTITY_ATTACK doesn't work either...
     
  4. Offline

    mrZcr4fter

    just use EntityDamageByEntityEvent event! it saves hassle
    Code:java
    1. public void onEntityDamage(EntityDamageByEntityEvent event){
    2. Entity damager = event.getDamager();
    3. if (damager instanceof Player){
    4. //do stuff here
    5. }
    6. }
     
  5. Offline

    DCDJ


    I mean, as in, do you get any exceptions thrown/stack-trace in the console? Or does it just do nothing at all?
    The method player.getLastDamageCause() and sub-methods are working for me inside the EntityDamageEvent event, so this is a little strange. Perhaps you could post some more code?
     
  6. Offline

    FatAussieFatBoy

    DCDJ
    I can post my whole Event if you would like but, I use custom references from my Classes so some pieces of code may look out of place...
    Code:java
    1. @EventHandler
    2. public void onPlayerEDE(EntityDamageEvent e) {
    3. if(e.getEntity() instanceof Player) {
    4. Player p = (Player) e.getEntity();
    5. DamageCause cause = e.getCause();
    6. setArena(p);
    7. Arena a = arena;
    8. if(a != null) {
    9. String arenaName = a.getName();
    10. if(a.getPlayers().contains(p.getName())) {
    11. Damageable damge = p;
    12. if(cause == DamageCause.ENTITY_ATTACK) {
    13. if(p.getLastDamageCause().getEntity() instanceof Player) {
    14. Player pl = (Player) p.getLastDamageCause().getEntity();
    15. if(a.getPlayers().contains(pl.getName())) {
    16. if(a.getJuggernaut().equals(p)) {
    17. if(damge.getHealth() - e.getDamage() < 1.5) {
    18. if(a.isInGame()) {
    19. a.setJuggernaut(pl);
    20. a.sendMessage(prefix + msg + "The Juggernaut has been killed!");
    21. a.sendMessage(prefix + gold + pl.getName() + msg + " is the New Juggernaut!");
    22. statsConfig.set("stats." + pl.getName() + ".coins", + 5);
    23. GameEvents.getEvent().playerRespawn(p);
    24. }else{
    25. p.setHealth(damge.getMaxHealth());
    26. p.setFireTicks(0);
    27. p.setFoodLevel(20);
    28. p.teleport(a.getLobby());
    29. }
    30. }else if(damge.getHealth() - e.getDamage() < 1) {
    31. if(a.isInGame()) {
    32. e.setCancelled(true);
    33. p.setHealth((double) 1);
    34. }
    35. }
    36. }else if(!a.getJuggernaut().equals(p) && !a.getJuggernaut().equals(pl)){
    37. e.setCancelled(true);
    38. pl.sendMessage(prefix + error + "Sorry, FriendlyFire is turned off in Juggernaut!");
    39. }
    40. }
    41. }else{
    42. p.sendMessage(ChatColor.DARK_PURPLE + "The Entity is not a Player!");
    43. }
    44. }else{
    45. p.sendMessage(ChatColor.DARK_PURPLE + "You are not being attacked by a Entity or Player!");
    46. if(a.getJuggernaut().equals(p)) {
    47. if(damge.getHealth() - e.getDamage() < 1.5) {
    48. if(a.isInGame()) {
    49. statsConfig.set("stats." + p.getName() + ".coins", - 5);
    50. if(statsConfig.getInt("stats." + p.getName() + ".coins") < 0) {
    51. statsConfig.set("stats." + p.getName() + ".coins", 0);
    52. }
    53. GameEvents.getEvent().playerRespawn(p);
    54. GameEvents.getEvent().JuggernautSelect(arenaName);
    55. }else{
    56. p.setHealth(damge.getMaxHealth());
    57. p.setFireTicks(0);
    58. p.setFoodLevel(20);
    59. p.teleport(a.getLobby());
    60. }
    61. }
    62. }else{
    63. if(damge.getHealth() - e.getDamage() < 1.5) {
    64. if(a.isInGame()) {
    65. statsConfig.set("stats." + p.getName() + ".coins", - 5);
    66. if(statsConfig.getInt("stats." + p.getName() + ".coins") < 0) {
    67. statsConfig.set("stats." + p.getName() + ".coins", 0);
    68. }
    69. GameEvents.getEvent().playerRespawn(p);
    70. }else{
    71. p.setHealth(damge.getMaxHealth());
    72. p.setFireTicks(0);
    73. p.setFoodLevel(20);
    74. p.teleport(a.getLobby());
    75. }
    76. }
    77. }
    78. }
    79. }else{
    80. p.sendMessage(ChatColor.DARK_PURPLE + "This Player is not in the Arena!");
    81. }
    82. }else{
    83. p.sendMessage(ChatColor.DARK_PURPLE + "Arena is Null!");
    84. }
    85. }
    86. }

    mrZcr4fter I have already tried to use the EntityDamageByEntityEvent but can't use it as the EntityDamageEvent overrides the Priority of use, no matter what I set the EventPriority to on the EntityDamageByEntityEvent the EntityDamageEvent overrides it :/
     
  7. Offline

    The_Doctor_123

    FatAussieFatBoy
    And this is why you don't set deadlines. You end up putting in unnecessary tags like "[URGENT]" resulting in SLOWER help.
     
  8. Offline

    FatAussieFatBoy

    The_Doctor_123
    When I say "Personal Dead-line" I mean I can only work on this Plugin for a Set amount of time before I have to make others for my Clients... This Plugin is for my MC Server and I need it Urgently as I can not waste anymore time on this Error (already spent over 3 Days trying to fix it).

    So please if you "Can Help" then please Tahg Me in a response.

    Cheers -
    FatAussieFatBoy
     
Thread Status:
Not open for further replies.

Share This Page