package net.minecraftforge.liquids;

import static cpw.mods.fml.relauncher.Side.CLIENT;

import com.google.common.base.Objects;

import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFluid;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Icon;

/**
 * ItemStack substitute for liquids
 * @author SirSengir
 */
public class LiquidStack
{
    public int itemID;
    public int amount;
    public int itemMeta;

    private LiquidStack(){}

    public LiquidStack(int itemID,  int amount) { this(itemID,        amount, 0); }
    public LiquidStack(Item item,   int amount) { this(item.itemID,   amount, 0); }
    public LiquidStack(Block block, int amount) { this(block.blockID, amount, 0); }

    public LiquidStack(int itemID, int amount, int itemDamage)
    {
        this.itemID = itemID;
        this.amount = amount;
        this.itemMeta = itemDamage;
    }

    public NBTTagCompound writeToNBT(NBTTagCompound nbt)
    {
        nbt.setShort("Id", (short)itemID);
        nbt.setInteger("Amount", amount);
        nbt.setShort("Meta", (short)itemMeta);
        nbt.setString("LiquidName", LiquidDictionary.findLiquidName(this));
        return nbt;
    }

    public void readFromNBT(NBTTagCompound nbt)
    {
        String liquidName = nbt.getString("LiquidName");
        if (liquidName != null)
        {
            LiquidStack liquid = LiquidDictionary.getCanonicalLiquid(liquidName);
            itemID = liquid.itemID;
            itemMeta = liquid.itemMeta;
        }
        else
        {
            itemID = nbt.getShort("Id");
            itemMeta = nbt.getShort("Meta");
        }
        amount = nbt.getInteger("Amount");
    }

    /**
     * @return A copy of this LiquidStack
     */
    public LiquidStack copy()
    {
        return new LiquidStack(itemID, amount, itemMeta);
    }

    /**
     * @param other
     * @return true if this LiquidStack contains the same liquid as the one passed in.
     */
    public boolean isLiquidEqual(LiquidStack other)
    {
        return other != null && itemID == other.itemID && itemMeta == other.itemMeta;
    }

    /**
     * @param other
     * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount).
     */
    public boolean containsLiquid(LiquidStack other)
    {
        return isLiquidEqual(other) && amount >= other.amount;
    }

    /**
     * @param other ItemStack containing liquids.
     * @return true if this LiquidStack contains the same liquid as the one passed in.
     */
    public boolean isLiquidEqual(ItemStack other)
    {
        if (other == null)
        {
            return false;
        }

        if (itemID == other.itemID && itemMeta == other.getItemDamage())
        {
            return true;
        }

        return isLiquidEqual(LiquidContainerRegistry.getLiquidForFilledItem(other));
    }

    /**
     * @return ItemStack representation of this LiquidStack
     */
    public ItemStack asItemStack()
    {
        return new ItemStack(itemID, 1, itemMeta);
    }

    /**
     * Reads a liquid stack from the passed nbttagcompound and returns it.
     *
     * @param nbt
     * @return the liquid stack
     */
    public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbt)
    {
        LiquidStack liquidstack = new LiquidStack();
        liquidstack.readFromNBT(nbt);
        return liquidstack.itemID == 0 ? null : liquidstack;
    }

    @SideOnly(CLIENT)
    private Icon renderingIcon;

    @SideOnly(CLIENT)
    public Icon getRenderingIcon()
    {
        if (itemID == Block.waterStill.blockID)
        {
            return BlockFluid.func_94424_b("water");
        }
        else if (itemID == Block.lavaStill.blockID)
        {
            return BlockFluid.func_94424_b("lava");
        }
        return renderingIcon;
    }

    @SideOnly(CLIENT)
    public void setRenderingIcon(Icon icon)
    {
        this.renderingIcon = icon;
    }

    @Override
    public final int hashCode()
    {
        return 31 * itemID + itemMeta;
    }

    @Override
    public final boolean equals(Object ob)
    {
        return ob instanceof LiquidStack && Objects.equal(((LiquidStack)ob).itemID, itemID) && Objects.equal(((LiquidStack)ob).itemMeta, itemMeta);
    }
}

@@ -1463,24 +1463,102 @@
 quidName%22);%0A
+        itemID = nbt.getShort(%22Id%22);%0A        itemMeta = nbt.getShort(%22Meta%22);%0A
         if (
@@ -1686,31 +1686,30 @@
    i
-temID = liquid.itemID;%0A
+f(liquid != null) %7B%0A  
     
@@ -1716,24 +1716,24 @@
         
+  
 item
-Meta
+ID
  = liqui
@@ -1734,28 +1734,26 @@
  liquid.item
-Meta
+ID
 ;%0A        %7D%0A
@@ -1746,34 +1746,32 @@
 ID;%0A        
-%7D%0A
         
 else%0A       
@@ -1762,108 +1762,49 @@
     
-else%0A        %7B%0A            itemID = nbt.getShort(%22Id%22);%0A            itemMeta = nbt.getShort(%22Meta%22);
+itemMeta = liquid.itemMeta;%0A            %7D
 %0A   

fix bug #489 from #429