Solved Changing Bow damage

Discussion in 'Plugin Development' started by isleepzzz, May 17, 2014.

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

    isleepzzz

    Hello there guys!
    So I just came back from a LONG break of programming in java.
    Now, I am having a problem here. (honestly, the code might even work) however, I'm looking at it and it doesn't seem properly and I cannot figure out what I'm going to do to make it right.
    So what I want: When shoot a bow and the arrow its either a player or mob then do damage that is on the bow's meta lore with something like "Arrow Damage: 1-3" so it will deal damage randomly 1-3.

    My code so far:
    Code:java
    1. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    2. Entity damager = event.getDamager();
    3.  
    4.  
    5. if (damager instanceof Arrow) {
    6. final Arrow arrow = (Arrow) damager;
    7. ItemStack i = ((Player) damager).getPlayer().getItemInHand();
    8. Player p = (Player) damager;
    9.  
    10. if (i != null && i.hasItemMeta()) {
    11. for (String s : i.getItemMeta().getLore()) {
    12.  
    13. if (s.contains("Arrow Damage:")) {
    14. String dmgStr = s.substring(16);
    15. String dmgAmnt[] = dmgStr.split("-");
    16. Random rand = new Random();
    17. int damageAmount = rand.nextInt(Integer.parseInt(dmgAmnt[1]) - Integer.parseInt(dmgAmnt[0]) + 1) + Integer.parseInt(dmgAmnt[0]);
    18. event.setDamage(damageAmount);
    19. }
    20.  
    21. }
    22. }
    23. }


    Yup, hit a cow and gave errors:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    rsod

    1) listen for EntityShootBowEvent. Set some metadata on arrows that were fired from specified bow.
    2) Listen for EntityDamageByEntityEvent. If damager is arrow, then check if there is a metadata and if there is, then change damage.
    And your error is because you're checking if damager is arrow
    if (damager instanceof Arrow) {
    and immediately casting damager to player:
    ItemStack i = ((Player) damager)
     
  3. Offline

    isleepzzz

    Fixed it thanks!
     
Thread Status:
Not open for further replies.

Share This Page