Add new IFluidHandler for capabilities. (#2942)

Old fluid system is deprecated and slated for removal in the next cleanup cycle.
This commit is contained in:
mezz authored and LexManos committed 2016-06-03 18:26:41 -07:00
1 parent 13ed510fcf
commit 92914f82ae
39 files changed
+2612 -298

No files matched your search

@@ -1,26 +1,34 @@
package net.minecraftforge.fluids;
import javax.annotation.Nullable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.capability.FluidTankPropertiesWrapper;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;
/**
* Reference implementation of {@link IFluidTank}. Use/extend this or implement your own.
*/
public class FluidTank implements IFluidTank
public class FluidTank implements IFluidTank, IFluidHandler
{
@Nullable
protected FluidStack fluid;
protected int capacity;
protected TileEntity tile;
protected boolean canFill = true;
protected boolean canDrain = true;
protected IFluidTankProperties[] tankProperties;
public FluidTank(int capacity)
{
this(null, capacity);
}
public FluidTank(FluidStack stack, int capacity)
public FluidTank(@Nullable FluidStack fluidStack, int capacity)
{
this.fluid = stack;
this.fluid = fluidStack;
this.capacity = capacity;
}
@@ -56,23 +64,19 @@ public class FluidTank implements IFluidTank
return nbt;
}
public void setFluid(FluidStack fluid)
{
this.fluid = fluid;
}
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
/* IFluidTank */
@Override
@Nullable
public FluidStack getFluid()
{
return fluid;
}
public void setFluid(@Nullable FluidStack fluid)
{
this.fluid = fluid;
}
@Override
public int getFluidAmount()
{
@@ -89,16 +93,36 @@ public class FluidTank implements IFluidTank
return capacity;
}
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
public void setTileEntity(TileEntity tile)
{
this.tile = tile;
}
@Override
public FluidTankInfo getInfo()
{
return new FluidTankInfo(this);
}
@Override
public IFluidTankProperties[] getTankProperties()
{
if (this.tankProperties == null)
{
this.tankProperties = new IFluidTankProperties[] { new FluidTankPropertiesWrapper(this) };
}
return this.tankProperties;
}
@Override
public int fill(FluidStack resource, boolean doFill)
{
if (resource == null)
if (resource == null || resource.amount <= 0 || !canFillFluidType(resource))
{
return 0;
}
@@ -122,6 +146,8 @@ public class FluidTank implements IFluidTank
{
fluid = new FluidStack(resource, Math.min(capacity, resource.amount));
onContentsChanged();
if (tile != null)
{
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, tile.getWorld(), tile.getPos(), this, fluid.amount));
@@ -145,6 +171,8 @@ public class FluidTank implements IFluidTank
fluid.amount = capacity;
}
onContentsChanged();
if (tile != null)
{
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, tile.getWorld(), tile.getPos(), this, filled));
@@ -152,10 +180,20 @@ public class FluidTank implements IFluidTank
return filled;
}
@Override
public FluidStack drain(FluidStack resource, boolean doDrain)
{
if (resource == null || !resource.isFluidEqual(getFluid()))
{
return null;
}
return drain(resource.amount, doDrain);
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain)
{
if (fluid == null)
if (fluid == null || maxDrain <= 0 || !canDrainFluidType(fluid))
{
return null;
}
@@ -175,6 +213,8 @@ public class FluidTank implements IFluidTank
fluid = null;
}
onContentsChanged();
if (tile != null)
{
FluidEvent.fireEvent(new FluidEvent.FluidDrainingEvent(fluid, tile.getWorld(), tile.getPos(), this, drained));
@@ -182,4 +222,75 @@ public class FluidTank implements IFluidTank
}
return stack;
}
/**
* Whether this tank can be filled with {@link IFluidHandler}
*
* @see IFluidTankProperties#canFill()
*/
public boolean canFill()
{
return canFill;
}
/**
* Whether this tank can be drained with {@link IFluidHandler}
*
* @see IFluidTankProperties#canDrain()
*/
public boolean canDrain()
{
return canDrain;
}
/**
* Set whether this tank can be filled with {@link IFluidHandler}
*
* @see IFluidTankProperties#canFill()
*/
public void setCanFill(boolean canFill)
{
this.canFill = canFill;
}
/**
* Set whether this tank can be drained with {@link IFluidHandler}
*
* @see IFluidTankProperties#canDrain()
*/
public void setCanDrain(boolean canDrain)
{
this.canDrain = canDrain;
}
/**
* Returns true if the tank can be filled with this type of fluid.
* Used as a filter for fluid types.
* Does not consider the current contents or capacity of the tank,
* only whether it could ever fill with this type of fluid.
*
* @see IFluidTankProperties#canFillFluidType(FluidStack)
*/
public boolean canFillFluidType(FluidStack fluid)
{
return canFill();
}
/**
* Returns true if the tank can drain out this type of fluid.
* Used as a filter for fluid types.
* Does not consider the current contents or capacity of the tank,
* only whether it could ever drain out this type of fluid.
*
* @see IFluidTankProperties#canDrainFluidType(FluidStack)
*/
public boolean canDrainFluidType(FluidStack fluid)
{
return canDrain();
}
protected void onContentsChanged()
{
}
}