From b80481890315a1e59dc0c63c124b1971791bfe4d Mon Sep 17 00:00:00 2001 From: BluSunrize Date: Sat, 11 Jan 2014 13:11:59 +0100 Subject: [PATCH] Update CastingChannelLogic.java Un-derping the Casting Channel --- .../blocks/logic/CastingChannelLogic.java | 1083 ++++++++--------- 1 file changed, 483 insertions(+), 600 deletions(-) diff --git a/src/main/java/tconstruct/blocks/logic/CastingChannelLogic.java b/src/main/java/tconstruct/blocks/logic/CastingChannelLogic.java index 2ac85d10..8a14c1f2 100644 --- a/src/main/java/tconstruct/blocks/logic/CastingChannelLogic.java +++ b/src/main/java/tconstruct/blocks/logic/CastingChannelLogic.java @@ -1,21 +1,24 @@ package tconstruct.blocks.logic; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Set; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.INetworkManager; +import net.minecraft.network.packet.NetHandler; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidEvent; import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; import net.minecraftforge.fluids.FluidTankInfo; import net.minecraftforge.fluids.IFluidHandler; -import net.minecraftforge.fluids.IFluidTank; import tconstruct.TConstruct; -import tconstruct.library.util.IActiveLogic; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -23,649 +26,529 @@ import cpw.mods.fml.relauncher.SideOnly; * @author BluSunrize */ -public class CastingChannelLogic extends TileEntity implements IFluidTank, IFluidHandler, IActiveLogic +public class CastingChannelLogic extends TileEntity implements IFluidHandler { - public final static int fillMax = TConstruct.ingotLiquidValue*5; - public final static int outputMax = TConstruct.ingotLiquidValue; - public FluidStack liquid; - public ForgeDirection lastProvider; - private int ticks = 0; - private int ticksLPReset = 0; - public int recentlyFilledDelay; - boolean pullingLiquids; + public final static int fillMax = TConstruct.ingotLiquidValue; + public final static int outputMax = TConstruct.ingotLiquidValue; + FluidTank internalTank = new FluidTank(fillMax); + HashMap subTanks = new HashMap(); + public ForgeDirection lastProvider; + public ArrayList validOutputs = new ArrayList(); + public int ticks = 0; + public int recentlyFilledDelay; + + /* UNUSED + public CastingChannelMode mode = CastingChannelMode.SINGLEDRAIN; + */ + + public CastingChannelLogic() + { + lastProvider = ForgeDirection.UNKNOWN; + validOutputs.add(ForgeDirection.DOWN); + validOutputs.add(ForgeDirection.NORTH); + validOutputs.add(ForgeDirection.SOUTH); + validOutputs.add(ForgeDirection.EAST); + validOutputs.add(ForgeDirection.WEST); - public CastingChannelLogic() - { - recentlyFilledDelay = 0; - lastProvider = ForgeDirection.UNKNOWN; - } + subTanks.put(ForgeDirection.NORTH, new FluidTank(TConstruct.ingotLiquidValue/4)); + subTanks.put(ForgeDirection.SOUTH, new FluidTank(TConstruct.ingotLiquidValue/4)); + subTanks.put(ForgeDirection.WEST, new FluidTank(TConstruct.ingotLiquidValue/4)); + subTanks.put(ForgeDirection.EAST, new FluidTank(TConstruct.ingotLiquidValue/4)); + } - @Override - public void updateEntity () - { - if (this.worldObj.isRemote) - return; - ticks++; - ticksLPReset++; - if (!worldObj.isRemote) - { - if (this.pullingLiquids) - pullLiquids(); - } - //this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); - if (ticks == 20) - { - if (recentlyFilledDelay != 0) - recentlyFilledDelay--; - if (recentlyFilledDelay == 0) - moveLiquidToTanks(); - ticks = 0; - } - if (ticksLPReset == 60) - { - this.lastProvider = ForgeDirection.UNKNOWN; - ticksLPReset = 0; - } - } + @Override + public void updateEntity () + { +// if (this.worldObj.isRemote) +// return; + ticks++; - void pullLiquids () - { - if (pullLiquid(xCoord + 1, yCoord, zCoord, ForgeDirection.EAST) || pullLiquid(xCoord - 1, yCoord, zCoord, ForgeDirection.WEST) || pullLiquid(xCoord, yCoord, zCoord + 1, ForgeDirection.NORTH) - || pullLiquid(xCoord, yCoord, zCoord - 1, ForgeDirection.SOUTH)) - { + boolean flagActiveFaucet = false; - } - else - { - pullingLiquids = false; - } - } + TileEntity possibleFaucet = worldObj.getBlockTileEntity(xCoord, yCoord+1, zCoord); + if(possibleFaucet!=null && possibleFaucet instanceof FaucetLogic) + flagActiveFaucet = ((FaucetLogic)possibleFaucet).active; - boolean pullLiquid (int x, int y, int z, ForgeDirection direction) - { - TileEntity tank = worldObj.getBlockTileEntity(x, y, z); - if (tank instanceof IFluidHandler && !(tank instanceof CastingChannelLogic)) - { - FluidStack templiquid = ((IFluidHandler) tank).drain(direction, 3, false); - if (templiquid != null) - { - if (this.liquid == null) - { - if (templiquid.amount > fillMax) - templiquid.amount = fillMax; - this.liquid = ((IFluidHandler) tank).drain(direction, templiquid.amount, true); - return true; - } - else if (this.liquid.isFluidEqual(templiquid)) - { - if (templiquid.amount + this.liquid.amount > fillMax) - templiquid.amount = fillMax - this.liquid.amount; - liquid.amount += ((IFluidHandler) tank).drain(direction, templiquid.amount, true).amount; - return true; - } - } - } - return false; - } + if(ticks == 6 && !flagActiveFaucet) + this.distributeFluids(); + if (ticks >= 12 && !flagActiveFaucet) + { + if (recentlyFilledDelay != 0) + recentlyFilledDelay--; + if (recentlyFilledDelay == 0 || lastProvider == ForgeDirection.UP) + this.outputFluids(); - private void moveLiquidToChannels () - { - if (this.liquid == null || this.liquid.amount <= 0) - { - return; - } - TileEntity tankXplus = null; - TileEntity tankXminus = null; - TileEntity tankZplus = null; - TileEntity tankZminus = null; - TileEntity tankYminus = null; - HashMap tankMap = this.getOutputs(); - if (tankMap.size() <= 0) - { - //TConstruct.logger.info("Nope, no connections to go to"); - return; - } - boolean yFilled = false; - if (tankMap.containsKey("yMinus")) - { - tankYminus = (TileEntity) tankMap.get("yMinus"); - FluidStack liquidToPush = this.drain(24, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankYminus).fill(ForgeDirection.UP, liquidToPush, true); - if (filled > 0) - { - yFilled = true; - this.drain(filled, true); - } - } - } + ticks = 0; + } + } - if (!yFilled) - { - int divisionSurplus = (this.liquid.amount % tankMap.size()); - int amountXp = (this.outputMax / tankMap.size()); - int amountXm = (this.outputMax / tankMap.size()); - int amountZp = (this.outputMax / tankMap.size()); - int amountZm = (this.outputMax / tankMap.size()); - if (divisionSurplus > 0) - amountXp++; - if (divisionSurplus > 1) - amountXm++; - if (divisionSurplus > 2) - amountZp++; + public void changeOutputs(EntityPlayer player, int side, float hitX, float hitY, float hitZ) + { + /* + * UNUSED + if(player.isSneaking()) + { - if (tankMap.containsKey("xPlus")) - { - tankXplus = (TileEntity) tankMap.get("xPlus"); - FluidStack liquidToPush = this.drain(amountXp, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankXplus).fill(ForgeDirection.WEST, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("xMinus")) - { - tankXminus = (TileEntity) tankMap.get("xMinus"); - FluidStack liquidToPush = this.drain(amountXm, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankXminus).fill(ForgeDirection.EAST, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("zPlus")) - { - tankZplus = (TileEntity) tankMap.get("zPlus"); - FluidStack liquidToPush = this.drain(amountZp, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankZplus).fill(ForgeDirection.NORTH, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("zMinus")) - { - tankZminus = (TileEntity) tankMap.get("zMinus"); - FluidStack liquidToPush = this.drain(amountZm, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankZminus).fill(ForgeDirection.SOUTH, liquidToPush, true); - this.drain(filled, true); - } - } - } - } + if(mode == null || mode == CastingChannelMode.SINGLEDRAIN) + mode = CastingChannelMode.CONTINUEDRAIN; + else + mode = CastingChannelMode.SINGLEDRAIN; - private void moveLiquidToTanks () - { - if (this.liquid == null || this.liquid.amount <= 0) - { - return; - } - TileEntity tankXplus = null; - TileEntity tankXminus = null; - TileEntity tankZplus = null; - TileEntity tankZminus = null; - TileEntity tankYminus = null; - HashMap tankMap = this.getOutputs(); - if (tankMap.size() <= 0) - { - //TConstruct.logger.info("Nope, no connections to go to"); - return; - } - boolean yFilled = false; - if (tankMap.containsKey("yMinus")) - { - tankYminus = (TileEntity) tankMap.get("yMinus"); - FluidStack liquidToPush = this.drain(24, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankYminus).fill(ForgeDirection.UP, liquidToPush, true); - if (filled > 0) - { - yFilled = true; - this.drain(filled, true); - } - } - } + String s = "Casting Channel Mode "+(worldObj.isRemote?"client side: ":"server side: "); + player.addChatMessage(s+mode.toString()); + return; + } + */ + + ForgeDirection toggle = ForgeDirection.UNKNOWN; + if(side == 0 || side == 1) + { + if(hitX>0.3125 && hitX<0.6875) + if(hitZ>0 && hitZ<0.3125) + toggle = ForgeDirection.NORTH; + if(hitX>0.3125 && hitX<0.6875) + if(hitZ>0.6875 && hitZ<1) + toggle = ForgeDirection.SOUTH; + if(hitZ>0.3125 && hitZ<0.6875) + if(hitX>0 && hitX<0.3125) + toggle = ForgeDirection.WEST; + if(hitZ>0.3125 && hitZ<0.6875) + if(hitX>0.6875 && hitX<1) + toggle = ForgeDirection.EAST; - if (!yFilled) - { - int divisionSurplus = (this.liquid.amount % tankMap.size()); - int amountXp = (this.outputMax / tankMap.size()); - int amountXm = (this.outputMax / tankMap.size()); - int amountZp = (this.outputMax / tankMap.size()); - int amountZm = (this.outputMax / tankMap.size()); - if (divisionSurplus > 0) - amountXp++; - if (divisionSurplus > 1) - amountXm++; - if (divisionSurplus > 2) - amountZp++; + if(side == 0) + if(hitX>0.3125 && hitX<0.6875) + if(hitZ>0.3125 && hitZ<0.6875) + toggle = ForgeDirection.DOWN; + } + else + { + if(side==2) + { + if(hitX>0 && hitX<0.3125) + toggle = ForgeDirection.WEST; + if(hitX>0.6875 && hitX<1) + toggle = ForgeDirection.EAST; + } + if(side==3) + { + if(hitX>0 && hitX<0.3125) + toggle = ForgeDirection.WEST; + if(hitX>0.6875 && hitX<1) + toggle = ForgeDirection.EAST; + } + if(side==4) + { + if(hitZ>0 && hitZ<0.3125) + toggle = ForgeDirection.NORTH; + if(hitZ>0.6875 && hitZ<1) + toggle = ForgeDirection.SOUTH; + } + if(side==5) + { + if(hitZ>0 && hitZ<0.3125) + toggle = ForgeDirection.NORTH; + if(hitZ>0.6875 && hitZ<1) + toggle = ForgeDirection.SOUTH; + } + } - if (tankMap.containsKey("xPlus")) - { - tankXplus = (TileEntity) tankMap.get("xPlus"); - FluidStack liquidToPush = this.drain(amountXp, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankXplus).fill(ForgeDirection.WEST, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("xMinus")) - { - tankXminus = (TileEntity) tankMap.get("xMinus"); - FluidStack liquidToPush = this.drain(amountXm, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankXminus).fill(ForgeDirection.EAST, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("zPlus")) - { - tankZplus = (TileEntity) tankMap.get("zPlus"); - FluidStack liquidToPush = this.drain(amountZp, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankZplus).fill(ForgeDirection.NORTH, liquidToPush, true); - this.drain(filled, true); - } - } - if (tankMap.containsKey("zMinus")) - { - tankZminus = (TileEntity) tankMap.get("zMinus"); - FluidStack liquidToPush = this.drain(amountZm, false); - if ((liquidToPush != null) && (liquidToPush.amount > 0)) - { - int filled = ((IFluidHandler) tankZminus).fill(ForgeDirection.SOUTH, liquidToPush, true); - this.drain(filled, true); - } - } - } - } + if(toggle != ForgeDirection.UNKNOWN && toggle != ForgeDirection.UP) + { + TileEntity tile = worldObj.getBlockTileEntity(xCoord+toggle.offsetX,yCoord+toggle.offsetY,zCoord+toggle.offsetZ); + if(tile instanceof IFluidHandler) + if(validOutputs.contains(toggle)) + { + validOutputs.remove(toggle); + if(tile instanceof CastingChannelLogic && toggle != ForgeDirection.DOWN) + ((CastingChannelLogic)tile).validOutputs.remove(toggle.getOpposite()); + } + else + { + validOutputs.add(toggle); + if(tile instanceof CastingChannelLogic && toggle != ForgeDirection.DOWN) + if(! ((CastingChannelLogic)tile).validOutputs.contains(toggle.getOpposite()) ) + ((CastingChannelLogic)tile).validOutputs.add(toggle.getOpposite()); + } - @SideOnly(Side.CLIENT) - public float tankBelow () - { - TileEntity te = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord); - if (te instanceof CastingChannelLogic) - return -0.5f; - if (te instanceof LavaTankLogic) - return -1.0f; - if (te instanceof CastingBasinLogic) - return -0.75f; - if (te instanceof CastingTableLogic) - return -0.125f; - if (te instanceof IFluidHandler) - return 0f; + } + this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord); + } - return 0.5f; - } + private void distributeFluids() + { + boolean printDebugLog = false; + ArrayList debugLog = new ArrayList(); + //Move Fluid to Output Tanks + Set connected = this.getOutputs().keySet(); + if(connected.contains(ForgeDirection.DOWN)) + connected.remove(ForgeDirection.DOWN); + if(connected.contains(lastProvider)) + connected.remove(lastProvider); + int output = Math.min(internalTank.getFluidAmount(), 12); + int connectedAmount = connected.size(); + if(connectedAmount < 1)connectedAmount = 1; + int scaledAmount = output/connectedAmount; + for(ForgeDirection dirOut: connected) + { + if(this.internalTank.getFluid() == null) + break; - public boolean hasChannelConnected (ForgeDirection dir) - { - switch (dir) - { - case DOWN: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord) instanceof CastingChannelLogic); - case NORTH: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1) instanceof CastingChannelLogic); - case SOUTH: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord + 1) instanceof CastingChannelLogic); - case WEST: - return (this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord) instanceof CastingChannelLogic); - case EAST: - return (this.worldObj.getBlockTileEntity(this.xCoord + 1, this.yCoord, this.zCoord) instanceof CastingChannelLogic); - default: - return false; - } - } + debugLog.add("Moving to "+dirOut+"ern SubTank"); + FluidStack tempFS = new FluidStack(internalTank.getFluid().getFluid(), scaledAmount); + int fit = subTanks.get(dirOut).fill(tempFS, false); + debugLog.add("SubTank will accept "+fit+"mb"); + if(fit > 0) + fit = internalTank.drain(fit, true).amount; + debugLog.add("Internal Tank was drained by "+fit+"mb"); + tempFS.amount = fit; + fit = subTanks.get(dirOut).fill(tempFS, true); + debugLog.add("SubTank was filled by "+fit+"mb"); + if(fit > 0) + printDebugLog = true; + debugLog.add("Internal Tank now contains: "+internalTank.getFluidAmount()+"mb"); + debugLog.add("SubTank now contains: "+subTanks.get(dirOut).getFluidAmount()+"mb"); - public boolean hasTankConnected (ForgeDirection dir) - { - switch (dir) - { - case DOWN: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord) instanceof IFluidHandler); - case NORTH: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1) instanceof IFluidHandler); - case SOUTH: - return (this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord + 1) instanceof IFluidHandler); - case WEST: - return (this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord) instanceof IFluidHandler); - case EAST: - return (this.worldObj.getBlockTileEntity(this.xCoord + 1, this.yCoord, this.zCoord) instanceof IFluidHandler); - default: - return false; - } - } + this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord); + } + if(printDebugLog) + { + for(String s: debugLog) + System.out.println(s); + debugLog.clear(); + } + printDebugLog = false; - private HashMap getOutputs () - { - HashMap map = new HashMap(); - TileEntity tankXplus = this.worldObj.getBlockTileEntity(this.xCoord + 1, this.yCoord, this.zCoord); - TileEntity tankXminus = this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord); - TileEntity tankZplus = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord + 1); - TileEntity tankZminus = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1); - TileEntity tankYminus = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord); + //Get Fluid from most recent InputTank + FluidTank inputTank = subTanks.get(lastProvider); + if(inputTank != null && inputTank.getFluid() != null) //Tank can be null if input was received from top + { + debugLog.add("Importing from "+lastProvider+"ern SubTank"); + FluidStack tempFS = new FluidStack(inputTank.getFluid().getFluid(), Math.min(inputTank.getFluidAmount(), 12)); + int fit = internalTank.fill(tempFS, false); + debugLog.add("Internal Tank will accept "+fit+"mb"); + if(fit > 0) + fit = inputTank.drain(fit, true).amount; + debugLog.add("Import Tank was drained by "+fit+"mb"); + tempFS.amount = fit; + fit = internalTank.fill(tempFS, true); + debugLog.add("Internal Tank was filled by "+fit+"mb"); + if(fit > 0) + printDebugLog = true; + debugLog.add("Internal Tank now contains: "+internalTank.getFluidAmount()+"mb"); + debugLog.add("SubTank now contains: "+inputTank.getFluidAmount()+"mb"); - if (this.pullingLiquids) - { - if (this.hasChannelConnected(ForgeDirection.EAST) && (this.lastProvider != ForgeDirection.EAST)) - { - map.put("xPlus", tankXplus); - } - if (this.hasChannelConnected(ForgeDirection.WEST) && (this.lastProvider != ForgeDirection.WEST)) - { - map.put("xMinus", tankXminus); - } - if (this.hasChannelConnected(ForgeDirection.SOUTH) && (this.lastProvider != ForgeDirection.SOUTH)) - { - map.put("zPlus", tankZplus); - } - if (this.hasChannelConnected(ForgeDirection.NORTH) && (this.lastProvider != ForgeDirection.NORTH)) - { - map.put("zMinus", tankZminus); - } - if (this.hasChannelConnected(ForgeDirection.DOWN) && (this.lastProvider != ForgeDirection.DOWN)) - { - map.put("yMinus", tankYminus); - } - } - else - { - if (this.hasTankConnected(ForgeDirection.EAST) && (this.lastProvider != ForgeDirection.EAST)) - { - map.put("xPlus", tankXplus); - } - if (this.hasTankConnected(ForgeDirection.WEST) && (this.lastProvider != ForgeDirection.WEST)) - { - map.put("xMinus", tankXminus); - } - if (this.hasTankConnected(ForgeDirection.SOUTH) && (this.lastProvider != ForgeDirection.SOUTH)) - { - map.put("zPlus", tankZplus); - } - if (this.hasTankConnected(ForgeDirection.NORTH) && (this.lastProvider != ForgeDirection.NORTH)) - { - map.put("zMinus", tankZminus); - } - if (this.hasTankConnected(ForgeDirection.DOWN) && (this.lastProvider != ForgeDirection.DOWN)) - { - map.put("yMinus", tankYminus); - } - } + this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord); + } + if(printDebugLog) + { + for(String s: debugLog) + System.out.println(s); + debugLog.clear(); + } + } - return map; - } + private void outputFluids() + { + boolean printDebugLog = false; + ArrayList debugLog = new ArrayList(); - public int fill (FluidStack stack, boolean doFill) - { - if (stack == null) - { - return 0; - } - if (this.liquid == null) - { - FluidStack transfered = stack.copy(); + HashMap connected = this.getOutputs(); + if(connected.containsKey(lastProvider)) + connected.remove(lastProvider); + if(connected.containsKey(ForgeDirection.DOWN) && this.internalTank.getFluid() != null) // Prioritizes FluidHandlers below + { + int output = Math.min(internalTank.getFluid().amount, 12); + FluidStack tempFS = new FluidStack(internalTank.getFluid().getFluid(), output); + int fittingBelow = ((IFluidHandler)connected.get(ForgeDirection.DOWN)).fill(ForgeDirection.UP, tempFS, false); + if(fittingBelow > 0) + fittingBelow = this.drain(ForgeDirection.DOWN, fittingBelow, true).amount; + tempFS.amount = fittingBelow; + fittingBelow = ((IFluidHandler)connected.get(ForgeDirection.DOWN)).fill(ForgeDirection.UP, tempFS, true); - if (transfered.amount > this.fillMax) - { - transfered.amount = this.fillMax; - } + connected.remove(ForgeDirection.DOWN); + } + if(connected.size() != 0) + { + for(ForgeDirection dir:connected.keySet()) //Iterates to connected FluidHandlers + { + if(subTanks.get(dir)!=null && subTanks.get(dir).getFluid() != null) + { + debugLog.add("Exporting from "+dir+"ern SubTank"); + FluidStack tempFS = new FluidStack(subTanks.get(dir).getFluid().getFluid(), Math.min(subTanks.get(dir).getFluidAmount(),12)); + int fit = ((IFluidHandler)connected.get(dir)).fill(dir.getOpposite(), tempFS, false); + debugLog.add("New Channel will accept "+fit+"mb"); + try{ + if(fit > 0) + fit = this.drain(dir, fit, true).amount; + debugLog.add("OldSubTank was drained by "+fit+"mb"); + tempFS.amount = fit; + fit = ((IFluidHandler)connected.get(dir)).fill(dir.getOpposite(), tempFS, true); + debugLog.add("New Channel was filled by "+fit+"mb"); - if (doFill) - { - this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); - this.liquid = transfered; - } - recentlyFilledDelay = 2; - if (this.lastProvider == ForgeDirection.UP) - recentlyFilledDelay = 3; - return transfered.amount; - } + //debugLog.add("Internal Tank now contains: "+internalTank.getFluidAmount()+"mb"); + debugLog.add("OldSubTank now contains: "+drain(dir, 100000, false).amount+"mb"); + if(fit > 0) + printDebugLog = true; - if (stack.isFluidEqual(this.liquid)) - { - if (stack.amount + this.liquid.amount >= this.fillMax) - { - int spaceInTank = this.fillMax - this.liquid.amount; - if ((doFill) && (spaceInTank > 0)) - { - this.liquid.amount = this.fillMax; - this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); - } - return spaceInTank; - } + }catch(Exception e){ + } + } +// for(String s: debugLog) +// System.out.println(s); +// debugLog.clear(); + } + } + } - if (doFill) - { - this.liquid.amount += stack.amount; - this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); - } - return stack.amount; - } + @SideOnly(Side.CLIENT) + public float tankBelow () + { + TileEntity te = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord); + if (te instanceof CastingChannelLogic) + return -0.5f; + if (te instanceof LavaTankLogic) + return -1.0f; + if (te instanceof CastingBasinLogic) + return -0.75f; + if (te instanceof CastingTableLogic) + return -0.125f; + if (te instanceof IFluidHandler) + return 0f; - return 0; - } + return 0.5f; + } - @Override - public int fill (ForgeDirection from, FluidStack resource, boolean doFill) - { - this.lastProvider = from; - ticksLPReset = 0; - int filled = fill(resource, doFill); - if (from == ForgeDirection.UP) - moveLiquidToTanks(); - return filled; - } + public HashMap getOutputs () + { + HashMap map = new HashMap(); + for(ForgeDirection fd: this.validOutputs) + { + int tX = this.xCoord + fd.offsetX; + int tY = this.yCoord + fd.offsetY; + int tZ = this.zCoord + fd.offsetZ; + TileEntity tile = this.worldObj.getBlockTileEntity(tX,tY,tZ); + + if(tile != null && tile instanceof IFluidHandler) + map.put(fd, tile); + } + return map; + } - public FluidStack drain (int maxDrain, boolean doDrain) - { - if ((this.liquid == null)) - { - return null; - } - if (this.liquid.amount <= 0) - { - return null; - } - int toDrain = maxDrain; - if (this.liquid.amount < toDrain) - { - toDrain = this.liquid.amount; - } - if (doDrain) - { - this.liquid.amount -= toDrain; - } + @Override + public int fill(ForgeDirection from, FluidStack resource, boolean doFill) + { + if(doFill) + { + this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord); + this.lastProvider = from; + if(this.internalTank.getFluid() == null) + this.recentlyFilledDelay = 2; + } - FluidStack drained = new FluidStack(liquid.fluidID, toDrain); + if(from == ForgeDirection.UP) + { + return this.internalTank.fill(resource, doFill); + } + else if(from == ForgeDirection.NORTH || from == ForgeDirection.SOUTH || from == ForgeDirection.WEST || from == ForgeDirection.EAST) + { + return this.subTanks.get(from).fill(resource, doFill); + } + return 0; + } - if (this.liquid.amount <= 0) - { - this.liquid = null; - } - if (doDrain) - { - this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord); - FluidEvent.fireEvent(new FluidEvent.FluidDrainingEvent(drained, this.worldObj, this.xCoord, this.yCoord, this.zCoord, this)); - } - return drained; - } + @Override + public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) + { + return drain(from, resource.amount, doDrain); + } - @Override - public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain) - { - return this.drain(maxDrain, doDrain); - } + @Override + public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) + { + //if(doDrain) + // this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord); + if(from == ForgeDirection.DOWN) + return this.internalTank.drain(maxDrain, doDrain); + else if(from == ForgeDirection.NORTH || from == ForgeDirection.SOUTH || from == ForgeDirection.WEST || from == ForgeDirection.EAST) + { + return this.subTanks.get(from).drain(maxDrain, doDrain); + } - @Override - public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain) - { - return this.drain(resource.amount, doDrain); - } + return null; + } - @Override - public int getCapacity () - { - return this.fillMax; - } + @Override + public boolean canFill(ForgeDirection from, Fluid fluid) + { + if(from == ForgeDirection.DOWN) + return false; + if(from == ForgeDirection.UP) + return true; + return validOutputs.contains(from); + } - public int getLiquidAmount () - { - return this.liquid.amount; - } + @Override + public boolean canDrain(ForgeDirection from, Fluid fluid) + { + if(from == ForgeDirection.UP) + return false; + return validOutputs.contains(from); + } - @Override - public void readFromNBT (NBTTagCompound tags) - { - super.readFromNBT(tags); - readCustomNBT(tags); - pullingLiquids = tags.getBoolean("PullingLiquids"); - } + @Override + public FluidTankInfo[] getTankInfo(ForgeDirection from) + { + if(from == null || from == ForgeDirection.UP || from == ForgeDirection.DOWN || from == ForgeDirection.UNKNOWN) + return new FluidTankInfo[]{new FluidTankInfo(internalTank)}; + else + return new FluidTankInfo[]{new FluidTankInfo(subTanks.get(from))}; + } - public void readCustomNBT (NBTTagCompound tags) - { - if (tags.getBoolean("hasLiquid")) - { - this.liquid = FluidStack.loadFluidStackFromNBT(tags.getCompoundTag("Fluid")); - } - else - this.liquid = null; - this.recentlyFilledDelay = tags.getInteger("recentlyFilledDelay"); - this.lastProvider = this.convertIntToFD(tags.getInteger("LastProvider")); - } + @Override + public void readFromNBT (NBTTagCompound tags) + { + super.readFromNBT(tags); + readCustomNBT(tags); + } - @Override - public void writeToNBT (NBTTagCompound tags) - { - super.writeToNBT(tags); - writeCustomNBT(tags); - tags.setBoolean("PullingLiquids", pullingLiquids); - } + public void readCustomNBT (NBTTagCompound tags) + { - public void writeCustomNBT (NBTTagCompound tags) - { - tags.setBoolean("hasLiquid", liquid != null); - if (liquid != null) - { - NBTTagCompound nbt = new NBTTagCompound(); - liquid.writeToNBT(nbt); - tags.setCompoundTag("Fluid", nbt); - } - tags.setInteger("LastProvider", this.convertFDToInt(this.lastProvider)); - tags.setInteger("recentlyFilledDelay", this.recentlyFilledDelay); - } + NBTTagCompound nbtTank = tags.getCompoundTag("internalTank"); + if(nbtTank != null) + internalTank.readFromNBT(nbtTank); - @Override - public Packet getDescriptionPacket () - { - NBTTagCompound tag = new NBTTagCompound(); - writeCustomNBT(tag); - return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag); - } + for(ForgeDirection fdSub: subTanks.keySet()) + { + NBTTagCompound nbtSubTank = tags.getCompoundTag("subTank_"+fdSub.name()); + if(nbtSubTank != null) + subTanks.get(fdSub).readFromNBT(nbtSubTank); + } - @Override - public void onDataPacket (INetworkManager net, Packet132TileEntityData packet) - { - readCustomNBT(packet.data); - this.worldObj.markBlockForRenderUpdate(this.xCoord, this.yCoord, this.zCoord); - } + int[] validFDs = tags.getIntArray("validOutputs"); + if(validFDs != null) + { + validOutputs = new ArrayList(); + for(int i: validFDs) + validOutputs.add(convertIntToFD(i)); + } + /* UNUSED + this.mode = CastingChannelMode.readFromNBT(tags); + */ + this.lastProvider = this.convertIntToFD(tags.getInteger("LastProvider")); + } - public int convertFDToInt (ForgeDirection dir) - { - switch (dir) - { - case DOWN: - return 0; - case UP: - return 1; - case NORTH: - return 2; - case SOUTH: - return 3; - case WEST: - return 4; - case EAST: - return 5; - default: - return -1; - } - } + @Override + public void writeToNBT (NBTTagCompound tags) + { + super.writeToNBT(tags); + writeCustomNBT(tags); + } - public ForgeDirection convertIntToFD (int i) - { - switch (i) - { - case 0: - return ForgeDirection.DOWN; - case 1: - return ForgeDirection.UP; - case 2: - return ForgeDirection.NORTH; - case 3: - return ForgeDirection.SOUTH; - case 4: - return ForgeDirection.WEST; - case 5: - return ForgeDirection.EAST; - default: - return ForgeDirection.UNKNOWN; - } - } + public void writeCustomNBT (NBTTagCompound tags) + { + NBTTagCompound nbtTank = new NBTTagCompound(); + internalTank.writeToNBT(nbtTank); + tags.setCompoundTag("internalTank", nbtTank); + for(ForgeDirection fdSub: subTanks.keySet()) + { + NBTTagCompound nbtSubTank = new NBTTagCompound(); + subTanks.get(fdSub).writeToNBT(nbtSubTank); + tags.setCompoundTag("subTank_"+fdSub.name(), nbtSubTank); + } - @Override - public boolean getActive () - { - return pullingLiquids; - } + int[] validFDs = new int[validOutputs.size()]; + int it = 0; + for(ForgeDirection fd: validOutputs) + { + if(fd != null) + validFDs[it] = convertFDToInt(fd); + it++; + } + tags.setIntArray("validOutputs", validFDs); + /* UNUSED + CastingChannelMode.writeToNBT(tags, this.mode); + */ + tags.setInteger("LastProvider", this.convertFDToInt(this.lastProvider)); + } - @Override - public void setActive (boolean flag) - { - //pullingLiquids = flag; - pullingLiquids = !pullingLiquids; - } + @Override + public Packet getDescriptionPacket () + { + NBTTagCompound tag = new NBTTagCompound(); + writeCustomNBT(tag); + return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag); + } - @Override - public boolean canFill (ForgeDirection from, Fluid fluid) - { - return liquid == null || (liquid.amount < fillMax); - } + @Override + public void onDataPacket (INetworkManager net, Packet132TileEntityData packet) + { + readCustomNBT(packet.data); + this.worldObj.markBlockForRenderUpdate(this.xCoord, this.yCoord, this.zCoord); + } - @Override - public boolean canDrain (ForgeDirection from, Fluid fluid) - { - return true; - } + public int convertFDToInt (ForgeDirection dir) + { + switch (dir) + { + case DOWN: + return 0; + case UP: + return 1; + case NORTH: + return 2; + case SOUTH: + return 3; + case WEST: + return 4; + case EAST: + return 5; + default: + return -1; + } + } - @Override - public FluidTankInfo[] getTankInfo (ForgeDirection from) - { - return new FluidTankInfo[] { getInfo() }; - } + public ForgeDirection convertIntToFD (int i) + { + switch (i) + { + case 0: + return ForgeDirection.DOWN; + case 1: + return ForgeDirection.UP; + case 2: + return ForgeDirection.NORTH; + case 3: + return ForgeDirection.SOUTH; + case 4: + return ForgeDirection.WEST; + case 5: + return ForgeDirection.EAST; + default: + return ForgeDirection.UNKNOWN; + } + } + /* UNUSED + public enum CastingChannelMode + { + //Will not output while being filled by Faucet, hence only taking one ingots worth from the Smeltery + SINGLEDRAIN, + //Will keep the Faucet above it open + CONTINUEDRAIN; - @Override - public FluidStack getFluid () - { - return liquid == null ? null : liquid.copy(); - } + public static void writeToNBT(NBTTagCompound tag, CastingChannelMode mode) + { + if(mode == SINGLEDRAIN) + tag.setString("CastingChannelMode","single"); + else + tag.setString("CastingChannelMode","continue"); + } + public static CastingChannelMode readFromNBT(NBTTagCompound tag) + { + return (!tag.hasKey("CastingChannelMode") || tag.getString("CastingChannelMode")=="single") ? SINGLEDRAIN : CONTINUEDRAIN ; + } + } + */ - @Override - public int getFluidAmount () - { - return liquid == null ? 0 : liquid.amount; - } - - @Override - public FluidTankInfo getInfo () - { - FluidTankInfo info = new FluidTankInfo(this); - return info; - } }