How to get the attack damage of an item

Discussion in 'Plugin Development' started by ChintziSecilMC, Aug 1, 2015.

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

    ChintziSecilMC

    How do i get the attack damage of an item that an entity or player is holding?
     
  2. Offline

    PDKnight

    I think it's
    Code:java
    1. (your ItemStack).getDurability();
     
  3. Offline

    ChintziSecilMC

    @PDKnight Thanks, but thats getting its usage, i'm talking about its physical damage it does to entites
     
  4. Offline

    PDKnight

    Well, I forgot :D I'll make code for you soon, just wait...
     
  5. Offline

    ChintziSecilMC

  6. Offline

    PDKnight

    Finally, here's the code:
    Code:java
    1. public int getItemDamageValue(ItemStack is, Player damaged) {
    2. double damageValue = 0;
    3. if(is != null) {
    4. if (is.getType() == Material.AIR) {
    5. damageValue = 0;
    6. } else if (is.getType() == Material.WOOD_SWORD) {
    7. damageValue = 5;
    8. } else if (is.getType() == Material.STONE_SWORD) {
    9. damageValue = 6;
    10. } else if (is.getType() == Material.IRON_SWORD) {
    11. damageValue = 7;
    12. } else if (is.getType() == Material.DIAMOND_SWORD) {
    13. damageValue = 8;
    14. } else {
    15. damageValue = 1; // other blocks & items
    16. }
    17. }
    18.  
    19. if(damaged != null) {
    20. Inventory i = damaged.getInventory();
    21. Material helmet = i.getItem(39).getType();
    22. Material chestplate = i.getItem(40).getType();
    23. Material leggings = i.getItem(41).getType();
    24. Material boots = i.getItem(42).getType();
    25. if (helmet == Material.LEATHER_HELMET)
    26. damageValue -= (0.5 / 1.5);
    27. // value shown at bar above the health bar / 1.5
    28. else if (helmet == Material.CHAINMAIL_HELMET
    29. || helmet == Material.IRON_HELMET
    30. || helmet == Material.DIAMOND_HELMET
    31. || helmet == Material.GOLD_HELMET)
    32. damageValue -= (1 / 1.5);
    33.  
    34. if (chestplate == Material.LEATHER_CHESTPLATE)
    35. damageValue -= (1.5 / 1.5);
    36. else if (chestplate == Material.CHAINMAIL_CHESTPLATE
    37. || chestplate == Material.GOLD_CHESTPLATE)
    38. damageValue -= (2.5 / 1.5);
    39. else if (chestplate == Material.IRON_CHESTPLATE)
    40. damageValue -= (3 / 1.5);
    41. else if (chestplate == Material.DIAMOND_CHESTPLATE)
    42. damageValue -= (4 / 1.5);
    43.  
    44. if (leggings == Material.LEATHER_LEGGINGS)
    45. damageValue -= (1 / 1.5);
    46. else if (leggings == Material.GOLD_LEGGINGS)
    47. damageValue -= (1.5 / 1.5);
    48. else if (leggings == Material.GOLD_LEGGINGS)
    49. damageValue -= (1.5 / 1.5);
    50. else if (leggings == Material.GOLD_LEGGINGS)
    51. damageValue -= (1.5 / 1.5);
    52. else if (leggings == Material.CHAINMAIL_LEGGINGS)
    53. damageValue -= (2 / 1.5);
    54. else if (leggings == Material.IRON_LEGGINGS)
    55. damageValue -= (2.5 / 1.5);
    56. else if (leggings == Material.DIAMOND_LEGGINGS)
    57. damageValue -= (3 / 1.5);
    58.  
    59. if (boots == Material.LEATHER_BOOTS
    60. || boots == Material.GOLD_BOOTS
    61. || boots == Material.CHAINMAIL_BOOTS)
    62. damageValue -= (0.5 / 1.5);
    63. else if (boots == Material.IRON_BOOTS)
    64. damageValue -= (1 / 1.5);
    65. else if (boots == Material.DIAMOND_BOOTS)
    66. damageValue -= (1.5 / 1.5);
    67.  
    68. }
    69. return (int) Math.round(damageValue);
    70. }

    Anyway, I didn't test bow, because I couldn't shot myself, but everything's here :D

    Usage:
    Code:java
    1. ItemStack item = somePlayer.getInventory().getItemInHand();
    2. int damage = getItemDamageValue(item, somePlayer);

    • damage 1 means half a heart, damage 2 one heart etc...
    somePlayer should be the damaged player, the function checks for his armor (so when somebody will hit him the item will have less damage if he has armor on), if you don't want to use this value, just write
    Code:java
    1. ItemStack item = somePlayer.getInventory().getItemInHand();
    2. int damage = getItemDamageValue(item);


    I didn't test it yet, but it should work :) Hope this helps :D
     
Thread Status:
Not open for further replies.

Share This Page