Solved NBT doesnt work

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Oct 13, 2018.

Thread Status:
Not open for further replies.
  1. I've tried to make this setDamage thing to an item and an amount which sets the generic.attackDamage number to the item, but it just does nothing. Anyone know why? (Also some people say you need to add nbts like which hand, but that's optional).

    Code (Java):

    public static void setDamage(ItemStack item, final int amount) {
            net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
            NBTTagCompound compound = nmsStack.hasTag() ? nmsStack.getTag() : new NBTTagCompound();
            NBTTagList modifiers = new NBTTagList();
            NBTTagCompound damage = new NBTTagCompound();
            damage.set("AttributeName", new NBTTagString("generic.attackDamage"));
            damage.set("Name", new NBTTagString("generic.attackDamage"));
            damage.set("Amount", new NBTTagInt(amount));


            compound.set("AttributeModifiers", modifiers);

            meta = CraftItemStack.asBukkitCopy(nmsStack).getItemMeta();
    #1 Jaffaaaa, Oct 13, 2018
    Last edited: Oct 13, 2018
    • Agree Agree x 1
  2. For people looking how to do this using 1.13 without NMS:
    Code (Text):
    public static void setDamage(ItemStack itemStack, final int amount) {
        ItemMeta meta = itemStack.getItemMeta();
        meta.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE); // Remove any existing ones, this isn't strictly necessary
        meta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, new AttributeModifier("generic.attackDamage", amount, Operation.ADD_NUMBER));
    • Like Like x 2
    • Agree Agree x 2
  3. I've used the same code as I had in a plugin before, i added your stuff (it works now <3) but no idea how it worked before
Thread Status:
Not open for further replies.

Share This Page