Solved Durability bar decreases even though I make the item unbreakable

Discussion in 'Spigot Plugin Development' started by SkillCode, Jul 7, 2021.

  1. Hello, I would like to make items unbreakable in my plugin via NMS, however this is not working quite the way I wanted it to. The durability bar decreases (it resets when you right click the item). Is there a way to make the bar not change? Thanks!

    Code (Java):
    public ItemStack setUnbreakable(@NotNull ItemStack itemStack) {
            try {
                Class<?> craftItemStack = getOBCClass("inventory.CraftItemStack");
                Method getNMSI = craftItemStack.getMethod("asNMSCopy", ItemStack.class);
                Object nms = getNMSI.invoke(null, itemStack);
                Object tag = getNMSClass("NBTTagCompound", "net.minecraft.nbt.NBTTagCompound").getConstructor().newInstance();
                tag.getClass().getMethod("setInt", String.class, int.class).invoke(tag, "Unbreakable", 1);
                nms.getClass().getMethod("setTag", tag.getClass()).invoke(nms, tag);
                itemStack = (ItemStack) craftItemStack.getMethod("asCraftMirror", nms.getClass()).invoke(null, nms);
            } catch (Exception e) {
            return itemStack;
  2. Why you do not use ItemMeta#spigot#setUnbreakable ?
  3. It doesnt work on higher versions
  4. I wouldn't recommend using NMS, and totally not use reflection. Check out this useful API It's super easy to use and intuitive. Hope this helps :D
  5. For 1.8 it’s an NBT TAG, no need for reflection or NMS. Google it nbt tag unbreakable 1.8
  6. But my plugin should be compatible for all versions
  7. After 1.12 it isn’t really a NBT TAG tho, but I might be wrong. It is however easier to create an unbreakable item. You can check the server version and use the right NBT tag.
  8. You are using 1.13 then use meta.setUnbreakable(true); this code is working
  9. Title says 1.18…
  10. I found simple solution, I just had to update the inventory. Thanks for all your answers!
    Code (Java):