Recoloring Leather armor in 1.4.6

Discussion in 'Plugin Development' started by orange451, Dec 23, 2012.

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

    orange451

    About a month ago I found a bit of code that allowed me to change the color of leather armor. Since 1.4.6 the code errored (so I fixed it [or so I thought]).

    Code:
        public ItemStack setColor(ItemStack item, int color) {
            CraftItemStack craftStack = null;
            net.minecraft.server.v1_4_6.ItemStack itemStack = null;
           
            if(item instanceof CraftItemStack) {
                craftStack = (CraftItemStack)item;
                itemStack = CraftItemStack.asNMSCopy(craftStack);
            } else if(item instanceof ItemStack) {
                craftStack = CraftItemStack.asCraftCopy(item);
                itemStack = CraftItemStack.asNMSCopy(craftStack);
            }
     
            NBTTagCompound tag = itemStack.tag;
            if(tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                itemStack.tag = tag;
            }
     
            tag = itemStack.tag.getCompound("display");
            tag.setInt("color", color);
            itemStack.tag.setCompound("display", tag);
            return craftStack;
        }
    There is the code I am currently using (modified of the original I found so it doesn't error).
    What am I doing wrong when trying to set the color of leather armor?

    ex:
    hexColor = 16711935;
    ItemStack c0 =
    this.setColor(new ItemStack(Material.LEATHER_HELMET, 1), hexColor);
     
  2. Offline

    drampelt

    I'm pretty sure LeatherArmorMeta is the proper way to do this now. If you check to make sure the ItemStack is actually one of the leather armour pieces, you should be able to do:
    Code:
    LeatherArmorMeta lam = (LeatherArmorMeta) SomeLeatherItemStack.getItemMeta();
    lam.setColor(Color.RED);
    SomeLeatherItemStack.setItemMeta(lam);
     
  3. Offline

    caseif

    An API was added into Bukkit for NBT tags in 1.4.5-R0.3. Use this code instead:
    Code:java
    1. public void setColor(ItemStack is, Color color){
    2. LeatherArmorMeta lam = (LeatherArmorMeta)is.getItemMeta();
    3. lam.setColor(color);
    4. is.setItemMeta(lam);
    5. }
     
    orange451 likes this.
  4. Offline

    orange451

    You're a god :)
     
  5. Offline

    caseif

    Glad to help. :D
     
Thread Status:
Not open for further replies.

Share This Page