EntityDamage Problem

Discussion in 'Plugin Development' started by TheHandfish, Apr 13, 2014.

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

    TheHandfish

    There's a problem with my code (duh). I'm trying to get it so if you shoot a horse with an arrow and it has iron armor, the shot will be deflected. If the horse is wearing diamond armor, it can deflect arrows and fire will not effect it.

    However, it's not detecting the damage the right way.

    Code:javascript
    1. /*
    2.  * To change this license header, choose License Headers in Project Properties.
    3.  * To change this template file, choose Tools | Templates
    4.  * and open the template in the editor.
    5.  */
    6.  
    7. package com.hand.Race;
    8.  
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.EntityType;
    13. import org.bukkit.entity.Horse;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.entity.EntityDamageEvent;
    18. import org.bukkit.inventory.ItemStack;
    19.  
    20. /**
    21.  *
    22.  * @author Robert
    23.  */
    24. public class Damage implements Listener
    25. {
    26. public static Main plugin;
    27. public Damage(Main ins)
    28. {
    29. Damage.plugin = ins;
    30. }
    31.  
    32. @EventHandler
    33. public static void onDamage(EntityDamageEvent event)
    34. {
    35. Entity e = event.getEntity();
    36. Horse h = (Horse) event.getEntity();
    37. Player p = (Player) h.getPassenger();
    38. p.sendMessage("Notifier: Some entity was injured.");
    39.  
    40. if(e.equals(EntityType.HORSE))
    41. {
    42. p.sendMessage("Notifier: A horse was injured.");
    43. if(h.getInventory().getArmor().equals(new ItemStack(Material.IRON_BARDING)) && event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE)
    44. {
    45. p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
    46.  
    47. event.setCancelled(true);
    48.  
    49. }
    50. if(h.getInventory().getArmor().equals(new ItemStack(Material.DIAMOND_BARDING)))
    51. {
    52. if (event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE || h.getFireTicks() != 0 || event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK)
    53. {
    54. p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
    55. h.setFireTicks(0);
    56. event.setCancelled(true);
    57. }
    58.  
    59. }
    60.  
    61. }
    62. }
    63. }
    64.  

    I only ever get the notifier for general entity damage if I set the horse on fire. I don't get the other notifier that's supposed to go off if a horse is damaged. Should I just use another event?
    Thanks.
     
  2. Offline

    ZapChance

    TheHandfish If I'm reading this correctly (Probably not, it's 11:30), but you're casting 'Horse' to the damaged entity before you check that it is a horse. And you're also assuming that there is a rider, which, in case there might not be, because the entity isn't even a horse! It wouldn't send the message if the rider was a null pointer exception.
     
    Qwahchees likes this.
  3. Offline

    Qwahchees

    ZapChance is right.

    For the
    Code:
    if (e.equals(EntityType.HORSE)) {
    Change it to:
    Code:
    if (e instanceof Horse) {
    And make sure to cast
    Code:
    Horse h = (Horse) e.getEntity();
    after you do the if statement for instanceof Horse.

    Player p = (Player) h.getPassenger(); as well, put that after instanceof Horse if statement.
     
  4. Offline

    TheHandfish

    ZapChance and Qwahchees:

    Thanks, but I'm still getting an error. I've traced it to:

    Code:
    EntityDamageEvent.DamageCause.PROJECTILE
    I don't think that's being recognized when I shoot the horse with an arrow.
     
  5. Offline

    ZapChance

    TheHandfish I believe your reason for that is that you're attempting to shoot the arrow at the horse and send a notification to the passenger, when there most likely isn't a passenger.
     
  6. Offline

    TheHandfish

    ZapChance:

    I put in notifiers for when the horse is shot, and they show up. What do you suggest I do?
     
  7. Offline

    Blackveil

    Switch to EntityDamageByEntityEvent

    EntityDamageEvent.getEntity() - returns the attacker.
     
  8. Offline

    TheHandfish

    Blackveil:

    Wait it does? I thought it returns the entity being damaged. o_o

    Still getting issues. Ran a notifier to tell me what entity is harming the horse, and I get "ARROW" if I shoot it with a bow...

    Code:
    I have: if(a instanceof Arrow)
    Where

    Code:
    Entity a = event.getDamager();
    But it never goes with the instance that the entity is an arrow, even if it says the entity I'm shooting it with is an arrow...

    Just to recap, here's what I've got.

    Code:javascript
    1.  
    2. public static void onDamage(EntityDamageByEntityEvent event)
    3. {
    4. Entity e = event.getEntity();
    5. //plugin.getServer().broadcastMessage("Notifier: Some entity was injured.");
    6.  
    7. if (e instanceof Horse)
    8. {
    9. Horse h = (Horse) e;
    10. Player p = (Player) h.getPassenger();
    11. Entity a = event.getDamager();
    12. plugin.getServer().broadcastMessage("Notifier: A horse was injured. " + a.getType());
    13.  
    14. if(h.getInventory().contains(new ItemStack(Material.IRON_BARDING)) && a instanceof Arrow)
    15. {
    16. p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
    17. plugin.getServer().broadcastMessage("Notifier: Deflecting!");
    18.  
    19. event.setCancelled(true);
    20.  
    21. }
    22. if(!(a instanceof Arrow))
    23. {
    24. plugin.getServer().broadcastMessage("Wasn't an arrow!");
    25. }
    26. else
    27. {
    28. plugin.getServer().broadcastMessage("WAS an arrow!");
    29. }
    30. if(!h.getInventory().contains(new ItemStack(Material.IRON_BARDING)) && !h.getInventory().contains(new ItemStack(Material.DIAMOND_BARDING)))
    31. {
    32. plugin.getServer().broadcastMessage("Wasn't wearing horse armor!");
    33.  
    34. }
    35. if(h.getInventory().contains(new ItemStack(Material.DIAMOND_BARDING)))
    36. {
    37. if (a instanceof Arrow || h.getFireTicks() != 0 || event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK)
    38. {
    39. p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
    40. h.setFireTicks(0);
    41. event.setCancelled(true);
    42. }
    43.  
    44. }
    45.  
    46. }
    47. }
    48.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page