package com.carpentersblocks.util;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.logging.log4j.Level;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier;

/**
 * Stores attribute and unique identifier for validation purposes.
 */
public class Attribute {
    
    private static final String TAG_UNIQUE_ID = "cbUniqueId";
    private ItemStack _itemStack;
    private String _uniqueId;
    private boolean _error;
    
    public Attribute(ItemStack itemStack) {
        if (itemStack != null && itemStack.getItem() != null) {
            this._itemStack = itemStack;
            _uniqueId = GameRegistry.findUniqueIdentifierFor(itemStack.getItem()).toString();
        }
    }
    
    public ItemStack getItemStack() {
        return _itemStack;
    }

    public String getUniqueId() {
        return _uniqueId;
    }
    
    /**
     * Write the stack fields to a NBT object. Return the new NBT object.
     */
    public NBTTagCompound writeToNBT(NBTTagCompound nbt)
    {
        _itemStack.writeToNBT(nbt);
        nbt.setString(TAG_UNIQUE_ID, _uniqueId);
        return nbt;
    }
    
    public static Attribute loadAttributeFromNBT(NBTTagCompound nbt)
    {
        ItemStack itemStack = ItemStack.loadItemStackFromNBT(nbt);
        if (itemStack == null) {
            UniqueIdentifier uniqueId = new UniqueIdentifier(nbt.getString(TAG_UNIQUE_ID));
            itemStack = GameRegistry.findItemStack(uniqueId.modId, uniqueId.name, 1);
            if (itemStack != null) {
                int dmg = nbt.getShort("Damage");
                itemStack.setItemDamage(dmg);
                ModLogger.log(Level.WARN, "Invalid Id for attribute '" + uniqueId.toString() + "' corrected.");
            } else {
                ModLogger.log(Level.WARN, "Block attribute '" + uniqueId.toString() + "' was unable to be recovered. Was a mod removed?");
            }
        }        
        return new Attribute(itemStack);
    }
    
}
@@ -1371,32 +1371,130 @@
 tack == null) %7B%0A
+            String uuid = nbt.getString(TAG_UNIQUE_ID);%0A            if (uuid.contains(%22:%22)) %7B%0A    
             Uniq
@@ -1542,39 +1542,19 @@
 ier(
-nbt.getString(TAG_UNIQUE_ID));%0A
+uuid);%0A    
     
@@ -1639,32 +1639,36 @@
 1);%0A            
+    
 if (itemStack !=
@@ -1668,32 +1668,36 @@
 tack != null) %7B%0A
+    
                 
@@ -1738,32 +1738,36 @@
                 
+  
+  
 itemStack.setIte
@@ -1780,16 +1780,20 @@
 e(dmg);%0A
+    
         
@@ -1908,16 +1908,20 @@
         
+   
+ 
 %7D else %7B
@@ -1917,24 +1917,28 @@
    %7D else %7B%0A
+    
             
@@ -2060,24 +2060,152 @@
 removed?%22);%0A
+                %7D%0A            %7D else %7B%0A                ModLogger.log(Level.WARN, %22Unable to resolve attribute '%22 + uuid + %22'%22);%0A
             

Correct OOB attribute issue. Fixes #393.