    001    package net.minecraftforge.liquids;
    002
    003    import net.minecraft.tileentity.TileEntity;
    004
    005    /**
    006     * Reference implementation of ILiquidTank. Use this or implement your own.
    007     */
    008    public class LiquidTank implements ILiquidTank {
    009        private LiquidStack liquid;
    010        private int capacity;
    011        private int tankPressure;
    012        private TileEntity tile;
    013
    014        public LiquidTank(int capacity) {
    015            this(null, capacity);
    016        }
    017
    018        public LiquidTank(int liquidId, int quantity, int capacity) {
    019            this(new LiquidStack(liquidId, quantity), capacity);
    020        }
    021
    022        public LiquidTank(int liquidId, int quantity, int capacity, TileEntity tile) {
    023            this(liquidId, quantity, capacity);
    024            this.tile = tile;
    025        }
    026
    027        public LiquidTank(LiquidStack liquid, int capacity) {
    028            this.liquid = liquid;
    029            this.capacity = capacity;
    030        }
    031
    032        public LiquidTank(LiquidStack liquid, int capacity, TileEntity tile)
    033        {
    034            this(liquid, capacity);
    035            this.tile = tile;
    036        }
    037        @Override
    038        public LiquidStack getLiquid() {
    039            return this.liquid;
    040        }
    041
    042        @Override
    043        public void setLiquid(LiquidStack liquid) {
    044            this.liquid = liquid;
    045        }
    046
    047        @Override
    048        public void setCapacity(int capacity) {
    049            this.capacity = capacity;
    050        }
    051
    052        @Override
    053        public int getCapacity() {
    054            return this.capacity;
    055        }
    056
    057        @Override
    058        public int fill(LiquidStack resource, boolean doFill) {
    059            if(resource == null || resource.itemID <= 0)
    060                return 0;
    061
    062            if(liquid == null || liquid.itemID <= 0) {
    063                if(resource.amount <= capacity) {
    064                    if(doFill)
    065                        this.liquid = resource.copy();
    066                    return resource.amount;
    067                } else {
    068                    if(doFill) {
    069                        this.liquid = resource.copy();
    070                        this.liquid.amount = capacity;
    071                        if (tile!=null)
    072                            LiquidEvent.fireEvent(new LiquidEvent.LiquidFillingEvent(liquid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this));
    073                    }
    074                    return capacity;
    075                }
    076            }
    077
    078            if(!liquid.isLiquidEqual(resource))
    079                return 0;
    080
    081            int space = capacity - liquid.amount;
    082            if(resource.amount <= space) {
    083                if(doFill)
    084                    this.liquid.amount += resource.amount;
    085                return resource.amount;
    086            } else {
    087
    088                if(doFill)
    089                    this.liquid.amount = capacity;
    090                return space;
    091            }
    092
    093        }
    094        @Override
    095        public LiquidStack drain(int maxDrain, boolean doDrain) {
    096            if(liquid == null || liquid.itemID <= 0)
    097                return null;
    098            if(liquid.amount <= 0)
    099                return null;
    100
    101            int used = maxDrain;
    102            if(liquid.amount < used)
    103                used = liquid.amount;
    104
    105            if(doDrain) {
    106                liquid.amount -= used;
    107            }
    108
    109            LiquidStack drained = new LiquidStack(liquid.itemID, used, liquid.itemMeta);
    110
    111            // Reset liquid if emptied
    112            if(liquid.amount <= 0)
    113                liquid = null;
    114
    115            if (doDrain && tile!=null)
    116                LiquidEvent.fireEvent(new LiquidEvent.LiquidDrainingEvent(drained, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this));
    117
    118            return drained;
    119        }
    120
    121        @Override
    122        public int getTankPressure() {
    123            return tankPressure;
    124        }
    125
    126        public void setTankPressure(int pressure)
    127        {
    128            this.tankPressure = pressure;
    129        }
    130
    131    }