Search in sources :

Example 46 with IFluidHandler

use of net.minecraftforge.fluids.IFluidHandler in project TechReborn by TechReborn.

the class MultipartFluidPipe method update.

@Override
public void update() {
    if (!getWorld().isRemote) {
        for (EnumFacing dir : EnumFacing.VALUES) {
            // if (connectedSides.containsKey(dir)) {
            MultipartFluidPipe fluidPipe = getPartFromWorld(getWorld(), getPos().offset(dir), dir);
            if (fluidPipe != null) {
                if (!tank.isEmpty()) {
                    if (fluidPipe.tank.isEmpty() || fluidPipe.tank.getFluid().getFluid() == tank.getFluid().getFluid()) {
                        if (fluidPipe.tank.getFluidAmount() < tank.getFluidAmount()) {
                            int freeSpace = fluidPipe.tank.getCapacity();
                            if (!fluidPipe.tank.isEmpty()) {
                                freeSpace = fluidPipe.tank.getCapacity() - fluidPipe.tank.getFluidAmount();
                            }
                            int difference = tank.getFluidAmount() - freeSpace;
                            int amountToChange = difference / 2;
                            fluidPipe.tank.setFluid(tank.getFluid());
                            fluidPipe.tank.setFluidAmount(fluidPipe.tank.getFluidAmount() + amountToChange);
                            tank.setFluidAmount(tank.getFluidAmount() - amountToChange);
                        }
                    }
                }
            }
            // }
            TileEntity tileEntity = getNeighbourTile(dir);
            if (tileEntity != null) {
                if (tileEntity instanceof IFluidHandler) {
                    IFluidHandler handler = (IFluidHandler) tileEntity;
                    if (getPipeType() == EnumFluidPipeTypes.EXTRACT) {
                        if (!tank.isFull()) {
                            FluidTankInfo[] fluidTankInfos = handler.getTankInfo(dir.getOpposite());
                            if (fluidTankInfos.length != 0) {
                                FluidTankInfo info = fluidTankInfos[0];
                                if (info != null & info.fluid != null) {
                                    if (tank.isEmpty() || info.fluid.getFluid() == tank.getFluid().getFluid()) {
                                        if (handler.canDrain(dir.getOpposite(), info.fluid.getFluid())) {
                                            int amountToMove = Math.min(mbt, tank.getCapacity() - tank.getFluidAmount());
                                            int fluidAmount = tank.getFluidAmount();
                                            FluidStack fluidStack = handler.drain(dir.getOpposite(), amountToMove, true);
                                            tank.fill(fluidStack, true);
                                            tank.setFluid(fluidStack);
                                            tank.setFluidAmount(fluidAmount + amountToMove);
                                        }
                                    }
                                }
                            }
                        }
                    } else if (getPipeType() == EnumFluidPipeTypes.INSERT) {
                        if (!tank.isEmpty()) {
                            FluidTankInfo[] fluidTankInfos = handler.getTankInfo(dir.getOpposite());
                            if (fluidTankInfos.length != 0) {
                                FluidTankInfo info = fluidTankInfos[0];
                                if (info.fluid == null || info.fluid.getFluid() == null || info.fluid.getFluid() == tank.getFluid().getFluid() && handler.canFill(dir.getOpposite(), tank.getFluid().getFluid())) {
                                    int infoSpace = info.capacity;
                                    if (info.fluid != null) {
                                        infoSpace = info.capacity - info.fluid.amount;
                                    }
                                    int amountToMove = Math.min(mbt, infoSpace);
                                    FluidStack fluidStack = tank.drain(amountToMove, true);
                                    handler.fill(dir.getOpposite(), fluidStack, true);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidTankInfo(net.minecraftforge.fluids.FluidTankInfo) FluidStack(net.minecraftforge.fluids.FluidStack) IFluidHandler(net.minecraftforge.fluids.IFluidHandler)

Aggregations

IFluidHandler (net.minecraftforge.fluids.IFluidHandler)46 TileEntity (net.minecraft.tileentity.TileEntity)35 FluidStack (net.minecraftforge.fluids.FluidStack)33 ItemStack (net.minecraft.item.ItemStack)13 EnumFacing (net.minecraft.util.EnumFacing)13 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)11 IInventory (net.minecraft.inventory.IInventory)9 FluidTankInfo (net.minecraftforge.fluids.FluidTankInfo)7 IPipeTile (buildcraft.api.transport.IPipeTile)5 ArrayList (java.util.ArrayList)5 Fluid (net.minecraftforge.fluids.Fluid)5 FluidIdentifier (logisticspipes.utils.FluidIdentifier)3 Block (net.minecraft.block.Block)3 IBlockState (net.minecraft.block.state.IBlockState)3 ChunkPosition (net.minecraft.world.ChunkPosition)3 IFluidContainerItem (net.minecraftforge.fluids.IFluidContainerItem)3 TargetPoint (cpw.mods.fml.common.network.NetworkRegistry.TargetPoint)2 HashMap (java.util.HashMap)2 IBufferTransmitter (micdoodle8.mods.galacticraft.api.transmission.tile.IBufferTransmitter)2 RadiationFluidTileEntity (minechem.radiation.RadiationFluidTileEntity)2