001package net.minecraftforge.liquids;
002
003import static cpw.mods.fml.relauncher.Side.CLIENT;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.block.Block;
006import net.minecraft.client.renderer.texture.TextureManager;
007import net.minecraft.item.Item;
008import net.minecraft.item.ItemStack;
009import net.minecraft.nbt.NBTTagCompound;
010import net.minecraft.util.Icon;
011
012/**
013 * ItemStack substitute for liquids
014 * @author SirSengir
015 */
016public class LiquidStack
017{
018    public int itemID;
019    public int amount;
020    public int itemMeta;
021
022    private LiquidStack(){}
023
024    public LiquidStack(int itemID,  int amount) { this(itemID,        amount, 0); }
025    public LiquidStack(Item item,   int amount) { this(item.itemID,   amount, 0); }
026    public LiquidStack(Block block, int amount) { this(block.blockID, amount, 0); }
027
028    public LiquidStack(int itemID, int amount, int itemDamage)
029    {
030        this.itemID = itemID;
031        this.amount = amount;
032        this.itemMeta = itemDamage;
033    }
034
035    public NBTTagCompound writeToNBT(NBTTagCompound nbt)
036    {
037        nbt.setShort("Id", (short)itemID);
038        nbt.setInteger("Amount", amount);
039        nbt.setShort("Meta", (short)itemMeta);
040        return nbt;
041    }
042
043    public void readFromNBT(NBTTagCompound nbt)
044    {
045        itemID = nbt.getShort("Id");
046        amount = nbt.getInteger("Amount");
047        itemMeta = nbt.getShort("Meta");
048    }
049
050    /**
051     * @return A copy of this LiquidStack
052     */
053    public LiquidStack copy()
054    {
055        return new LiquidStack(itemID, amount, itemMeta);
056    }
057
058    /**
059     * @param other
060     * @return true if this LiquidStack contains the same liquid as the one passed in.
061     */
062    public boolean isLiquidEqual(LiquidStack other)
063    {
064        return other != null && itemID == other.itemID && itemMeta == other.itemMeta;
065    }
066
067    /**
068     * @param other
069     * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount).
070     */
071    public boolean containsLiquid(LiquidStack other)
072    {
073        return isLiquidEqual(other) && amount >= other.amount;
074    }
075
076    /**
077     * @param other ItemStack containing liquids.
078     * @return true if this LiquidStack contains the same liquid as the one passed in.
079     */
080    public boolean isLiquidEqual(ItemStack other)
081    {
082        if (other == null)
083        {
084            return false;
085        }
086
087        if (itemID == other.itemID && itemMeta == other.getItemDamage())
088        {
089            return true;
090        }
091
092        return isLiquidEqual(LiquidContainerRegistry.getLiquidForFilledItem(other));
093    }
094
095    /**
096     * @return ItemStack representation of this LiquidStack
097     */
098    public ItemStack asItemStack()
099    {
100        return new ItemStack(itemID, 1, itemMeta);
101    }
102
103    /**
104     * Reads a liquid stack from the passed nbttagcompound and returns it.
105     *
106     * @param nbt
107     * @return the liquid stack
108     */
109    public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbt)
110    {
111        LiquidStack liquidstack = new LiquidStack();
112        liquidstack.readFromNBT(nbt);
113        return liquidstack.itemID == 0 ? null : liquidstack;
114    }
115
116    @SideOnly(CLIENT)
117    private Icon renderingIcon;
118
119    @SideOnly(CLIENT)
120    public Icon getRenderingIcon()
121    {
122        return renderingIcon;
123    }
124
125    @SideOnly(CLIENT)
126    public void setRenderingIcon(Icon icon)
127    {
128        this.renderingIcon = icon;
129    }
130}