2014-01-20 17:41:37 +01:00
|
|
|
package appeng.parts.p2p;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-08-10 07:16:02 +02:00
|
|
|
import java.util.Stack;
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.init.Blocks;
|
2014-01-20 17:41:37 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.util.IIcon;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-01-20 17:41:37 +01:00
|
|
|
import net.minecraftforge.fluids.Fluid;
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
import net.minecraftforge.fluids.FluidTankInfo;
|
|
|
|
import net.minecraftforge.fluids.IFluidHandler;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.config.TunnelType;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.GridAccessException;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
|
|
|
|
public class PartP2PLiquids extends PartP2PTunnel<PartP2PLiquids> implements IFluidHandler
|
|
|
|
{
|
|
|
|
|
2014-04-21 23:11:39 +02:00
|
|
|
private final static FluidTankInfo[] activeTank = new FluidTankInfo[] { new FluidTankInfo( null, 10000 ) };
|
|
|
|
private final static FluidTankInfo[] inactiveTank = new FluidTankInfo[] { new FluidTankInfo( null, 0 ) };
|
2014-05-18 21:44:58 +02:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public TunnelType getTunnelType()
|
|
|
|
{
|
|
|
|
return TunnelType.FLUID;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public PartP2PLiquids(ItemStack is) {
|
|
|
|
super( is );
|
|
|
|
}
|
|
|
|
|
|
|
|
private FluidTankInfo[] getTank()
|
|
|
|
{
|
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
PartP2PLiquids tun = getInput();
|
|
|
|
if ( tun != null )
|
2014-04-21 23:11:39 +02:00
|
|
|
return activeTank;
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( !getOutputs().isEmpty() )
|
2014-04-21 23:11:39 +02:00
|
|
|
return activeTank;
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :(
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 23:11:39 +02:00
|
|
|
return inactiveTank;
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IFluidHandler cachedTank;
|
|
|
|
|
|
|
|
public float getPowerDrainPerTick()
|
|
|
|
{
|
|
|
|
return 2.0f;
|
|
|
|
};
|
|
|
|
|
|
|
|
private int tmpUsed;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
super.writeToNBT( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
super.readFromNBT( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-02-09 02:34:52 +01:00
|
|
|
public IIcon getTypeTexture()
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-04-06 04:50:01 +02:00
|
|
|
return Blocks.lapis_block.getBlockTextureFromSide( 0 );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
List<PartP2PLiquids> getOutputs(Fluid input)
|
|
|
|
{
|
|
|
|
List<PartP2PLiquids> outs = new LinkedList<PartP2PLiquids>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for (PartP2PLiquids l : getOutputs())
|
|
|
|
{
|
2014-05-18 21:44:58 +02:00
|
|
|
IFluidHandler targ = l.getTarget();
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( targ != null )
|
|
|
|
{
|
2014-07-26 23:35:02 +02:00
|
|
|
if ( targ.canFill( l.side.getOpposite(), input ) )
|
2014-01-20 17:41:37 +01:00
|
|
|
outs.add( l );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
|
|
|
|
return outs;
|
|
|
|
}
|
|
|
|
|
2014-01-31 04:06:30 +01:00
|
|
|
@Override
|
|
|
|
public void onNeighborChanged()
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
cachedTank = null;
|
2014-01-31 04:06:30 +01:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
PartP2PLiquids in = getInput();
|
|
|
|
if ( in != null )
|
2014-06-09 01:10:19 +02:00
|
|
|
in.onTunnelNetworkChange();
|
2014-01-31 04:06:30 +01:00
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
};
|
|
|
|
|
2014-01-31 04:06:30 +01:00
|
|
|
@Override
|
2014-06-09 01:10:19 +02:00
|
|
|
public void onTunnelNetworkChange()
|
2014-01-31 04:06:30 +01:00
|
|
|
{
|
|
|
|
cachedTank = null;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
IFluidHandler getTarget()
|
|
|
|
{
|
2014-05-18 21:44:58 +02:00
|
|
|
if ( !proxy.isActive() )
|
|
|
|
return null;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( cachedTank != null )
|
|
|
|
return cachedTank;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
TileEntity te = tile.getWorldObj().getTileEntity( tile.xCoord + side.offsetX, tile.yCoord + side.offsetY, tile.zCoord + side.offsetZ );
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( te instanceof IFluidHandler )
|
|
|
|
return cachedTank = (IFluidHandler) te;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-08-10 07:16:02 +02:00
|
|
|
static final ThreadLocal<Stack<PartP2PLiquids>> depth = new ThreadLocal<Stack<PartP2PLiquids>>();
|
|
|
|
|
|
|
|
private Stack<PartP2PLiquids> getDepth()
|
|
|
|
{
|
|
|
|
Stack<PartP2PLiquids> s = depth.get();
|
|
|
|
|
|
|
|
if ( s == null )
|
|
|
|
depth.set( s = new Stack<PartP2PLiquids>() );
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
@Override
|
|
|
|
public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
|
|
|
|
{
|
2014-08-10 07:16:02 +02:00
|
|
|
Stack<PartP2PLiquids> stack = getDepth();
|
|
|
|
|
|
|
|
for (PartP2PLiquids t : stack)
|
|
|
|
if ( t == this )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
stack.push( this );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
List<PartP2PLiquids> list = getOutputs( resource.getFluid() );
|
|
|
|
int requestTotal = 0;
|
|
|
|
|
|
|
|
Iterator<PartP2PLiquids> i = list.iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
PartP2PLiquids l = i.next();
|
|
|
|
IFluidHandler tank = l.getTarget();
|
|
|
|
if ( tank != null )
|
|
|
|
l.tmpUsed = tank.fill( l.side.getOpposite(), resource.copy(), false );
|
|
|
|
else
|
|
|
|
l.tmpUsed = 0;
|
|
|
|
|
|
|
|
if ( l.tmpUsed <= 0 )
|
|
|
|
i.remove();
|
|
|
|
else
|
|
|
|
requestTotal += l.tmpUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( requestTotal <= 0 )
|
2014-08-10 07:16:02 +02:00
|
|
|
{
|
|
|
|
if ( stack.pop() != this )
|
|
|
|
throw new RuntimeException( "Invalid Recursion detected." );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
return 0;
|
2014-08-10 07:16:02 +02:00
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
if ( !doFill )
|
2014-08-10 07:16:02 +02:00
|
|
|
{
|
|
|
|
if ( stack.pop() != this )
|
|
|
|
throw new RuntimeException( "Invalid Recursion detected." );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
return Math.min( resource.amount, requestTotal );
|
2014-08-10 07:16:02 +02:00
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
|
2014-09-21 01:48:57 +02:00
|
|
|
int available = resource.amount;
|
2014-01-20 17:41:37 +01:00
|
|
|
int used = 0;
|
|
|
|
|
|
|
|
i = list.iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
PartP2PLiquids l = i.next();
|
|
|
|
|
|
|
|
FluidStack insert = resource.copy();
|
|
|
|
insert.amount = (int) Math.ceil( insert.amount * ((double) l.tmpUsed / (double) requestTotal) );
|
2014-09-21 01:48:57 +02:00
|
|
|
if ( insert.amount > available )
|
|
|
|
insert.amount = available;
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
IFluidHandler tank = l.getTarget();
|
|
|
|
if ( tank != null )
|
|
|
|
l.tmpUsed = tank.fill( l.side.getOpposite(), insert.copy(), true );
|
|
|
|
else
|
|
|
|
l.tmpUsed = 0;
|
|
|
|
|
2014-09-21 01:48:57 +02:00
|
|
|
available -= insert.amount;
|
2014-01-20 17:41:37 +01:00
|
|
|
used += insert.amount;
|
|
|
|
}
|
|
|
|
|
2014-08-10 07:16:02 +02:00
|
|
|
if ( stack.pop() != this )
|
|
|
|
throw new RuntimeException( "Invalid Recursion detected." );
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canFill(ForgeDirection from, Fluid fluid)
|
|
|
|
{
|
|
|
|
return !output && from.equals( side ) && !getOutputs( fluid ).isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canDrain(ForgeDirection from, Fluid fluid)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidTankInfo[] getTankInfo(ForgeDirection from)
|
|
|
|
{
|
|
|
|
if ( from.equals( side ) )
|
|
|
|
return getTank();
|
|
|
|
return new FluidTankInfo[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|