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