Example usage for net.minecraftforge.fluids.capability IFluidHandler drain

List of usage examples for net.minecraftforge.fluids.capability IFluidHandler drain

Introduction

In this page you can find the example usage for net.minecraftforge.fluids.capability IFluidHandler drain.

Prototype

@Nonnull
FluidStack drain(int maxDrain, FluidAction action);

Source Link

Document

Drains fluid out of internal tanks, distribution is left entirely to the IFluidHandler.

Usage

From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityFluidPump.java

@Override
public void update() {
    ApiUtils.checkForNeedlessTicking(this);
    if (dummy || world.isRemote)
        return;/*from   w  w w.  ja  va 2 s  .  com*/
    if (tank.getFluidAmount() > 0) {
        int i = outputFluid(tank.getFluid(), false);
        tank.drain(i, true);
    }

    if (world.getRedstonePowerFromNeighbors(getPos()) > 0
            || world.getRedstonePowerFromNeighbors(getPos().add(0, 1, 0)) > 0) {
        for (EnumFacing f : EnumFacing.values())
            if (sideConfig[f.ordinal()] == 0) {
                BlockPos output = getPos().offset(f);
                TileEntity tile = Utils.getExistingTileEntity(world, output);
                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 (world.getTotalWorldTime() % 20 == ((getPos().getX() ^ getPos().getZ()) & 19)
                        && world.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 = world.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 (world.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(world, 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(world, pos, true);
                    //                  int rainbow = (closedList.size()%11)+1;
                    //                  if(rainbow>6)
                    //                     rainbow+=2;
                    //                  if(rainbow>9)
                    //                     rainbow++;
                    //                  world.setBlock( cc.posX,cc.posY,cc.posZ, Blocks.stained_glass,rainbow, 0x3);
                    if (IEConfig.Machines.pump_placeCobble && placeCobble)
                        world.setBlockState(pos, Blocks.COBBLESTONE.getDefaultState());
                    this.tank.fill(fs, true);
                    closedList.remove(target);
                }
            }
        }
    }

    if (checkingArea)
        checkAreaTick();
}

From source file:blusunrize.immersiveengineering.common.crafting.RecipeJerrycan.java

@Override
public boolean matches(@Nonnull InventoryCrafting inv, World world) {

    ItemStack jerrycan = ItemStack.EMPTY;
    ItemStack container = ItemStack.EMPTY;
    int[] slots = getRelevantSlots(inv);
    if (slots[0] >= 0)
        jerrycan = inv.getStackInSlot(slots[0]);
    if (slots[1] >= 0)
        container = inv.getStackInSlot(slots[1]);
    if (!jerrycan.isEmpty() && !container.isEmpty()) {
        IFluidHandler handler = FluidUtil.getFluidHandler(container);
        FluidStack fs = handler.drain(Integer.MAX_VALUE, false);
        return fs == null || (fs.amount < handler.getTankProperties()[0].getCapacity()
                && fs.isFluidEqual(FluidUtil.getFluidContained(jerrycan)));
    }/* w  w w. j a v  a2 s.com*/
    return false;
}

From source file:blusunrize.immersiveengineering.common.items.ItemDrill.java

@Override
public boolean onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos,
        EntityLivingBase living) {/*  w ww.  java 2s  . c om*/
    if ((double) state.getBlockHardness(world, pos) != 0.0D) {
        int dmg = ForgeHooks.isToolEffective(world, pos, stack) ? 1 : 3;
        ItemStack head = getHead(stack);
        if (!head.isEmpty()) {
            if (living instanceof EntityPlayer) {
                if (((EntityPlayer) living).capabilities.isCreativeMode)
                    return true;
                ((IDrillHead) head.getItem()).afterBlockbreak(stack, head, (EntityPlayer) living);
            }
            if (!getUpgrades(stack).getBoolean("oiled") || Utils.RAND.nextInt(4) == 0)
                ((IDrillHead) head.getItem()).damageHead(head, dmg);
            this.setHead(stack, head);
            IFluidHandler handler = FluidUtil.getFluidHandler(stack);
            handler.drain(1, true);

            Triple<ItemStack, ShaderRegistryEntry, ShaderCase> shader = ShaderRegistry
                    .getStoredShaderAndCase(stack);
            if (shader != null)
                shader.getMiddle().getEffectFunction().execute(world, shader.getLeft(), stack,
                        shader.getRight().getShaderType(),
                        new Vec3d(pos.getX() + .5, pos.getY() + .5, pos.getZ() + .5), null, .375f);
        }
    }

    return true;
}

