Search in sources :

Example 21 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityFluidSorter method getValidOutputs.

public IFluidHandler[][] getValidOutputs(EnumFacing inputSide, FluidStack fluidStack, boolean allowUnmapped) {
    if (fluidStack == null)
        return new IFluidHandler[2][0];
    ArrayList<IFluidHandler> validFilteredInvOuts = new ArrayList<IFluidHandler>(6);
    ArrayList<IFluidHandler> validUnfilteredInvOuts = new ArrayList<IFluidHandler>(6);
    for (EnumFacing side : EnumFacing.values()) if (side != inputSide && worldObj.isBlockLoaded(getPos().offset(side))) {
        boolean unmapped = true;
        boolean allowed = false;
        filterIteration: {
            for (FluidStack filterStack : filters[side.ordinal()]) if (filterStack != null) {
                unmapped = false;
                boolean b = filterStack.getFluid() == fluidStack.getFluid();
                if (doNBT(side.ordinal()))
                    b &= FluidStack.areFluidStackTagsEqual(filterStack, fluidStack);
                if (b) {
                    allowed = true;
                    break filterIteration;
                }
            }
        }
        if (allowed || (allowUnmapped && unmapped)) {
            TileEntity tile = worldObj.getTileEntity(getPos().offset(side));
            if (tile != null && tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite())) {
                IFluidHandler handler = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite());
                if (handler.fill(fluidStack.copy(), false) > 0)
                    if (allowed)
                        validFilteredInvOuts.add(handler);
                    else
                        validUnfilteredInvOuts.add(handler);
            }
        }
    }
    return new IFluidHandler[][] { validFilteredInvOuts.toArray(new IFluidHandler[validFilteredInvOuts.size()]), validUnfilteredInvOuts.toArray(new IFluidHandler[validUnfilteredInvOuts.size()]) };
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 22 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class RecipeJerrycan method matches.

@Override
public boolean matches(InventoryCrafting inv, World world) {
    ItemStack jerrycan = null;
    ItemStack container = null;
    for (int i = 0; i < inv.getSizeInventory(); i++) {
        ItemStack stackInSlot = inv.getStackInSlot(i);
        if (stackInSlot != null)
            if (jerrycan == null && IEContent.itemJerrycan.equals(stackInSlot.getItem()) && FluidUtil.getFluidContained(stackInSlot) != null)
                jerrycan = stackInSlot;
            else if (container == null && FluidUtil.getFluidHandler(stackInSlot) != null)
                container = stackInSlot;
            else
                return false;
    }
    if (jerrycan != null && container != null) {
        IFluidHandler handler = FluidUtil.getFluidHandler(container);
        FluidStack fs = handler.drain(Integer.MAX_VALUE, false);
        if (fs == null || (fs.amount < handler.getTankProperties()[0].getCapacity() && fs.isFluidEqual(FluidUtil.getFluidContained(jerrycan))))
            return true;
    }
    return false;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 23 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class RecipeJerrycan method getCraftingResult.

@Override
public ItemStack getCraftingResult(InventoryCrafting inv) {
    ItemStack jerrycan = null;
    ItemStack container = null;
    FluidStack fs = null;
    for (int i = 0; i < inv.getSizeInventory(); i++) {
        ItemStack stackInSlot = inv.getStackInSlot(i);
        if (stackInSlot != null)
            if (jerrycan == null && IEContent.itemJerrycan.equals(stackInSlot.getItem()) && FluidUtil.getFluidContained(stackInSlot) != null) {
                jerrycan = stackInSlot;
                fs = FluidUtil.getFluidContained(jerrycan);
            } else if (container == null && FluidUtil.getFluidHandler(stackInSlot) != null)
                container = stackInSlot;
    }
    if (fs != null && container != null) {
        ItemStack newContainer = Utils.copyStackWithAmount(container, 1);
        IFluidHandler handler = FluidUtil.getFluidHandler(newContainer);
        int accepted = handler.fill(fs, false);
        if (accepted > 0) {
            handler.fill(fs, true);
            //				FluidUtil.getFluidHandler(jerrycan).drain(accepted,true);
            ItemNBTHelper.setInt(jerrycan, "jerrycanDrain", accepted);
        }
        return newContainer;
    }
    return null;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 24 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityFluidPump method outputFluid.

public int outputFluid(FluidStack fs, boolean simulate) {
    if (fs == null)
        return 0;
    int canAccept = fs.amount;
    if (canAccept <= 0)
        return 0;
    int accelPower = IEConfig.Machines.pump_consumption_accelerate;
    final int fluidForSort = canAccept;
    int sum = 0;
    HashMap<DirectionalFluidOutput, Integer> sorting = new HashMap<DirectionalFluidOutput, Integer>();
    for (EnumFacing f : EnumFacing.values()) if (sideConfig[f.ordinal()] == 1) {
        TileEntity tile = worldObj.getTileEntity(getPos().offset(f));
        if (tile != null && tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite())) {
            IFluidHandler handler = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite());
            FluidStack insertResource = new FluidStack(fs.getFluid(), fs.amount);
            if (tile instanceof TileEntityFluidPipe && this.energyStorage.extractEnergy(accelPower, true) >= accelPower) {
                insertResource.tag = new NBTTagCompound();
                insertResource.tag.setBoolean("pressurized", true);
            }
            int temp = handler.fill(insertResource, false);
            if (temp > 0) {
                sorting.put(new DirectionalFluidOutput(handler, tile, f), temp);
                sum += temp;
            }
        }
    }
    if (sum > 0) {
        int f = 0;
        int i = 0;
        for (DirectionalFluidOutput output : sorting.keySet()) {
            float prio = sorting.get(output) / (float) sum;
            int amount = (int) (fluidForSort * prio);
            if (i++ == sorting.size() - 1)
                amount = canAccept;
            FluidStack insertResource = new FluidStack(fs.getFluid(), amount);
            if (output.containingTile instanceof TileEntityFluidPipe && this.energyStorage.extractEnergy(accelPower, true) >= accelPower) {
                this.energyStorage.extractEnergy(accelPower, false);
                insertResource.tag = new NBTTagCompound();
                insertResource.tag.setBoolean("pressurized", true);
            }
            int r = output.output.fill(insertResource, !simulate);
            f += r;
            canAccept -= r;
            if (canAccept <= 0)
                break;
        }
        return f;
    }
    return 0;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) HashMap(java.util.HashMap) FluidStack(net.minecraftforge.fluids.FluidStack) DirectionalFluidOutput(blusunrize.immersiveengineering.common.blocks.metal.TileEntityFluidPipe.DirectionalFluidOutput) EnumFacing(net.minecraft.util.EnumFacing) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 25 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntityFluidPump method update.

