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