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 * Things of note: they are equal if their items are equal. Amount does NOT matter for java equals() testing
019 * <br/>
020 * The canonical liquidstack is probably the only one that has a lot of the rendering data on it. Use {@link #canonical()}
021 * to get it.
022 *
023 * @author SirSengir
024 */
025public class LiquidStack
026{
027    public final int itemID;
028    public int amount;
029    public final int itemMeta;
030
031    public LiquidStack(int itemID,  int amount) { this(itemID,        amount, 0); }
032    public LiquidStack(Item item,   int amount) { this(item.itemID,   amount, 0); }
033    public LiquidStack(Block block, int amount) { this(block.blockID, amount, 0); }
034
035    public LiquidStack(int itemID, int amount, int itemDamage)
036    {
037        this.itemID = itemID;
038        this.amount = amount;
039        this.itemMeta = itemDamage;
040    }
041
042    public NBTTagCompound writeToNBT(NBTTagCompound nbt)
043    {
044        nbt.setInteger("Amount", amount);
045        nbt.setShort("Id", (short)itemID);
046        nbt.setShort("Meta", (short)itemMeta);
047        nbt.setString("LiquidName", LiquidDictionary.findLiquidName(this));
048        return nbt;
049    }
050
051
052    /**
053     * NO-OP now. Use {@link #loadLiquidStackFromNBT(NBTTagCompound)} to get a new instance
054     *
055     * @param nbt
056     */
057    @Deprecated
058    public void readFromNBT(NBTTagCompound nbt)
059    {
060    }
061
062    /**
063     * @return A copy of this LiquidStack
064     */
065    public LiquidStack copy()
066    {
067        return new LiquidStack(itemID, amount, itemMeta);
068    }
069
070    /**
071     * @param other
072     * @return true if this LiquidStack contains the same liquid as the one passed in.
073     */
074    public boolean isLiquidEqual(LiquidStack other)
075    {
076        return other != null && itemID == other.itemID && itemMeta == other.itemMeta;
077    }
078
079    /**
080     * @param other
081     * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount).
082     */
083    public boolean containsLiquid(LiquidStack other)
084    {
085        return isLiquidEqual(other) && amount >= other.amount;
086    }
087
088    /**
089     * @param other ItemStack containing liquids.
090     * @return true if this LiquidStack contains the same liquid as the one passed in.
091     */
092    public boolean isLiquidEqual(ItemStack other)
093    {
094        if (other == null)
095        {
096            return false;
097        }
098
099        if (itemID == other.itemID && itemMeta == other.getItemDamage())
100        {
101            return true;
102        }
103
104        return isLiquidEqual(LiquidContainerRegistry.getLiquidForFilledItem(other));
105    }
106
107    /**
108     * @return ItemStack representation of this LiquidStack
109     */
110    public ItemStack asItemStack()
111    {
112        return new ItemStack(itemID, 1, itemMeta);
113    }
114
115    /**
116     * Reads a liquid stack from the passed nbttagcompound and returns it.
117     *
118     * @param nbt
119     * @return the liquid stack
120     */
121    public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbt)
122    {
123        if (nbt == null)
124        {
125            return null;
126        }
127        String liquidName = nbt.getString("LiquidName");
128        int itemID = nbt.getShort("Id");
129        int itemMeta = nbt.getShort("Meta");
130        LiquidStack liquid = LiquidDictionary.getCanonicalLiquid(liquidName);
131        if(liquid != null) {
132            itemID = liquid.itemID;
133            itemMeta = liquid.itemMeta;
134        }
135        int amount = nbt.getInteger("Amount");
136        LiquidStack liquidstack = new LiquidStack(itemID, amount, itemMeta);
137        return liquidstack.itemID == 0 ? null : liquidstack;
138    }
139
140    @SideOnly(CLIENT)
141    private String textureSheet = "/terrain.png";
142
143    /**
144     * Return the textureSheet used for this liquid stack's texture Icon
145     * Defaults to '/terrain.png'
146     *
147     * See {@link #getRenderingIcon()} for the actual icon
148     *
149     * @return The texture sheet
150     */
151    public String getTextureSheet()
152    {
153        return textureSheet;
154    }
155
156    /**
157     * Set the texture sheet for this icon (usually /terrain.png or /gui/items.png)
158     *
159     * See also the {@link #setRenderingIcon(Icon)} for the icon itself
160     *
161     * @param textureSheet
162     * @return the liquid stack
163     */
164    public LiquidStack setTextureSheet(String textureSheet)
165    {
166        this.textureSheet = textureSheet;
167        return this;
168    }
169    @SideOnly(CLIENT)
170    private Icon renderingIcon;
171
172    /**
173     * Get the rendering icon for this liquid stack, for presentation in the world or in GUIs.
174     * Defaults to handling water and lava, and returns the set rendering icon otherwise.
175     *
176     * See {@link #getTextureSheet()} to get the texture sheet this icon is associated with
177     *
178     * @return The icon for rendering this liquid
179     */
180    @SideOnly(CLIENT)
181    public Icon getRenderingIcon()
182    {
183        if (itemID == Block.waterStill.blockID)
184        {
185            return BlockFluid.func_94424_b("water");
186        }
187        else if (itemID == Block.lavaStill.blockID)
188        {
189            return BlockFluid.func_94424_b("lava");
190        }
191        return renderingIcon;
192    }
193
194    /**
195     * Set the icon for rendering this liquid
196     * It should be refreshed whenever textures are refreshed.
197     *
198     * See also {@link #setTextureSheet(String)} for setting the sheet this icon is associated with
199     *
200     * @param icon The icon to render
201     * @return The liquid stack
202     */
203    @SideOnly(CLIENT)
204    public LiquidStack setRenderingIcon(Icon icon)
205    {
206        this.renderingIcon = icon;
207        return this;
208    }
209
210    @Override
211    public final int hashCode()
212    {
213        return 31 * itemMeta + itemID;
214    }
215
216    @Override
217    public final boolean equals(Object ob)
218    {
219        if (ob instanceof LiquidStack)
220        {
221            LiquidStack ls = (LiquidStack)ob;
222            return ls.itemID == itemID && ls.itemMeta == itemMeta;
223        }
224        return false;
225    }
226
227
228    /**
229     * Get the canonical version of this liquid stack (will contain things like icons and texturesheets)
230     * @return The canonical liquidstack
231     */
232    public LiquidStack canonical()
233    {
234        return LiquidDictionary.getCanonicalLiquid(this);
235    }
236}