Example usage for net.minecraftforge.fluids FluidActionResult getResult

List of usage examples for net.minecraftforge.fluids FluidActionResult getResult

Introduction

In this page you can find the example usage for net.minecraftforge.fluids FluidActionResult getResult.

Prototype

@Nonnull
    public ItemStack getResult() 

Source Link

Usage

From source file:com.buuz135.industrial.utils.ItemStackUtils.java

License:Open Source License

public static void fillItemFromTank(ItemStackHandler fluidItems, IFluidTank tank) {
    if (tank.getFluid() == null)
        return;//from w w  w.j  a  v a 2s.  co m
    ItemStack stack = fluidItems.getStackInSlot(0).copy();
    if (!stack.isEmpty()) {
        if (stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            FluidActionResult result = FluidUtil.tryFillContainer(stack, (IFluidHandler) tank,
                    tank.getCapacity(), null, false);
            if (result.isSuccess() && (fluidItems.getStackInSlot(1).isEmpty()
                    || (ItemHandlerHelper.canItemStacksStack(result.getResult(), fluidItems.getStackInSlot(1))
                            && result.getResult().getCount() + fluidItems.getStackInSlot(1).getCount() <= result
                                    .getResult().getMaxStackSize()))) {
                result = FluidUtil.tryFillContainer(stack, (IFluidHandler) tank, tank.getCapacity(), null,
                        true);
                if (fluidItems.getStackInSlot(1).isEmpty()) {
                    fluidItems.setStackInSlot(1, result.getResult());
                } else {
                    fluidItems.getStackInSlot(1).grow(1);
                }
                fluidItems.getStackInSlot(0).shrink(1);
            }
        }
    }
}

From source file:com.buuz135.industrial.utils.ItemStackUtils.java

License:Open Source License

public static void fillTankFromItem(ItemStackHandler fluidItems, IFluidTank tank) {
    ItemStack stack = fluidItems.getStackInSlot(0).copy();
    if (!stack.isEmpty()) {
        if (stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            IFluidHandlerItem cap = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY,
                    null);/*from  www.j  a  v  a 2 s.  co  m*/
            FluidStack fluidStack = cap.drain(1000, false);
            if (fluidStack != null && tank.fill(fluidStack, false) == 0)
                return;
            FluidActionResult result = FluidUtil.tryEmptyContainer(stack, (IFluidHandler) tank, 1000, null,
                    false);
            if (result.isSuccess() && (fluidItems.getStackInSlot(1).isEmpty()
                    || (ItemHandlerHelper.canItemStacksStack(result.getResult(), fluidItems.getStackInSlot(1))
                            && result.getResult().getCount() + fluidItems.getStackInSlot(1).getCount() <= result
                                    .getResult().getMaxStackSize()))) {
                result = FluidUtil.tryEmptyContainer(stack, (IFluidHandler) tank, tank.getCapacity(), null,
                        true);
                if (fluidItems.getStackInSlot(1).isEmpty()) {
                    fluidItems.setStackInSlot(1, result.getResult());
                } else {
                    fluidItems.getStackInSlot(1).grow(1);
                }
                fluidItems.getStackInSlot(0).shrink(1);
            }
        }
    }
}

From source file:com.elytradev.thermionics.tileentity.TileEntityPotStill.java

License:Open Source License