From source file:blusunrize.immersiveengineering.common.items.ItemJerrycan.java

@Override
public ItemStack getContainerItem(ItemStack stack) {
    ItemStack compare = stack;//  w  w w.ja  va  2  s . c o m
    if (stack.isEmpty()) {
        stack.grow(1);
        compare = stack.copy();
        stack.shrink(1);
    }
    if (ItemNBTHelper.hasKey(compare, "jerrycanDrain")) {
        ItemStack ret = compare.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(ItemNBTHelper.getInt(ret, "jerrycanDrain"), true);
        ItemNBTHelper.remove(ret, "jerrycanDrain");
        return ret;
    } else if (FluidUtil.getFluidContained(compare) != null) {
        ItemStack ret = compare.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(1000, true);
        return ret;
    }
    return stack;
}

From source file:com.buuz135.industrial.item.addon.movility.FluidTransferAddon.java

License:Open Source License

@Override
public boolean actionTransfer(World world, BlockPos pos, EnumFacing facing, int transferAmount) {
    TileEntity tileEntityOrigin = world.getTileEntity(pos);
    TileEntity tileEntityDestination = world.getTileEntity(pos.offset(facing));
    if (tileEntityOrigin != null && tileEntityDestination != null
            && tileEntityOrigin.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing)
            && tileEntityDestination.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY,
                    facing.getOpposite())) {
        IFluidHandler fluidHandlerOrigin = getCapabilityOriginByMode(tileEntityOrigin, tileEntityDestination,
                facing);//from  w w w .  j  a v  a2 s . co m
        IFluidHandler fluidHandlerDestination = getCapabilityDestinationByMode(tileEntityOrigin,
                tileEntityDestination, facing);
        FluidStack fluidStack = fluidHandlerOrigin.drain(transferAmount * 100, false);
        if (fluidStack != null) {
            fluidHandlerOrigin.drain(fluidHandlerDestination.fill(fluidStack, true), true);
            return true;
        }
    }
    return false;
}

From source file:com.buuz135.industrial.item.ItemStraw.java

License:Open Source License

