Solved How to calculate damage if the entity is wearing armor?

Discussion in 'Plugin Development' started by MrShnig, Nov 11, 2018.

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

    MrShnig

    Hey there, I'm making a plugin where I want to make it so that players can fight each other at a distance (like a super-powers plugin).
    I can get the attack damage of the item the player is holding, and then apply that damage to the opponent, but what if the opponent was wearing armor? And then what if the armor had protection or thorns? I need to know how I'd set up the calculations for the damage the player would then cause to the opponent.

    Here's the code I'm using for the reach:
    Code:
        @EventHandler
        public void onAttack(PlayerInteractEvent e) {
           
            Player player = e.getPlayer();
           
            if(e.getAction().equals(Action.LEFT_CLICK_AIR) && player.hasPermission("distanceAttacks")) {
               
                Location start = player.getEyeLocation();
                Vector dir = player.getLocation().getDirection();
               
                for (double i = 0; i < 10; i += 0.2) {
                   
                    start.add(dir.getX() * i, dir.getY() * i, dir.getZ() * i);
                       
                    if(start.getBlock().getType() != Material.AIR || start.getBlock().getType() != Material.YELLOW_FLOWER || start.getBlock().getType() != Material.LONG_GRASS) break;
                       
                    for(Entity entity : player.getWorld().getNearbyEntities(start, 0.1, 0.1, 0.1)) {
                       
                        if(entity instanceof Damageable) {
                           
                            ((Damageable) entity).damage(getAttackDamage(player.getItemInHand()));
                           
                        }
                       
                        break;
                       
                    }
                   
                    start.subtract(dir);
                    dir.normalize();
                   
                }
               
            }
           
        }
    Here's the code I'm using to get the held weapon's attack damage (I didn't write this part):
    Code:
        public double getAttackDamage(ItemStack itemStack) {
           
            double attackDamage = 1.0;
           
            UUID uuid = UUID.fromString("CB3F55D3-645C-4F38-A497-9C13A33DB5CF");
           
            net.minecraft.server.v1_9_R1.ItemStack craftItemStack = CraftItemStack.asNMSCopy(itemStack);
            net.minecraft.server.v1_9_R1.Item item = craftItemStack.getItem();
           
            if(item instanceof net.minecraft.server.v1_9_R1.ItemSword || item instanceof net.minecraft.server.v1_9_R1.ItemTool || item instanceof net.minecraft.server.v1_9_R1.ItemHoe) {
               
                Multimap<String, AttributeModifier> map = item.a(EnumItemSlot.MAINHAND);
               
                Collection<AttributeModifier> attributes = map.get(GenericAttributes.ATTACK_DAMAGE.getName());
                        if(am.a().toString().equalsIgnoreCase(uuid.toString()) && am.c() == 0) attackDamage += am.d();
                       
                    }
                   
                    double y = 1;
                   
                    for(AttributeModifier am: attributes) {
                       
                        if(am.a().toString().equalsIgnoreCase(uuid.toString()) && am.c() == 1) y += am.d();
                       
                    }
                   
                    attackDamage *= y;
                   
                    for(AttributeModifier am: attributes) {
                       
                        if(am.a().toString().equalsIgnoreCase(uuid.toString()) && am.c() == 2) attackDamage *= (1 + am.d());
                       
                    }
                   
                }
               
            }
           
            return attackDamage;
           
        }
     
  2. Offline

    Zombie_Striker

  3. Offline

    MrShnig

  4. Offline

    MrShnig

Thread Status:
Not open for further replies.

Share This Page