@Override
public void update() {
    boolean curTickPower = world.isBlockIndirectlyGettingPowered(pos) != 0;

    //TODO: Actually lock tanks

    { //Bottle fluids out
        ItemStack bottles = itemStorage.getStackInSlot(SLOT_EMPTY_BUCKET_IN);
        ItemStack outputItem = itemStorage.getStackInSlot(SLOT_FULL_BUCKET_OUT);
        FluidStack outputFluid = outputTank.getFluid();
        //Are there bottles to fill, is there room to put them, is there fluid to fill them with, and is there *enough* of that fluid?
        if (!bottles.isEmpty() && outputItem.getCount() < outputItem.getMaxStackSize() && outputFluid != null
                && outputFluid.amount > 250) {
            ItemStack outBottle = new ItemStack(ThermionicsItems.SPIRIT_BOTTLE);
            outBottle.setTagCompound(outputFluid.tag.copy());

            if (itemStorage.insertItem(SLOT_FULL_BUCKET_OUT, outBottle, true).isEmpty()) {
                outputTank.drainInternal(250, true);
                itemStorage.insertItem(SLOT_FULL_BUCKET_OUT, outBottle, false);
                itemStorage.extractItem(SLOT_EMPTY_BUCKET_IN, 1, false);
            }/*from   ww w  .j a  v a2s . co m*/
        }
    }

    if (!tanksLocked) {
        if (curTickPower & !lastTickPower) {
            //Lock the tanks on a rising current edge.
            setTanksLocked(true);
            processTime = 0;
        } else {
            //Fluid loading/unloading mode
            ItemStack inBucket = itemStorage.getStackInSlot(SLOT_FULL_BUCKET_IN);
            FluidActionResult result = FluidUtil.tryEmptyContainer(inBucket, inputTank, inputTank.getCapacity(),
                    null, true);
            if (result.isSuccess()) {
                itemStorage.setStackInSlot(SLOT_FULL_BUCKET_IN, ItemStack.EMPTY);
                itemStorage.setStackInSlot(SLOT_EMPTY_BUCKET_OUT, result.getResult());
            }
        }
    } else {
        if (processTime < MAX_PROCESS_TIME)
            processTime++;
        FluidStack in = inputTank.getFluid();
        if (in == null) {
            //Batch is done...?
            setTanksLocked(false);
        } else {
            //Find a recipe, let's go :D
            //For the moment, use Water->Rum
            PotStillRecipe recipe = MachineRecipes.getPotStill(inputTank);
            if (recipe != null) {
                FluidStack output = recipe.getOutput();
                int filled = outputTank.fillInternal(output, false);
                int extracted = heat.extractHeat(HEAT_REQUIRED, true);
                if (output.amount == filled && extracted == HEAT_REQUIRED) {
                    recipe.consumeIngredients(inputTank);
                    outputTank.fillInternal(output, true);
                    heat.extractHeat(HEAT_REQUIRED, false);
                }

            }
        }
    }

    lastTickPower = curTickPower;
}

From source file:com.lothrazar.cyclicmagic.block.autouser.TileEntityUser.java

License:Open Source License

/**
 * Pickup or place fluid in the world/*from  w ww .  java  2 s.  c  o  m*/
 * 
 * @param targetPos
 * @return
 */
private boolean rightClickFluidAir(BlockPos targetPos) {
    FakePlayer player = fakePlayer.get();
    ItemStack playerHeld = player.getHeldItemMainhand();
    //item stack does not hve fluid handler
    //dispense stack so either pickup or place liquid
    if (UtilFluid.isEmptyOfFluid(playerHeld)) {
        FluidActionResult res = UtilFluid.fillContainer(world, targetPos, playerHeld, this.getCurrentFacing());
        if (res != FluidActionResult.FAILURE) {
            player.setHeldItem(EnumHand.MAIN_HAND, res.getResult());
            this.tryDumpFakePlayerInvo(true);
            //        UtilItemStack.dropItemStackInWorld(world, getCurrentFacingPos(), res.getResult());
            return true;
        }
    } else {
        ItemStack drainedStackOrNull = UtilFluid.dumpContainer(world, targetPos, playerHeld);
        if (!drainedStackOrNull.isItemEqual(playerHeld)) {
            player.setHeldItem(EnumHand.MAIN_HAND, drainedStackOrNull);
            this.tryDumpFakePlayerInvo(true);
            return true;
        }
    }
    return false;
}

From source file:com.teambrmodding.neotech.common.blocks.machines.BlockMachine.java

License:Creative Commons License

/**
 * Called when the block is clicked on/*from w  w w .  java  2 s.  c  o  m*/
 * @return True to prevent future logic
 */
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    // Make sure our machine is reachable
    if (worldIn.getTileEntity(pos) != null && worldIn.getTileEntity(pos) instanceof AbstractMachine) {
        AbstractMachine machine = (AbstractMachine) worldIn.getTileEntity(pos);

        // First interact with fluid handlers
        if (machine.isFluidHandler()) {
            IFluidHandler fluidHandler = machine.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY,
                    null);
            FluidActionResult result = FluidUtil.interactWithFluidHandler(playerIn.getHeldItem(hand),
                    fluidHandler, playerIn);
            if (result.isSuccess()) {
                playerIn.setHeldItem(hand, result.getResult());
                return true;
            }
        }

        // Open a GUI
        if (!playerIn.isSneaking()) {
            playerIn.openGui(Bookshelf.INSTANCE, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
            return true;
        }
    }
    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}

