Cleaned up release valve tile entity

This commit is contained in:
DarkGuardsman 2013-12-04 17:43:22 -05:00
parent 388ab18e62
commit 826acc8c0d

View file

@ -1,18 +1,11 @@
package dark.fluid.common.machines; package dark.fluid.common.machines;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler; import net.minecraftforge.fluids.IFluidHandler;
import universalelectricity.core.vector.Vector3;
import dark.api.ColorCode;
import dark.api.ColorCode.IColorCoded;
import dark.api.IToolReadOut; import dark.api.IToolReadOut;
import dark.api.fluid.INetworkPipe; import dark.api.fluid.INetworkPipe;
import dark.api.tilenetwork.ITileConnector; import dark.api.tilenetwork.ITileConnector;
@ -22,51 +15,32 @@ import dark.fluid.common.prefab.TileEntityFluidDevice;
public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITileConnector, IToolReadOut public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITileConnector, IToolReadOut
{ {
public boolean[] allowed = new boolean[ColorCode.values().length - 1];
public TileEntity[] connected = new TileEntity[6]; public TileEntity[] connected = new TileEntity[6];
private List<INetworkPipe> output = new ArrayList<INetworkPipe>();
private IFluidHandler[] input = new IFluidHandler[6];
@Override @Override
public void updateEntity() public void updateEntity()
{ {
super.updateEntity(); super.updateEntity();
connected = ConnectionHelper.getSurroundingTileEntities(this); if (this.ticks % 10 == 0)
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
if (connected[dir.ordinal()] instanceof IFluidHandler) this.refresh();
if (!this.worldObj.isRemote && !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{ {
if (connected[dir.ordinal()] instanceof IColorCoded && !this.canConnect(((IColorCoded) connected[dir.ordinal()]).getColor())) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
connected[dir.ordinal()] = null; if (connected[dir.ordinal()] instanceof IFluidHandler && !(connected[dir.ordinal()] instanceof INetworkPipe))
}
}
else
{
connected[dir.ordinal()] = null;
}
}
if (!this.worldObj.isRemote && this.ticks % 10 == 0 && !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{
this.validateNBuildList();
// start the draining process
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
IFluidHandler drainedTank = input[dir.ordinal()];
if (drainedTank != null)
{
FluidStack stack = drainedTank.drain(dir.getOpposite(), FluidContainerRegistry.BUCKET_VOLUME, false);
if (stack != null && stack.amount > 0)
{ {
INetworkPipe inputPipe = this.findValidPipe(stack); IFluidHandler drainedTank = (IFluidHandler) connected[dir.ordinal()];
if (inputPipe != null) FluidStack stack = drainedTank.drain(dir.getOpposite(), FluidContainerRegistry.BUCKET_VOLUME, false);
if (stack != null && stack.amount > 0)
{ {
int ammountFilled = ((NetworkPipes) inputPipe.getTileNetwork()).addFluidToNetwork((TileEntity) drainedTank, stack, true); INetworkPipe inputPipe = this.findValidPipe(stack);
drainedTank.drain(ForgeDirection.UNKNOWN, ammountFilled, true); if (inputPipe != null)
{
int ammountFilled = ((NetworkPipes) inputPipe.getTileNetwork()).addFluidToNetwork((TileEntity) drainedTank, stack, true);
drainedTank.drain(dir.getOpposite(), ammountFilled, true);
}
} }
} }
} }
@ -78,83 +52,37 @@ public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITi
public INetworkPipe findValidPipe(FluidStack stack) public INetworkPipe findValidPipe(FluidStack stack)
{ {
// find normal color selective pipe first // find normal color selective pipe first
for (INetworkPipe pipe : output) for (int i = 0; i < connected.length; i++)
{ {
if (pipe.fill(ForgeDirection.UNKNOWN, stack, false) > 0) TileEntity tile = connected[i];
if (tile instanceof INetworkPipe && ((INetworkPipe) tile).fill(ForgeDirection.getOrientation(i), stack, false) > 0)
{ {
return pipe; return (INetworkPipe) tile;
} }
} }
return null; return null;
} }
/** sees if it can connect to a pipe of some color */
public boolean canConnect(ColorCode cc)
{
if (this.isRestricted())
{
for (int i = 0; i < this.allowed.length; i++)
{
if (i == cc.ordinal())
{
return allowed[i];
}
}
}
return true;
}
/** if any of allowed list is true
*
* @return true */
public boolean isRestricted()
{
for (int i = 0; i < this.allowed.length; i++)
{
if (allowed[i])
{
return true;
}
}
return false;
}
/** Collects info about the surrounding 6 tiles and orders them into drain-able(ITankContainer) /** Collects info about the surrounding 6 tiles and orders them into drain-able(ITankContainer)
* and fill-able(TileEntityPipes) instances */ * and fill-able(TileEntityPipes) instances */
public void validateNBuildList() public void refresh()
{ {
// cleanup // cleanup
this.connected = ConnectionHelper.getSurroundingTileEntities(worldObj, xCoord, yCoord, zCoord); this.connected = ConnectionHelper.getSurroundingTileEntities(this);
this.input = new IFluidHandler[6];
this.output.clear();
// read surroundings // read surroundings
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{ {
TileEntity tileEntity = connected[dir.ordinal()]; TileEntity tileEntity = connected[dir.ordinal()];
if (tileEntity instanceof INetworkPipe) if (tileEntity instanceof ITileConnector)
{ {
INetworkPipe pipe = (INetworkPipe) tileEntity;
if (this.canTileConnect(Connection.FLUIDS, dir.getOpposite())) if (this.canTileConnect(Connection.FLUIDS, dir.getOpposite()))
{ {
this.output.add(pipe); this.connected[dir.ordinal()] = tileEntity;
}
else
{
this.connected[dir.ordinal()] = null;
} }
} }
else if (tileEntity instanceof IFluidHandler) else if (tileEntity instanceof IFluidHandler)
{ {
IFluidHandler tank = (IFluidHandler) tileEntity; this.connected[dir.ordinal()] = tileEntity;
if (tank != null && tank.drain(dir.getOpposite(), FluidContainerRegistry.BUCKET_VOLUME, false) != null)
{
this.input[dir.ordinal()] = (IFluidHandler) tileEntity;
}
}
else
{
connected[dir.ordinal()] = null;
} }
} }
} }
@ -162,8 +90,7 @@ public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITi
@Override @Override
public boolean canTileConnect(Connection type, ForgeDirection dir) public boolean canTileConnect(Connection type, ForgeDirection dir)
{ {
TileEntity entity = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj); return type == Connection.FLUIDS;
return entity != null && entity instanceof IFluidHandler && entity instanceof IColorCoded && this.canConnect(((IColorCoded) entity).getColor());
} }
@Override @Override
@ -171,43 +98,14 @@ public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITi
{ {
// TODO maybe debug on # of connected units of input/output // TODO maybe debug on # of connected units of input/output
String output = ""; String output = "";
if (this.isRestricted())
{
output += "Output: Restricted and";
}
else
{
output += " Output: UnRestricted and";
}
if (!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) if (!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{ {
output += " Open "; output += "Open";
} }
else else
{ {
output += " Closed "; output += "Closed";
} }
return output; return output;
} }
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
for (int i = 0; i < this.allowed.length; i++)
{
allowed[i] = nbt.getBoolean("allowed" + i);
}
}
/** Writes a tile entity to NBT. */
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
for (int i = 0; i < this.allowed.length; i++)
{
nbt.setBoolean("allowed" + i, allowed[i]);
}
}
} }