Solved Get item damage of tools

Discussion in 'Plugin Development' started by knokko, Oct 21, 2018.

Thread Status:
Not open for further replies.
  1. For my plug-in, I need a stable way to get the damage of a tool, weapon or armor (and any other item that can take damage).

    So far, I only found ItemStack#getDurability().
    I suppose the logic of the method name is too complex for me to understand, but that method returns the damage of an item in most cases. Unfortunately, not in all cases.

    When the item is unbreakable, the result of the method depends on the version of minecraft/bukkit. On 1.12.2, it will just return the damage of the item. On 1.13, it will return 0 regardless of the item damage. I haven't tried versions prior to 1.12 because my plug-in requires the Short2ObjectMap at the moment.

    Because this is a surprisingly hard task, I created a static method with the name getDamage and I now refer to that method every time I need to get item damage.
    But now the great question: What do I need to put in that method to let it work with any version of minecraft/bukkit?
     
  2. Offline

    Zombie_Striker

    This is not true. My plugin QualityArmory uses the unbreakable tag on diamond_axes, and getDurability does return the damage value.

    If it is returning 0, can you post your code?
     
    knokko likes this.
  3. @Zombie_Striker

    Now things are getting interesting...
    I will do some deeper comparing...
    Maybe, I will find out why things work in 1.12, but not in 1.13, because this doesn't make any sense.

    Edit
    You were right, the problem was not with the getDurability(), it was the way I created the item.

    I used to create the item with
    Code:
    public ItemStack create(int amount){
            ItemStack item = new ItemStack(material, amount);
           item.setDurability(itemDamage);
            ItemMeta meta = Bukkit.getItemFactory().getItemMeta(material);
            meta.setDisplayName(displayName);
            meta.setLore(Lists.newArrayList(lore));
            meta.setUnbreakable(true);
            item.setItemMeta(meta);
            return item;
        }
    This worked fine in minecraft 1.12 because the order didn't matter back then, but it does matter in 1.13.

    So I had to change my code to this:
    Code:
    public ItemStack create(int amount){
            ItemStack item = new ItemStack(material, amount);
            ItemMeta meta = Bukkit.getItemFactory().getItemMeta(material);
            meta.setDisplayName(displayName);
            meta.setLore(Lists.newArrayList(lore));
            meta.setUnbreakable(true);
            item.setItemMeta(meta);
            item.setDurability(itemDamage);
            return item;
        }
    It looks like I accused the wrong method. (Also, it looks like the alternative /give method of Essentials messed with me when I tested it on tools given with the /give command).
     
    Last edited: Oct 22, 2018
Thread Status:
Not open for further replies.

Share This Page