Tutorial [1.11] The Complete Guide to Item NBTTags & Attributes!

Discussion in 'Resources' started by 87pen, Mar 14, 2016.

Thread Status:
Not open for further replies.
  1. Disclaimer: "This is my first tutorial/resource I have ever made. ALL comments are appreciated especially ones that can improve the tutorial or rate it. (Please don't mind my english, I know I suck at it.) Thanks for reading.

    When dealing with NBTTags NMS is required. This tutorial is made on Spigots 1.9R1 Craftbukkit release.

    The Complete Guide to Item NBTTags & Attributes!

    So to start we need to create an ItemStack to add NBTTags to, (This should be self explanatory but i'll go through it for the sake of something to do.
    Code:
    ItemStack item = new ItemStack(material.DIAMOND_SWORD, 1); //Creating new item.

    When adding NBTTags to an itemstack you have to edit and set the itemmeta BEFORE you start editing the NBTTags. So we will name our item and lore before hand.
    Code:
    ItemMeta itemmeta = item.getItemMeta();[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]itemmeta.setDisplayName("Cool Sword");[/COLOR][/FONT][/COLOR]
    itemmeta.setLore(Arrays.asList("This is a cool sword." ));[/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]item.setItemMeta(itemmeta);

    Time to start with NMS, first we are going to create an NMS copy of the itemstack we just created.
    Code:
    net.minecraft.server.v1_9_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);

    In this line above we crate an NMS ItemStack named nmsStack which is a copy of the itemstack we created earlier. Now that we have a NMS Itemstack we can start add and editing the Tags & Attributes of the NMS Copy! Next we need to grab the NMS items compound. And, An NMS itemstacks compound can be NULL so we need to check for it.
    Code:
    NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();

    Ok, the last thing we need to add before we can start adding attributes and tags is the list of tags.
    Code:
    NBTTagList modifiers = new NBTTagList();

    We will be adding all attributes to this list so we can apply them to the NMS stack.
    Finally we can now create an attribute to add to the itemstack. I will show you how to edit the DAMAGE attribute, but it is the same process for Speed, AttackSpeed and Health.
    First we will create the damage compound.
    Code:
    NBTTagCompound damage = new NBTTagCompound();

    And now we can start editing it.
    Code:
    damage.set("AttributeName", new NBTTagString("generic.attackDamage"));[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]damage.set("Name", new NBTTagString("generic.attackDamage"));[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]damage.set("Amount", new NBTTagInt(20 /*This is the Amount of the attribute aka how much damage the item will have*\));[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]damage.set("Operation", new NBTTagInt(0));[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]damage.set("UUIDLeast", new NBTTagInt(894654));[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]damage.set("UUIDMost", new NBTTagInt(2872));

    This is the newest edition in 1.9 Not adding this next line will cause it to apply to all slots.
    Code:
    damage.set("Slot", new NBTTagString("mainhand"));

    Example: [​IMG]
    With this line. It will only apply to main hand like so: [​IMG]
    This is a list of all the slots:
    slots (open)
    mainhand
    slots (open)
    slots (open)
    slots (open)
    slots (open)

    offhand
    feet
    legs
    chest
    head

    Next we add our damage compound to our NBTList.
    Code:
    modifiers.add(damage);

    We then add our NBTList to out compound we created earlier.
    Code:
    compound.set("AttributeModifiers", modifiers);

    We can then set out NMS items compound to our newly edited compound.
    Code:
    nmsStack.setTag(compound);

    And last but not least we create a bukkit copy of our NMS copy so we can give it to players as a normal itemstack.
    Code:
    item = CraftItemStack.asBukkitCopy(nmsStack);

    And now the item should have 20 Damage while in MainHand.
    You can also add unbreakable like this:
    Code:
    compound.set("Unbreakable", new NBTTagByte((byte) 1));
    Before setting the NmsTag as our compound.



    Attributes:
    Attributes are cool in the way they are weird. Currently from my experience and information the working NBT tags on ItemStacks is
    generic attributes (open)
    generic.attackDamage
    generic attributes (open)
    generic attributes (open)
    generic attributes (open)
    generic attributes (open)

    generic.attackSpeed
    generic.maxHealth
    generic.movementSpeed
    generic.armor
    generic.luck


    1. maxHealth: Changes the players max health while equipped in specified slots.
    2. movementSpeed:
      Default Speeds (open)
      [​IMG]
    3. armor: When equipped on specified slot adds armor points to player.
    4. luck:
    5. attackSpeed: affects the attackspeed of the user while swinging the item as long as it is in the specified slot.
    NBTTags added in 1.11:
    Exactly how we edited the unbreakable tag earlier we can edit the new Tags added in 1.11.
    The new tags added in 1.11 are;
    • LocName:
    • CustomPotionColor:
    • ColorMap:
    An example is presented above but for the sake of something to do here is another:
    Code:
            NBTTagCompound speed = new NBTTagCompound();[/COLOR][/FONT][/COLOR][/FONT]
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)][FONT=Georgia][COLOR=rgb(20, 20, 20)]        speed.set("AttributeName", new NBTTagString("generic.attackSpeed"));
            speed.set("Name", new NBTTagString("generic.attackSpeed"));
            speed.set("Amount", new NBTTagDouble(0.01));
            speed.set("Operation", new NBTTagInt(0));
            speed.set("UUIDLeast", new NBTTagInt(894654));
            speed.set("UUIDMost", new NBTTagInt(2872));
            speed.set("Slot", new NBTTagString("mainhand"));

    Like above you can see that not much is different than the damage attribute. UUID's do not differ between attributes. The only noticeable differences is the AttributeName, Name and Amount. Since damage is saved as an int, but attackSpeed is saved as a double we just change NBTTagInt to NBTTagDouble, and changed the attribute names. IF you where to add this new compound to the modifiers it would apply to the item.

    Hope I helped you enjoy this tutorial! FIN

    Todo:
    - Small demonstration on adding NBTTags and checking them for "hidden" data.

    Useful Links:
    http://minecraft.gamepedia.com/1.11
    http://minecraft.gamepedia.com/Tutorials/Command_NBT_tags
    http://www.minecraftforum.net/forums/minecraft-discussion/redstone-discussion-and/command-blocks/2488148-1-9-nbt-changes-and-additions#AllItems
    http://minecraft.gamepedia.com/Attribute
     
    Last edited: Dec 1, 2016
  2. Offline

    Redrield

    It looks like there's some BB code that got caught inside your code tags, it's a bit distracting :)
     
  3. @87pen
    You can change the new 1.9 Attack/Swing speed of items using this right?
     
  4. You need to edit the generic.attackSpeed attribute. If you set it to 1024, you basically get back the old spamming system.
     
  5. Fixed the bb code, and an example of changing the attackSpeed of an item is now provided.
    @Redrield @Evonoucono
     
  6. Offline

    AztroCreationz

    Wow, this is amazing! The tutorial is really clear and very neat! But, I have to ask, how would I go about getting values?
    In my case, I want to get the amount of speed I added.
     
  7. get<VariableType>(String name) (replace variable type witht the type of variable you use, e.g Int, Short, Float, etc.)
     
  8. Offline

    AztroCreationz

    Yeah, I figured that much, but where should I get it from? The NBTTagList?

    I am a total derp, the code doesn't work at all for me! xD
    Does anybody know what's wrong?
    Code:
            net.minecraft.server.v1_9_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
          
            NBTTagCompound nbt = nmsStack.hasTag() ? nmsStack.getTag() : new NBTTagCompound();
          
            NBTTagList nbtTags = new NBTTagList();
            NBTTagCompound speed = new NBTTagCompound();
          
            speed.set("AttributeName", new NBTTagString("generic.attackSpeed"));
            speed.set("Name", new NBTTagString("generic.attackSpeed"));
            speed.set("Amount", new NBTTagDouble(attackSpeed/10));
            speed.set("Operation", new NBTTagInt(0));
            speed.set("UUIDLeast", new NBTTagInt(894654));
            speed.set("UUIDMost", new NBTTagInt(2872));
            speed.set("Slot", new NBTTagString("mainhand"));
          
            nbtTags.add(speed);
            nbt.set("AttributeModifiers", nbtTags);
            nmsStack.setTag(nbt);
          
            item = CraftItemStack.asBukkitCopy(nmsStack);
     
    Last edited: Mar 20, 2016
  9. Can't be "Speed" as you are adding a list of attributes you need to add it to the "AttributeModifiers"
    @AztroCreationz
     
  10. Offline

    AztroCreationz

    Yeah, that was just a trace from me playing around a bit, but it didn't work with "AttributeModifiers" either
     
  11. @AztroCreationz After giving the item to a player it didn't have the Data?
     
  12. Offline

    AztroCreationz

    Nope, it doesn't say anything about attack speed anywhere.

    EDIT: Nevermind, I am just an absolute fool xD I had 2 "item" variables, since I was using a constructor to set data. Apparently I didn't set the value for this.item. Man, I should go sleep...
     
    Last edited: Mar 20, 2016
  13. @AztroCreationz Sorry for the late response. I had to run around, What is your attackSpeed variable? An int divided by an int is still an int.
     
  14. Offline

    AztroCreationz

    Yes, I know that, but as you can see in my previous comment I already got it fixed. I really am a fool sometimes -.-
     
  15. Offline

    Valconeye

    Would this replace the default attributes tied to the weapon in the first place?

    For instance the default attack damage or attack speed of a diamond sword.
     
  16. Only on the edited item, nothing else would change.
     
  17. Offline

    Valconeye

    Ah, what i meant to say is: If i make a new item, would i need to add the default attributes to it like default attack damage/speed?

    Example: i am trying to give a diamond sword +%damage and +%attackSpeed.

    When i make an item with these attributes, it seems to loose any default attack speed, and it recharges instantly.

    Here is some sample code:

    Code:
    // -- Attribute NMS stuff..
                    net.minecraft.server.v1_9_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(newItem);
                    NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                   
                    NBTTagList modifiers = new NBTTagList();
                   
                    // -- attack damage
                    NBTTagCompound damage = new NBTTagCompound();
                    damage.set("AttributeName", new NBTTagString("generic.attackDamage"));
                    damage.set("Name", new NBTTagString("generic.attackDamage"));
                    damage.set("Amount", new NBTTagInt(0.1));
                    damage.set("Operation", new NBTTagInt(1));
                    damage.set("UUIDLeast", new NBTTagInt(894654));
                    damage.set("UUIDMost", new NBTTagInt(2872));
                    damage.set("Slot", new NBTTagString("mainhand"));
                   
                    // -- attack speed
                    NBTTagCompound speed = new NBTTagCompound();
                    speed.set("AttributeName", new NBTTagString("generic.attackSpeed"));
                    speed.set("Name", new NBTTagString("generic.attackSpeed"));
                    speed.set("Amount", new NBTTagDouble(0.1));
                    speed.set("Operation", new NBTTagInt(1));
                    speed.set("UUIDLeast", new NBTTagInt(894654));
                    speed.set("UUIDMost", new NBTTagInt(2872));
                    speed.set("Slot", new NBTTagString("mainhand"));
    
    
                    // -- apply modifiers
                    modifiers.add(damage);
                    modifiers.add(speed);
                    compound.set("AttributeModifiers", modifiers);
                   
                    nmsStack.setTag(compound);
                    player.getInventory().addItem(CraftItemStack.asBukkitCopy(nmsStack));
     
  18. Offline

    Valconeye

    Anyone notice the same effect?

    Even if you simple add +1 damage to a diamond sword, the default generic.attackSpeed is lost, and no longer functions the same as a normal diamond sword.

    Even with a give command the attack speed is lost.
    Code:
    /give FanaticNinja diamond_sword 1 0 {AttributeModifiers:[{AttributeName:"generic.attackDamage",Name:"generic.attackDamage",Amount:0.1,Operation:1,UUIDMost:29342,UUIDLeast:48071}]}
    I haven't figured it out yet.

    EDIT

    nvm.. Figured it out. Just add a -speed value to it.

    -2.45 seems pretty close to the original sword attack speed.

    Code:
    /give FanaticNinja diamond_sword 1 0 {AttributeModifiers:[{Slot:"mainhand",AttributeName:"generic.attackSpeed",Name:"generic.attackSpeed",Amount:-2.45,Operation:0,UUIDMost:21204,UUIDLeast:647473},{Slot:"mainhand",AttributeName:"generic.attackDamage",Name:"generic.attackDamage",Amount:0.1,Operation:1,UUIDMost:71468,UUIDLeast:637524}]}
     
    Last edited: Apr 26, 2016
  19. Offline

    Mr. Sandwich

    Code:
                              ItemStack chainBoots = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
                                 net.minecraft.server.v1_9_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(chainBoots);
                                 NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                                 NBTTagList modifiers = new NBTTagList();
                                   NBTTagCompound luck = new NBTTagCompound();
                                   luck.set("AttributeName", new NBTTagString("generic.luck"));
                                   luck.set("Name", new NBTTagString("generic.luck"));
                                   luck.set("Amount", new NBTTagInt(1));
                                   luck.set("Operation", new NBTTagInt(0));
                                   luck.set("UUIDLeast", new NBTTagInt(894654));
                                   luck.set("UUIDMost", new NBTTagInt(2872));
                                  modifiers.add(luck);
                                  compound.set("AttributeModifiers", modifiers);
                                  nmsStack.setTag(compound);
                                  chainBoots = CraftItemStack.asBukkitCopy(nmsStack);
    
    can someone help me and tell me why this isn't working? (I am sorry if this is a noob question)
    The console said [16:13:17 WARN]: Unable to create attribute: Modifier name cannot be empty

    Edit: NVM I just put it in the wrong place.. oops
     
    Last edited: May 13, 2016
  20. Offline

    iwitrag

    Hello,

    thank you very much for this tutorial.
    I have tried to create method to set swords damage.
    I have several questions:

    Why are original Damage values in gray, but modified in blue?
    Why when I modify damage value - the attack speed will dissapear?
    Why modified damage does not change when enchanted by sharpness?


    Some images for comparison:

    Original sword without enchant

    [​IMG]

    Original sword with enchant (note that attack damage has changed)

    [​IMG]

    Modified sword without enchant (note that attack damage is in blue and attack speed is gone)

    [​IMG]

    Modified sword with enchant (note that attack damage did not change at all)

    [​IMG]


    Code:
    abstract public class NMSedit {
    
        public static ItemStack NMSsetDamage(ItemStack bukkitItem, int dmg, String slot) {
            /* --- SLOTS ---
            mainhand  offhand  feet  legs  chest  head */
            net.minecraft.server.v1_10_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(bukkitItem);
            NBTTagCompound originalTag = (nmsItem.hasTag()) ? nmsItem.getTag() : new NBTTagCompound();
            NBTTagList container = new NBTTagList();
    
            NBTTagCompound modifiedTag = new NBTTagCompound();
            modifiedTag.set("AttributeName", new NBTTagString("generic.attackDamage"));
            modifiedTag.set("Name", new NBTTagString("generic.attackDamage"));
            modifiedTag.set("Amount", new NBTTagInt(dmg));
            modifiedTag.set("Operation", new NBTTagInt(0));
            modifiedTag.set("UUIDLeast", new NBTTagInt(894654));
            modifiedTag.set("UUIDMost", new NBTTagInt(2872));
            if (slot.length() > 0)
                modifiedTag.set("Slot", new NBTTagString(slot));
    
            container.add(modifiedTag);
            originalTag.set("AttributeModifiers", container);
            nmsItem.setTag(originalTag);
            return CraftItemStack.asBukkitCopy(nmsItem);
        }
    
    }
    
    
     
    Last edited: Jun 30, 2016
  21. Offline

    ARH720

    Don't work for me :(
     
  22. Youre better off editing the players attributes. Makes it NMSless and reversable.
    Because the modified are the bonus, not base values.
    Thats how minecraft works
    Sharpness affects base value
     
Thread Status:
Not open for further replies.

Share This Page