Reduce damage on Chainmail armor

Discussion in 'Plugin Development' started by GhostRecovery, Feb 14, 2019.

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

    GhostRecovery

    Hi guys,

    I’ve been trying for months to create a plugin to reduce the damage received when wearing a Chainmail armor but I don’t know how to do it so if you have an idea ^^
    In this plugin I have assigned enchantments while I do not wish that the item is already enchanted
    (Sorry for my bad english)
    First plugin :
    Code:
    package fr.noufkou.emeraldproject;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    
    public class Main extends JavaPlugin {
    
       public Main() {}
        
        public void onEnable()
        {
        ItemStack hel = new ItemStack(Material.CHAINMAIL_HELMET);
        ItemMeta ihel = hel.getItemMeta();
        ihel.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
        ihel.addEnchant(Enchantment.DURABILITY, 25, true);
        ihel.addEnchant(Enchantment.PROTECTION_FIRE, 10, true);
        
        ihel.setDisplayName("§2EmeraldHelmet");
        hel.setItemMeta(ihel);
        
        ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
        LeatherArmorMeta ichest = (LeatherArmorMeta)chest.getItemMeta();
        ichest.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
        ichest.addEnchant(Enchantment.PROTECTION_FIRE, 10, true);
        
        ichest.addEnchant(Enchantment.DURABILITY, 25, true);
        
    
        ichest.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE });
        ichest.setColor(Color.GREEN);
        ichest.setDisplayName("§2EmeraldChestPlate");
        
        chest.setItemMeta(ichest);
        
        ItemStack legg = new ItemStack(Material.LEATHER_LEGGINGS);
        LeatherArmorMeta ilegg = (LeatherArmorMeta)legg.getItemMeta();
        ilegg.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
        ilegg.addEnchant(Enchantment.DURABILITY, 25, true);
        ilegg.addEnchant(Enchantment.PROTECTION_FIRE, 10, true);
        
    
        ilegg.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE });
        ilegg.setColor(Color.GREEN);
        ilegg.setDisplayName("§2EmeraldLeggings");
        
        legg.setItemMeta(ilegg);
        
    
        ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
        LeatherArmorMeta iboot = (LeatherArmorMeta)boot.getItemMeta();
        iboot.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 10, true);
        iboot.addEnchant(Enchantment.DURABILITY, 25, true);
        iboot.addEnchant(Enchantment.PROTECTION_FIRE, 10, true);
        
    
    
    
    
        iboot.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE });
        iboot.setColor(Color.GREEN);
        iboot.setDisplayName("§2EmeraldBoots");
        boot.setItemMeta(iboot);
        
    
    
    
    
    
        ItemStack sword = new ItemStack(Material.WOOD_SWORD);
        ItemMeta swordmeta = sword.getItemMeta();
        swordmeta.setDisplayName("§aEmmeraldSword");
        swordmeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);
        swordmeta.addEnchant(Enchantment.DURABILITY, 100, true);
        swordmeta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
        swordmeta.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE });
        sword.setItemMeta(swordmeta);
        
        ItemStack axe = new ItemStack(Material.WOOD_SWORD);
        ItemMeta axemeta = axe.getItemMeta();
        axemeta.setDisplayName("§aEmmeraldAxe");
        axemeta.addEnchant(Enchantment.DAMAGE_ALL, 15, true);
        axemeta.addEnchant(Enchantment.DURABILITY, 100, true);
        axemeta.addEnchant(Enchantment.LOOT_BONUS_BLOCKS, 10, true);
        axemeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 10, true);
        axemeta.addEnchant(Enchantment.SILK_TOUCH, 1, true);
        axemeta.addEnchant(Enchantment.DIG_SPEED, 30, true);
        axemeta.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE });
        axe.setItemMeta(axemeta);
        
        
    
        ShapedRecipe sh = new ShapedRecipe(chest);
        sh.shape(new String[] { "S S",
        "SSS",
        "SSS" }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh);
        
    
        ShapedRecipe sh2 = new ShapedRecipe(legg);
        sh2.shape(new String[] { "SSS",
        "S S",
        "S S" }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh2);
        
    
        ShapedRecipe sh3 = new ShapedRecipe(hel);
        sh3.shape(new String[] { "SSS",
        "S S",
        "  " }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh3);
        
        ShapedRecipe sh4 = new ShapedRecipe(hel);
        sh4.shape(new String[] { "  ",
        "SSS",
        "S S" }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh4);
        
        ShapedRecipe sh5 = new ShapedRecipe(boot);
        sh5.shape(new String[] { "S S",
        "S S",
        "  " }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh5);
        
        ShapedRecipe sh6 = new ShapedRecipe(boot);
        sh6.shape(new String[] { "  ",
        "S S",
        "S S" }).setIngredient('S', Material.EMERALD);
        
    
        Bukkit.addRecipe(sh6);
        
    
    
        ShapedRecipe sh7 = new ShapedRecipe(axe);
        sh6.shape(new String[] { " SS",
        " BS",
        " B " }).setIngredient('S', Material.EMERALD).setIngredient('B', Material.STICK);
        
    
        Bukkit.addRecipe(sh7);
        
    
        ShapedRecipe sh8 = new ShapedRecipe(axe);
        sh6.shape(new String[] { "SS ",
        "SB ",
        " B " }).setIngredient('S', Material.EMERALD).setIngredient('B', Material.STICK);
        
    
        Bukkit.addRecipe(sh8);
        
    
    
        ShapedRecipe sh9 = new ShapedRecipe(sword);
        sh6.shape(new String[] { " S ",
        " S ",
        " B " }).setIngredient('S', Material.EMERALD).setIngredient('B', Material.STICK);
        
    
        Bukkit.addRecipe(sh9);
        
        ShapedRecipe sh10 = new ShapedRecipe(sword);
        sh6.shape(new String[] { "S  ",
        "S  ",
        "B  " }).setIngredient('S', Material.EMERALD).setIngredient('B', Material.STICK);
        
    
        Bukkit.addRecipe(sh10);
        
        ShapedRecipe sh11 = new ShapedRecipe(sword);
        sh6.shape(new String[] { "  S",
        "  S",
        "  B" }).setIngredient('S', Material.EMERALD).setIngredient('B', Material.STICK);
        
    
        Bukkit.addRecipe(sh11);
        super.onEnable();
        }
      
    }
     
  2. Offline

    Tango_

    If you are trying to reduce damage on the player:
    The way I would do it is create an EntityDamageByEntity event, detect if the armour matches chainmail, then you can use the event to set the damage. You can reduce the damage by 70% if you set the damage to this:
    e.setDamage(e.getDamage / 100 * 30)

    If you are trying to reduce damage on the chainmail armour:
    Use PlayerItemDamageEvent, then you can generate a random number between 1 and 100, check if the number is above the chance of getting damage, if not then cancel the event and no damage will be done to the armour.
     
  3. Offline

    GhostRecovery

    Ok thanks i try it
     
  4. Offline

    GhostRecovery

    I can't Use PlayerItemDamageEvent on craftbukkit 1.7.9 :/
     
  5. Online

    timtower Administrator Administrator Moderator

    Then please say so when you are using an old version.
     
  6. Offline

    GhostRecovery

    Ah ok sorry ^^
     
  7. Offline

    GhostRecovery

    Do you have another idea ? ^^
     
  8. Offline

    Tango_

    You will probably have to listen to EntityDamageByEntity event, get the damaged players inventory, get their armour and maybe add 1 back to its durability based on a chance using a random generated number, you will have to work out the chance that the armour will be damaged, this all depends on how strong you want the chainmail armour to be.
     
    Last edited: Feb 22, 2019
  9. Offline

    Chr0mosom3

    @GhostRecovery, check the damage that chainmail armor prevents on some Minecraft wiki and in EntityDamageByEntity (don't forget to check for instanceof player) get the armor the player is wearing. Have an int variable of damage prevention and have an if statement checking if the player is wearing chainmail hat, if yes then add +1 for example to the damage prevention and at the end of the event set the player health to the player health + damage prevention.
     
Thread Status:
Not open for further replies.

Share This Page