From source file:com.teambrmodding.neotech.common.blocks.storage.BlockFluidStorage.java

License:Creative Commons License

/**
 * Called when the block is clicked on/*from  ww w.j av a2s.  c o  m*/
 * @return True to prevent future logic
 */
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    // Make sure our storage is reachable
    if (worldIn.getTileEntity(pos) != null && worldIn.getTileEntity(pos) instanceof TileBasicTank) {
        TileBasicTank fluidStorage = (TileBasicTank) worldIn.getTileEntity(pos);

        // First interact with fluid handlers
        IFluidHandler fluidHandler = fluidStorage.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY,
                null);
        FluidActionResult result = FluidUtil.interactWithFluidHandler(playerIn.getHeldItem(hand), fluidHandler,
                playerIn);
        if (result.isSuccess()) {
            playerIn.setHeldItem(hand, result.getResult());
            return true;
        }

        // We want to display in chat the current levels
        if (worldIn.isRemote && fluidStorage.tanks[TileBasicTank.TANK].getFluid() != null) {
            FluidStack fluidStack = fluidStorage.tanks[TileBasicTank.TANK].getFluid();
            if (fluidStack.getFluid() != null) {
                String display = fluidStack.getLocalizedName() + ": "
                        + ClientUtils.formatNumber(fluidStorage.tanks[TileBasicTank.TANK].getFluidAmount())
                        + " / "
                        + ClientUtils.formatNumber(fluidStorage.tanks[TileBasicTank.TANK].getCapacity());
                playerIn.sendMessage(new TextComponentString(display));
            }
        }
    }
    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}

From source file:com.teambrmodding.neotech.common.tiles.machines.generators.TileFluidGenerator.java

License:Creative Commons License

/**
 * Called per tick to manage burn time. You can do nothing here if there is nothing to generate. You should decrease burn time here
 * You should be handling checks if burnTime is 0 in this method, otherwise the tile won't know what to do
 *
 * @return True if able to continue generating
 *//*from   ww  w  . j  a v a  2 s . co  m*/
@Override
public boolean manageBurnTime() {
    if (tanks == null || tanks.length <= 0 || tanks[TANK] == null)
        return false;

    // Handle Items
    ItemStack stackToDrain = getStackInSlot(INPUT_SLOT);
    if (stackToDrain.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
        FluidActionResult result = FluidUtil.tryEmptyContainer(stackToDrain, this,
                tanks[TANK].getCapacity() - tanks[TANK].getFluidAmount(), null, true);

        if (result.isSuccess() && FluidUtil.getFluidContained(result.getResult()) == null) {
            if (getStackInSlot(OUTPUT_SLOT).isEmpty()) {
                setStackInSlot(OUTPUT_SLOT, result.getResult());
                setStackInSlot(INPUT_SLOT, ItemStack.EMPTY);
            } else if (InventoryUtils.canStacksMerge(result.getResult(), getStackInSlot(OUTPUT_SLOT))) {
                InventoryUtils.tryMergeStacks(result.getResult(), getStackInSlot(OUTPUT_SLOT));
                if (getStackInSlot(INPUT_SLOT).getCount() <= 0)
                    setStackInSlot(INPUT_SLOT, ItemStack.EMPTY);
            }
            markForUpdate(6);
        } else if (result.isSuccess()) {
            setStackInSlot(INPUT_SLOT, result.getResult());
            markForUpdate(6);
        }
    }

    // Do burnTime
    if (energyStorage.getEnergyStored() < energyStorage.getMaxEnergyStored() && burnTime <= 1) {
        FluidStack fluidDrained = tanks[TANK].drain(100, false);

        FluidFuelRecipe recipe = ((FluidFuelRecipeHandler) RecipeManager
                .getHandler(RecipeManager.RecipeType.FLUID_FUELS)).getRecipe(fluidDrained);
        if (recipe == null)
            return false;
        Pair<Integer, Integer> output = recipe
                .getOutput(AbstractRecipe.getFluidStackFromString(recipe.fluidStackInput));
        if (output == null)
            return false;

        fluidDrained = tanks[TANK].drain(AbstractRecipe.getFluidStackFromString(recipe.fluidStackInput).amount,
                false);

        if (fluidDrained == null || fluidDrained.getFluid() == null || fluidDrained.amount <= 0)
            return false;

        else
            drain(fluidDrained.amount, true);

        burnTime = output.getLeft();
        currentObjectBurnRate = output.getRight()
                + (output.getRight() * (getModifierForCategory(IUpgradeItem.ENUM_UPGRADE_CATEGORY.CPU) / 8));

        if (burnTime > 0) {
            currentObjectBurnTime = burnTime;
            return true;
        }
    }
    burnTime -= Math.max(1, getModifierForCategory(IUpgradeItem.ENUM_UPGRADE_CATEGORY.MEMORY)
            - (getModifierForCategory(IUpgradeItem.ENUM_UPGRADE_CATEGORY.CPU) / 2));
    return burnTime > 0;
}

