Resource Adding Glow to items In 1.13

Discussion in 'Spigot Plugin Development' started by DiamondDagger590, Oct 7, 2018.

  1. Hey guys, so I noticed a bit of an issue in 1.13 when adding glow to an ItemStack. The old approach of adding some random enchantment and then setting a HideEnchantments item flag starts breaking here.
    Code (Text):
    item.addUnsafeEnchantment(Enchantment.LUCK, 1);
    ItemMeta meta = item.getItemMeta();
    meta.addItemFlags(org.bukkit.inventory.ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(meta);
    The code above is the old method. This still works in 1.13 however there is a bit of a different approach you must take. In 1.13 setting an items metaData AFTER adding an unsafe enchantment will clear all added enchantments from that item. Now this is a bit of a problem but it can be solved by adding the metaData BEFORE you add the enchantments and the effect is the same.
    Code (Text):
    ItemMeta meta = item.getItemMeta();
    meta.addItemFlags(org.bukkit.inventory.ItemFlag.HIDE_ENCHANTS);
    item.setItemMeta(meta);
    item.addUnsafeEnchantment(Enchantment.LUCK, 1);
    A simple fix but one that stumped me for a long while. Hopefully this helps yall!
     
  2. try nbt tag. working
     
  3. But How ? ^^

    This wont work:
    Code (Java):
        public static org.bukkit.inventory.ItemStack addGlow(org.bukkit.inventory.ItemStack item){
            net.minecraft.server.v1_13_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
            NBTTagCompound tag = null;
            if(!nmsStack.hasTag()){
                tag = new NBTTagCompound();
                nmsStack.setTag(tag);
            }
            if(tag == null) {
                tag = nmsStack.getTag();
            }
            NBTTagList ench = new NBTTagList();
            tag.set("Enchantments", ench);
            nmsStack.setTag(tag);
            return CraftItemStack.asCraftMirror(nmsStack);
        }
     
  4. The problem is probably that you were getting the meta, then adding the enchantment to the ItemStack and then setting the un-enchanted old meta to the ItemStack. Enchantments were one of, if not the first case of complex data that was added to items in Minecraft in addition to the plain ID and DV numbers as of version 1.0. The Bukkit devs at that time had to invent a new API for item meta and were in the earliest days of this API not as consequently separating it from the ItemStack API as they were later. That's why there are methods to modify enchantments (and durability) in the ItemStack class, even though this kind of data normally goes into ItemMeta. There is nothing wrong with the ItemStack enchantment methods because any ItemStack (except maybe air types) can have enchantments, but if you modify ItemMeta, it is better to consistently use ItemMeta methods for all kinds of meta changes. This avoids problems with simultaneous modifications of two different ItemMeta copies.
     
  5. Yeah I don't see how this is a useful resource as it was just a logic error by OP.
     
    • Agree Agree x 1