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