Solved Creating method to set itemstack damage by a percentage

Discussion in 'Spigot Plugin Development' started by ENCOM, Feb 26, 2020.

Thread Status:
Not open for further replies.
  1. hi there

    im about to make a custom method which needs an itemstack like diamondsword and a percentage of durability

    Code (Java):
    ItemStack item = new ItemStack(Material.DIAMOND_SWORD, 1);
            Damageable meta = (Damageable) item.getItemMeta();
            meta.setDamage(300);
            item.setItemMeta((ItemMeta) meta);
    its using a deprecated method, to set the damage but it still works, a method to do this job would be very handy
    i think, i would need t get the item's base durability value to apply the percentage of remaining durability.

    i found some posts in here, but it no longer works in newer spigot versions like 1.14 which im using currently,
    does someone has a clue on how to get the base durability for fresh itemstacks?
    Code (Java):
    item.getDurability()
    doesnt seem to work, all help is appreciated
     
  2. You can use this method for damage by percentage
    Code (Java):
        public ItemStack getDamagedItem(ItemStack itemStack, int percentage) {
            Damageable meta = (Damageable) itemStack.getItemMeta();
            meta.setDamage((itemStack.getType().getMaxDurability() / 100) * percentage);
            itemStack.setItemMeta((ItemMeta) meta);
            return itemStack;
        }
     
    • Winner Winner x 1
  3. Thanks a lot Speedy you made my day!<3
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.