@Override
public void update() {
    if (dummy || worldObj.isRemote)
        return;
    if (tank.getFluidAmount() > 0) {
        int i = outputFluid(tank.getFluid(), false);
        tank.drain(i, true);
    }
    if (worldObj.isBlockIndirectlyGettingPowered(getPos()) > 0 || worldObj.isBlockIndirectlyGettingPowered(getPos().add(0, 1, 0)) > 0) {
        for (EnumFacing f : EnumFacing.values()) if (sideConfig[f.ordinal()] == 0) {
            TileEntity tile = worldObj.getTileEntity(getPos().offset(f));
            if (tile != null && tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite())) {
                IFluidHandler handler = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite());
                FluidStack drain = handler.drain(500, false);
                if (drain == null || drain.amount <= 0)
                    continue;
                int out = this.outputFluid(drain, false);
                handler.drain(out, true);
            } else if (worldObj.getTotalWorldTime() % 20 == ((getPos().getX() ^ getPos().getZ()) & 19) && worldObj.getBlockState(getPos().offset(f)).getBlock() == Blocks.WATER && IEConfig.Machines.pump_infiniteWater && tank.fill(new FluidStack(FluidRegistry.WATER, 1000), false) == 1000 && this.energyStorage.extractEnergy(IEConfig.Machines.pump_consumption, true) >= IEConfig.Machines.pump_consumption) {
                int connectedSources = 0;
                for (EnumFacing f2 : EnumFacing.HORIZONTALS) {
                    IBlockState waterState = worldObj.getBlockState(getPos().offset(f).offset(f2));
                    if (waterState.getBlock() == Blocks.WATER && Blocks.WATER.getMetaFromState(waterState) == 0)
                        connectedSources++;
                }
                if (connectedSources > 1) {
                    this.energyStorage.extractEnergy(IEConfig.Machines.pump_consumption, false);
                    this.tank.fill(new FluidStack(FluidRegistry.WATER, 1000), true);
                }
            }
        }
        if (worldObj.getTotalWorldTime() % 40 == (((getPos().getX() ^ getPos().getZ())) % 40 + 40) % 40) {
            if (closedList.isEmpty())
                prepareAreaCheck();
            else {
                int target = closedList.size() - 1;
                BlockPos pos = closedList.get(target);
                FluidStack fs = Utils.drainFluidBlock(worldObj, pos, false);
                if (fs == null)
                    closedList.remove(target);
                else if (tank.fill(fs, false) == fs.amount && this.energyStorage.extractEnergy(IEConfig.Machines.pump_consumption, true) >= IEConfig.Machines.pump_consumption) {
                    this.energyStorage.extractEnergy(IEConfig.Machines.pump_consumption, false);
                    fs = Utils.drainFluidBlock(worldObj, pos, true);
                    //						worldObj.setBlock( cc.posX,cc.posY,cc.posZ, Blocks.stained_glass,rainbow, 0x3);
                    if (IEConfig.Machines.pump_placeCobble && placeCobble)
                        worldObj.setBlockState(pos, Blocks.COBBLESTONE.getDefaultState());
                    this.tank.fill(fs, true);
                    closedList.remove(target);
                }
            }
        }
    }
    if (checkingArea)
        checkAreaTick();
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)28 FluidStack (net.minecraftforge.fluids.FluidStack)16 TileEntity (net.minecraft.tileentity.TileEntity)12 ItemStack (net.minecraft.item.ItemStack)11 EnumFacing (net.minecraft.util.EnumFacing)10 BlockPos (net.minecraft.util.math.BlockPos)7 IBlockState (net.minecraft.block.state.IBlockState)3 IFluidTankProperties (net.minecraftforge.fluids.capability.IFluidTankProperties)3 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 AdvancedFluidHandler (mods.railcraft.common.fluids.AdvancedFluidHandler)2 Block (net.minecraft.block.Block)2 BlockLiquid (net.minecraft.block.BlockLiquid)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidActionResult (net.minecraftforge.fluids.FluidActionResult)2 FermenterRecipe (blusunrize.immersiveengineering.api.crafting.FermenterRecipe)1 MixerRecipe (blusunrize.immersiveengineering.api.crafting.MixerRecipe)1 RefineryRecipe (blusunrize.immersiveengineering.api.crafting.RefineryRecipe)1 SqueezerRecipe (blusunrize.immersiveengineering.api.crafting.SqueezerRecipe)1