@Override
@Nonnull//  w  w  w  .  j a  v  a2  s . co m
public ItemStack onItemUseFinish(@Nonnull ItemStack heldStack, World world, EntityLivingBase entity) {
    if (!world.isRemote && entity instanceof EntityPlayer) {
        EntityPlayer player = (EntityPlayer) entity;
        RayTraceResult result = rayTrace(world, player, true);
        if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) {
            BlockPos pos = result.getBlockPos();
            IBlockState state = world.getBlockState(pos);
            Block block = state.getBlock();
            Fluid fluid = FluidRegistry.lookupFluidForBlock(block);
            if (fluid != null) {
                FluidStack stack = new FluidStack(fluid, Fluid.BUCKET_VOLUME);
                StrawUtils.getStrawHandler(stack)
                        .ifPresent(handler -> handler.onDrink(world, pos, stack, player, false));
                world.setBlockToAir(pos);
                return heldStack;
            } else if (block.hasTileEntity(state)) {
                TileEntity tile = world.getTileEntity(pos);
                if (tile != null) {
                    if (tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null)) {
                        IFluidHandler handler = tile
                                .getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
                        IFluidTankProperties[] fluidTankProperties = handler.getTankProperties();
                        for (IFluidTankProperties properties : fluidTankProperties) {
                            FluidStack stack = properties.getContents();
                            if (stack != null) {
                                fluid = stack.getFluid();
                                if (fluid != null && stack.amount >= Fluid.BUCKET_VOLUME) {
                                    FluidStack copiedStack = stack.copy();
                                    copiedStack.amount = Fluid.BUCKET_VOLUME;
                                    FluidStack out = handler.drain(copiedStack, false);
                                    if (out != null && out.amount == 1000) {
                                        StrawUtils.getStrawHandler(stack).ifPresent(strawHandler -> strawHandler
                                                .onDrink(world, pos, stack, player, true));
                                        handler.drain(copiedStack, true);
                                        return heldStack;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.onItemUseFinish(heldStack, world, entity);
}

From source file:com.buuz135.industrial.proxy.block.upgrade.ConveyorExtractionUpgrade.java

License:Open Source License

@Override
public void update() {
    if (getWorld().isRemote)
        return;//from w  w  w. ja  v a 2s. co m
    items.removeIf(entityItem -> entityItem.getItem().isEmpty() || entityItem.isDead);
    if (items.size() >= 20)
        return;
    if (getWorld().getTotalWorldTime() % (fast ? 10 : 20) == 0) {
        IItemHandler itemHandler = getHandlerCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
        if (itemHandler != null) {
            for (int i = 0; i < itemHandler.getSlots(); i++) {
                ItemStack stack = itemHandler.extractItem(i, 4, true);
                if (stack.isEmpty() || whitelist != filter.matches(stack))
                    continue;
                EntityItem item = new EntityItem(getWorld(), getPos().getX() + 0.5, getPos().getY() + 0.5,
                        getPos().getZ() + 0.5);
                item.motionX = 0;
                item.motionY = 0;
                item.motionZ = 0;
                item.setPickupDelay(40);
                item.setItem(itemHandler.extractItem(i, 4, false));
                if (getWorld().spawnEntity(item)) {
                    items.add(item);
                }
                break;
            }
        }
    }
    if (getContainer() instanceof TileEntityConveyor) {
        IFluidTank tank = ((TileEntityConveyor) getContainer()).getTank();
        IFluidHandler fluidHandler = getHandlerCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY);
        if (fluidHandler != null && fluidHandler.drain(250, false) != null
                && ((FluidTank) tank).canFillFluidType(fluidHandler.drain(250, false))
                && whitelist == filter.matches(fluidHandler.drain(250, false))) {
            FluidStack drain = fluidHandler.drain(tank.fill(fluidHandler.drain(250, false), true), true);
            if (drain != null && drain.amount > 0)
                getContainer().requestFluidSync();
        }
    }
}

From source file:com.buuz135.industrial.tile.world.FluidPumpTile.java

License:Open Source License

@Override
public float work() {
    if (WorkUtils.isDisabled(this.getBlockType()) || isWorkFinished || fluid == null)
        return 0;
    if (tank.getFluidAmount() + 1000 <= tank.getCapacity()) {
        if (lowestY == this.pos.getY()) {
            isWorkFinished = true;/*from w ww.  ja v  a  2s.  com*/
            allBlocks = null;
            return 0;
        }
        if (allBlocks == null || allBlocks.isEmpty()) {
            fillCache();
        }
        BlockPos peeked = allBlocks.peek();
        while (!allBlocks.isEmpty() && (this.world.isOutsideBuildHeight(peeked) || !isBlockSameFluid(peeked)
                || this.world.getBlockState(peeked).getBlock()
                        .getMetaFromState(this.world.getBlockState(peeked)) != 0)) {
            allBlocks.poll();
            if (!allBlocks.isEmpty())
                peeked = allBlocks.peek();
        }
        if (peeked == null)
            return 0;
        if (this.world.getTileEntity(peeked) != null)
            return 0;
        IFluidHandler handler = FluidUtil.getFluidHandler(this.world, peeked, null);
        FluidStack fluid = handler.drain(1000, true);
        if (fluid != null) {
            if (BlockRegistry.fluidPumpBlock.isReplaceFluidWithCobble()) {
                if (this.world.setBlockState(peeked, Blocks.COBBLESTONE.getDefaultState())) {
                    tank.fill(fluid, true);
                }
            } else if (world.setBlockToAir(peeked)) {
                tank.fill(fluid, true);
            }
        } else {
            return 0;
        }
        allBlocks.poll();
        return 1;
    }
    return 0;
}

From source file:com.lothrazar.cyclicmagic.util.UtilFluid.java

License:Open Source License

/**
 * Look for a fluid handler with gien position and direction try to extract from that pos and fill the tank
 * // w  w w .ja va  2 s.co m
 * 
 * @param world
 * @param posSide
 * @param sideOpp
 * @param tankTo
 * @param amount
 * @return
 */
public static boolean tryFillTankFromPosition(World world, BlockPos posSide, EnumFacing sideOpp,
        FluidTank tankTo, final int amount) {
    try {
        IFluidHandler fluidFrom = FluidUtil.getFluidHandler(world, posSide, sideOpp);
        if (fluidFrom != null) {
            //its not my facing dir
            // SO: pull fluid from that into myself
            FluidStack wasDrained = fluidFrom.drain(amount, false);
            if (wasDrained == null) {
                return false;
            }
            int filled = tankTo.fill(wasDrained, false);
            if (wasDrained != null && wasDrained.amount > 0 && filled > 0) {
                int realAmt = Math.min(filled, wasDrained.amount);
                wasDrained = fluidFrom.drain(realAmt, true);
                if (wasDrained == null) {
                    return false;
                }
                return tankTo.fill(wasDrained, true) > 0;
            }
        }
        return false;
    } catch (Exception e) {
        ModCyclic.logger.error("External fluid block had an issue when we tried to drain", e);
        //charset crashes here i guess
        //https://github.com/PrinceOfAmber/Cyclic/issues/605
        // https://github.com/PrinceOfAmber/Cyclic/issues/605https://pastebin.com/YVtMYsF6
        return false;
    }
}

From source file:com.teambrmodding.neotech.common.tiles.machines.processors.TileAlloyer.java

License:Creative Commons License

/**
 * This will try to take things from other inventories and put it into ours
 *//*from ww w.j ava2  s .c om*/
@Override
public void tryInput() {
    for (EnumFacing dir : EnumFacing.values()) {
        if (!isDisabled(dir)) {
            if (world.getTileEntity(pos.offset(dir)) != null && world.getTileEntity(pos.offset(dir))
                    .hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, dir.getOpposite())) {
                IFluidHandler otherTank = world.getTileEntity(pos.offset(dir))
                        .getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, dir.getOpposite());

                // Attempt fill left tank
                if (canInputFromSide(dir, true)) {
                    // Try and match our fluid
                    if (tanks[INPUT_TANK_1].getFluid() != null
                            && otherTank.drain(tanks[INPUT_TANK_1].getFluid(), false) != null) {
                        int amount = tanks[INPUT_TANK_1].fill(otherTank.drain(1000, false), false);
                        if (amount > 0) {
                            tanks[INPUT_TANK_1].fill(otherTank.drain(amount, true), true);
                            markForUpdate(6);
                        }
                    }

                    // Check if we can accept other fluid
                    else if (tanks[INPUT_TANK_1].getFluid() == null && otherTank.drain(1000, false) != null) {
                        FluidStack drainedFluid = otherTank.drain(1000, false);
                        boolean hasExistingOtherFluid = tanks[INPUT_TANK_2].getFluid() != null;
                        int amount = tanks[INPUT_TANK_1].fill(drainedFluid, false);
                        if (amount > 0 && (!hasExistingOtherFluid
                                || RecipeManager.getHandler(RecipeManager.RecipeType.ALLOYER)
                                        .isValidInput(Pair.of(drainedFluid, tanks[INPUT_TANK_2].getFluid())))) {
                            tanks[INPUT_TANK_1].fill(otherTank.drain(amount, true), true);
                            markForUpdate(6);
                        }
                    }
                }

                // Right Tank
                if (canInputFromSide(dir, false)) {
                    // Try and match existing
                    if (tanks[INPUT_TANK_2].getFluid() != null
                            && otherTank.drain(tanks[INPUT_TANK_2].getFluid(), false) != null) {
                        int amount = tanks[INPUT_TANK_2].fill(otherTank.drain(1000, false), false);
                        if (amount > 0) {
                            tanks[INPUT_TANK_2].fill(otherTank.drain(amount, true), true);
                            markForUpdate(6);
                        }
                    }

                    // Check if applicable
                    else if (tanks[INPUT_TANK_2].getFluid() == null && otherTank.drain(1000, false) != null) {
                        FluidStack drainedStack = otherTank.drain(1000, false);
                        boolean hasExistingInput = tanks[INPUT_TANK_1].getFluid() != null;
                        int amount = tanks[INPUT_TANK_2].fill(drainedStack, false);
                        if (amount > 0 && (!hasExistingInput
                                || RecipeManager.getHandler(RecipeManager.RecipeType.ALLOYER)
                                        .isValidInput(Pair.of(drainedStack, tanks[INPUT_TANK_1].getFluid())))) {
                            tanks[INPUT_TANK_2].fill(otherTank.drain(amount, true), true);
                            markForUpdate(6);
                        }
                    }
                }
            }
        }
    }
}