2013-05-23 05:01:19 +00:00
|
|
|
|
|
|
|
package net.minecraftforge.fluids;
|
|
|
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference implementation of {@link IFluidTank}. Use/extend this or implement your own.
|
|
|
|
*
|
|
|
|
* @author King Lemming, cpw (LiquidTank)
|
|
|
|
*
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public class FluidTank implements IFluidTank
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
protected FluidStack fluid;
|
|
|
|
protected int capacity;
|
|
|
|
protected TileEntity tile;
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidTank(int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this(null, capacity);
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidTank(FluidStack stack, int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.fluid = stack;
|
|
|
|
this.capacity = capacity;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidTank(Fluid fluid, int amount, int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this(new FluidStack(fluid, amount), capacity);
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidTank readFromNBT(NBTTagCompound nbt)
|
|
|
|
{
|
|
|
|
if (!nbt.hasKey("Empty"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
FluidStack fluid = FluidStack.loadFluidStackFromNBT(nbt);
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (fluid != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
setFluid(fluid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
|
|
|
|
{
|
|
|
|
if (fluid != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid.writeToNBT(nbt);
|
2013-07-17 04:40:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
nbt.setString("Empty", "");
|
|
|
|
}
|
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public void setFluid(FluidStack fluid)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.fluid = fluid;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public void setCapacity(int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IFluidTank */
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack getFluid()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return fluid;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public int getFluidAmount()
|
|
|
|
{
|
|
|
|
if (fluid == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fluid.amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public int getCapacity()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidTankInfo getInfo()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return new FluidTankInfo(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public int fill(FluidStack resource, boolean doFill)
|
|
|
|
{
|
|
|
|
if (resource == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!doFill)
|
|
|
|
{
|
|
|
|
if (fluid == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return Math.min(capacity, resource.amount);
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!fluid.isFluidEqual(resource))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
return Math.min(capacity - fluid.amount, resource.amount);
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (fluid == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid = new FluidStack(resource, Math.min(capacity, resource.amount));
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (tile != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this));
|
|
|
|
}
|
|
|
|
return fluid.amount;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!fluid.isFluidEqual(resource))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int filled = capacity - fluid.amount;
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (resource.amount < filled)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid.amount += resource.amount;
|
|
|
|
filled = resource.amount;
|
2013-07-17 04:40:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid.amount = capacity;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (tile != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this));
|
|
|
|
}
|
|
|
|
return filled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack drain(int maxDrain, boolean doDrain)
|
|
|
|
{
|
|
|
|
if (fluid == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
int drained = maxDrain;
|
|
|
|
if (fluid.amount < drained)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
drained = fluid.amount;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
FluidStack stack = new FluidStack(fluid, drained);
|
|
|
|
if (doDrain)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid.amount -= drained;
|
2013-07-17 04:40:49 +00:00
|
|
|
if (fluid.amount <= 0)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluid = null;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (tile != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
FluidEvent.fireEvent(new FluidEvent.FluidDrainingEvent(fluid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|