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