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;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
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.fluid.INetworkPipe;
import dark.api.tilenetwork.ITileConnector;
@ -22,51 +15,32 @@ import dark.fluid.common.prefab.TileEntityFluidDevice;
public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITileConnector, IToolReadOut
{
public boolean[] allowed = new boolean[ColorCode.values().length - 1];
public TileEntity[] connected = new TileEntity[6];
private List<INetworkPipe> output = new ArrayList<INetworkPipe>();
private IFluidHandler[] input = new IFluidHandler[6];
@Override
public void updateEntity()
{
super.updateEntity();
connected = ConnectionHelper.getSurroundingTileEntities(this);
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
if (this.ticks % 10 == 0)
{
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;
}
}
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)
if (connected[dir.ordinal()] instanceof IFluidHandler && !(connected[dir.ordinal()] instanceof INetworkPipe))
{
INetworkPipe inputPipe = this.findValidPipe(stack);
if (inputPipe != null)
IFluidHandler drainedTank = (IFluidHandler) connected[dir.ordinal()];
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);
drainedTank.drain(ForgeDirection.UNKNOWN, ammountFilled, true);
INetworkPipe inputPipe = this.findValidPipe(stack);
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)
{
// 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;
}
/** 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)
* and fill-able(TileEntityPipes) instances */
public void validateNBuildList()
public void refresh()
{
// cleanup
this.connected = ConnectionHelper.getSurroundingTileEntities(worldObj, xCoord, yCoord, zCoord);
this.input = new IFluidHandler[6];
this.output.clear();
this.connected = ConnectionHelper.getSurroundingTileEntities(this);
// read surroundings
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tileEntity = connected[dir.ordinal()];
if (tileEntity instanceof INetworkPipe)
if (tileEntity instanceof ITileConnector)
{
INetworkPipe pipe = (INetworkPipe) tileEntity;
if (this.canTileConnect(Connection.FLUIDS, dir.getOpposite()))
{
this.output.add(pipe);
}
else
{
this.connected[dir.ordinal()] = null;
this.connected[dir.ordinal()] = tileEntity;
}
}
else if (tileEntity instanceof IFluidHandler)
{
IFluidHandler tank = (IFluidHandler) tileEntity;
if (tank != null && tank.drain(dir.getOpposite(), FluidContainerRegistry.BUCKET_VOLUME, false) != null)
{
this.input[dir.ordinal()] = (IFluidHandler) tileEntity;
}
}
else
{
connected[dir.ordinal()] = null;
this.connected[dir.ordinal()] = tileEntity;
}
}
}
@ -162,8 +90,7 @@ public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITi
@Override
public boolean canTileConnect(Connection type, ForgeDirection dir)
{
TileEntity entity = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj);
return entity != null && entity instanceof IFluidHandler && entity instanceof IColorCoded && this.canConnect(((IColorCoded) entity).getColor());
return type == Connection.FLUIDS;
}
@Override
@ -171,43 +98,14 @@ public class TileEntityReleaseValve extends TileEntityFluidDevice implements ITi
{
// TODO maybe debug on # of connected units of input/output
String output = "";
if (this.isRestricted())
{
output += "Output: Restricted and";
}
else
{
output += " Output: UnRestricted and";
}
if (!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{
output += " Open ";
output += "Open";
}
else
{
output += " Closed ";
output += "Closed";
}
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]);
}
}
}