From source file:de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase.java

protected boolean tryUseItemOnTank(EntityPlayer player, EnumHand hand, FluidTank tank) {
    ItemStack heldItem = player.getHeldItem(hand);

    if (StackUtil.isValid(heldItem)) {
        FluidActionResult result = FluidUtil.interactWithFluidHandler(heldItem, tank, player);
        if (result.isSuccess()) {
            player.setHeldItem(hand, result.getResult());
            return true;
        }//  w w w. jav a  2s  .com
    }

    return false;
}

From source file:hellfirepvp.astralsorcery.common.block.network.BlockWell.java

License:Open Source License

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {

        ItemStack heldItem = playerIn.getHeldItem(hand);
        if (!heldItem.isEmpty() && playerIn instanceof EntityPlayerMP) {
            TileWell tw = MiscUtils.getTileAt(worldIn, pos, TileWell.class, false);
            if (tw == null)
                return false;

            WellLiquefaction.LiquefactionEntry entry = WellLiquefaction.getLiquefactionEntry(heldItem);
            if (entry != null) {
                ItemStackHandler handle = tw.getInventoryHandler();
                if (!handle.getStackInSlot(0).isEmpty())
                    return false;

                if (!worldIn.isAirBlock(pos.up())) {
                    return false;
                }/*from  w w  w .  ja  v a 2  s  .c  om*/

                handle.setStackInSlot(0, ItemUtils.copyStackWithSize(heldItem, 1));
                worldIn.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP,
                        SoundCategory.PLAYERS, 0.2F,
                        ((worldIn.rand.nextFloat() - worldIn.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);

                if (!MiscUtils.isPlayerFakeMP((EntityPlayerMP) playerIn)
                        && entry.producing instanceof FluidLiquidStarlight) {
                    //Lets assume it starts collecting right away...
                    playerIn.addStat(RegistryAchievements.achvLiqStarlight);
                }

                if (!playerIn.isCreative()) {
                    heldItem.shrink(1);
                }
                if (heldItem.getCount() <= 0) {
                    playerIn.setHeldItem(hand, ItemStack.EMPTY);
                }
            }

            FluidActionResult far = FluidUtil.tryFillContainerAndStow(heldItem,
                    tw.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN),
                    new InvWrapper(playerIn.inventory), 1000, playerIn);
            if (far.isSuccess()) {
                playerIn.setHeldItem(hand, far.getResult());
                SoundHelper.playSoundAround(SoundEvents.ITEM_BUCKET_FILL, worldIn, pos, 1F, 1F);
                tw.markForUpdate();
            }
        }
    }
    return true;
}

From source file:hellfirepvp.astralsorcery.common.crafting.altar.AbstractAltarRecipe.java

License:Open Source License

public void handleItemConsumption(TileAltar ta, ShapedRecipeSlot slot) {
    ItemHandle handle = recipe.getExpectedStackHandle(slot);
    if (handle == null)
        return;/*from w  w w .jav  a 2s  . c  o  m*/

    TileReceiverBaseInventory.ItemHandlerTile inventory = ta.getInventoryHandler();
    ItemStack stack = inventory.getStackInSlot(slot.getSlotID());
    if (!stack.isEmpty()) {
        FluidActionResult fas = ItemUtils.drainFluidFromItem(stack, handle.getFluidTypeAndAmount(), true);
        if (fas.isSuccess()) {
            inventory.setStackInSlot(slot.getSlotID(), fas.getResult());
        }
    }
}