Solved How to get power enchantment level of an Arrow?

Discussion in 'Plugin Development' started by CruzAPI, Jul 21, 2016.

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

    CruzAPI

    Hi, i am making a plugin and I need to know the level of the bow that shot an arrow.
    I found a method of the interface Arrow that returns the knockback level of the arrow
    -> arrow.getKnockbackStrength(), i need the same but for the power level.
     
  2. Offline

    SeniorCluckers

    @CruzAPI

    You could check if the bow has the enchantment..

    Using
    Code:
    .getEnchantmentLevel(Enchantment.ENCHANTMENT_HERE) == ENCHANTMENT_LEVEL HERE)
     
    Last edited: Jul 21, 2016
  3. Offline

    CruzAPI

    @SeniorCluckers

    If the bow has enchantment? But how i will get the bow? I am checking it in a EntityDamageByEntityEvent and checking if damager instanceof Arrow...
    It is wrong, have to have any way to get the power of an Arrow
     
  4. Offline

    SeniorCluckers

    @CruzAPI
    I don't understand why check if the instance is an arrow? Why not just check if the instance is a player, then check his inventory, get the item in hand, then get the enchantment and the level of the enchantment.
     
  5. Offline

    CruzAPI

    Because it is not instant, the player can shot an arrow and after this change his slot, drop the bow or anything else.
     
  6. Offline

    SeniorCluckers

    @CruzAPI
    So you have tried something similar to this already? Also whats your main goal with getting the arrow damage? Cancelling it or removing the enchantment?

    Code:
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player player = (Player) event.getEntity();
                ItemStack item = player.getInventory().getItemInHand();
              
                if(item.getEnchantmentLevel(Enchantment.ARROW_DAMAGE) == 4) {
                  
                    event.setCancelled(true);
                  
                }
              
            }
        }
     
  7. Offline

    CruzAPI

    @SeniorCluckers
    I don't want remove enchanting, i want to do my own damage and I need to know the power level of the bow.

    I am making it and i need to know the level of ARROW_DAMAGE of the arrow to change the damage

    Code:
    @EventHandler
    public void damage(EntityDamageByEntityEvent e)
    {
        if(e.getEntity() instanceof Player)
        {
            Player entity = (Player)e.getEntity();
        
            if(e.getDamager() instanceof Projectile)
            {
                Projectile projectile = (Projectile)e.getDamager();
                Entity shooter = projectile.getShooter();
            
                if(shooter instanceof Player)
                {
                    Player damager = (Player)shooter;
                 
                    if(projectile instanceof Arrow)
                    {
                        Arrow arrow = (Arrow)projectile;
                        //and here i need to know how much is the power of this arrow
                        //...to change the damage here
                        e.setDamage(3.0D + (arrow.isCritical() ? 1.5D : 0.0D));
                    }
                }
            }
        }
    }
     
  8. Offline

    Zombie_Striker

    @CruzAPI
    The only way to store that information would be to listen to the ProjectileLaunchEvent. When that event if triggered, check if the shooter is a player and if they are shooting an arrow. If so, store that information in a Map. When the arrow hits a target, get the arrow instance and get the data from the map.
     
  9. Offline

    CruzAPI

    @Zombie_Striker
    Yes, I was thinking in it, but I thought that had an easier way to get it.
    Thanks you to clarify my question.
     
Thread Status:
Not open for further replies.

Share This Page