Solved How do I add attributes to a ItemStack?

Discussion in 'Plugin Development' started by SeamusMcFreak, Jan 24, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    SeamusMcFreak

    Hello, Im trying to make a Mythical set of armor that players can buy. Its supposed to have speed on it, but I don't know how to use attributes for ItemStacks. Can someone help me? Code:
    Code:
    ItemStack item4 = new ItemStack(Material.DIAMOND_BOOTS);
                    ItemMeta im4 = item4.getItemMeta();
                    im4.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 4, true);
                    im4.addEnchant(Enchantment.PROTECTION_FIRE, 4, true);
                    im4.addEnchant(Enchantment.PROTECTION_FALL, 4, true);
                    im4.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, 4, true);
                    im4.addEnchant(Enchantment.PROTECTION_PROJECTILE, 4, true);
                    im4.addEnchant(Enchantment.THORNS, 3, true);
                    im4.spigot().setUnbreakable(true);
                    im4.setDisplayName(ChatColor.DARK_PURPLE + ChatColor.BOLD.toString() + "Mythical Boots");
                    im4.setLore(mlore1);
                    item4.setItemMeta(im4);
     
  2. Offline

    ShowbizLocket61

    @SeamusMcFreak
    There isn't any Bukkit enchantment for Speed, other Depth Strider for water. I suggest you listen to when he puts on the armor, and give him a speed effect. When he takes off the boots, take off the effect.
     
  3. Offline

    SeamusMcFreak

    I don't mean speed I mean the Attribute generic.movementSpeed how would I add that to my ItemStack?
     
  4. Offline

    Zombie_Striker

  5. Offline

    SeamusMcFreak

    No the attribute generic.movementSpeed or generic.maxHealth
     
  6. Offline

    teej107

    I think those apply to to entities. So you will still have to listen to when the player puts on and takes off the armor.
    Setting the attributes requires NMS.
    Code:
    AttributeInstance attributes = ((CraftLivingEntity) entity).getHandle().getAttributeInstance(GenericAttributes.ATTACK_DAMAGE);
    attributes.setValue(attack);
     
  7. Offline

    87pen

    @teej107 @SeamusMcFreak NBTtags! http://minecraft.gamepedia.com/NBT_format
    http://minecraft.gamepedia.com/Tutorials/Command_NBT_tags
    http://minecraft.gamepedia.com/Attribute
    This requires NMS.
    Code:
            //Normal ItemMeta stuff.
            ItemStack sythe = new ItemStack(Material.STONE_HOE, 1);
            ItemMeta im = sythe.getItemMeta();
            im.setDisplayName(ChatColor.GOLD + "Stone Sythe");
            sythe.setItemMeta(im);
           
            net.minecraft.server.v1_8_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(sythe);
            NBTTagCompound compound = nmsStack.getTag();
            if (compound == null) {
               compound = new NBTTagCompound();
                nmsStack.setTag(compound);
                compound = nmsStack.getTag();
            }
            NBTTagList modifiers = new NBTTagList();
           
            //Attributes are set like this:
            NBTTagCompound damage = new NBTTagCompound();
            damage.set("AttributeName", new NBTTagString("generic.attackDamage"));
            damage.set("Name", new NBTTagString("generic.attackDamage"));
            damage.set("Amount", new NBTTagInt(6 /*Item damage*/));
            damage.set("Operation", new NBTTagInt(0));
            damage.set("UUIDLeast", new NBTTagInt(894654));
            damage.set("UUIDMost", new NBTTagInt(2872));
            modifiers.add(damage);
           
            //What you want.
            NBTTagCompound speed = new NBTTagCompound();
            speed.set("AttributeName", new NBTTagString("generic.movementSpeed"));
            speed.set("Name", new NBTTagString("generic.movementSpeed"));
            speed.set("Amount", new NBTTagDouble(0.7));
            speed.set("Operation", new NBTTagInt(0));
            speed.set("UUIDLeast", new NBTTagInt(894654));
            speed.set("UUIDMost", new NBTTagInt(2872));
            modifiers.add(speed);
           
            //Tags like Unbreakable can be set like this:
            compound.set("Unbreakable", new NBTTagByte((byte) 1));
           
            compound.set("AttributeModifiers", modifiers);
            nmsStack.setTag(compound);
            sythe = CraftItemStack.asBukkitCopy(nmsStack);
     
    teej107 likes this.
  8. Offline

    SeamusMcFreak

    @87pen, it works but it removes all the ItemMeta (DisplayName, Lore, enchantments, etc.)
    Code:
    ItemStack item3 = new ItemStack(Material.DIAMOND_LEGGINGS);
                    ItemMeta im3 = item3.getItemMeta();
                    net.minecraft.server.v1_8_R3.ItemStack ia3nmsStack = CraftItemStack.asNMSCopy(item3);
                    NBTTagCompound ia3compound = ia3nmsStack.getTag();
                    if (ia3compound == null) {
                        ia3compound = new NBTTagCompound();
                         ia3nmsStack.setTag(ia3compound);
                         ia3compound = ia3nmsStack.getTag();
                     }
                    NBTTagCompound ia3speed = new NBTTagCompound();
                    NBTTagList ia3 = new NBTTagList();
                    ia3speed.set("AttributeName", new NBTTagString("generic.movementSpeed"));
                    ia3speed.set("Name", new NBTTagString("generic.movementSpeed"));
                    ia3speed.set("Amount", new NBTTagDouble(0.1));
                    ia3speed.set("Operation", new NBTTagInt(0));
                    ia3speed.set("UUIDLeast", new NBTTagInt(894654));
                    ia3speed.set("UUIDMost", new NBTTagInt(2872));
                    ia3.add(ia3speed);
                    im3.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 4, true);
                    im3.addEnchant(Enchantment.PROTECTION_FIRE, 4, true);
                    im3.addEnchant(Enchantment.PROTECTION_EXPLOSIONS, 4, true);
                    im3.addEnchant(Enchantment.PROTECTION_PROJECTILE, 4, true);
                    im3.addEnchant(Enchantment.THORNS, 3, true);
                    im3.spigot().setUnbreakable(true);
                    im3.setDisplayName(ChatColor.DARK_PURPLE + ChatColor.BOLD.toString() + "Mythical Leggings");
                    ia3compound.set("AttributeModifiers", ia3speed);
                    im3.setLore(mlore1);
                    item3.setItemMeta(im3);
                    ia3compound.set("AttributeModifiers", ia3);
                    ia3nmsStack.setTag(ia3compound);
                    item3 = CraftItemStack.asBukkitCopy(ia3nmsStack);
     
  9. Offline

    teej107

    Comment out some lines to see which one is causing the problem.
     
  10. Offline

    SeamusMcFreak

    @teej107, Ive tried that. If I remove any of them the attribute will fail.
     
  11. Offline

    teej107

  12. Offline

    SeamusMcFreak

    @teej107, it works! Thank you both for helping me! This will help me get further on with my plugin! :)
    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page