List of usage examples for net.minecraftforge.fluids FluidUtil tryEmptyContainer
@Nonnull public static FluidActionResult tryEmptyContainer(@Nonnull ItemStack container, IFluidHandler fluidDestination, int maxAmount, @Nullable PlayerEntity player, boolean doDrain)
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 w w w.j ava2 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 va 2 s . c o 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.elytradev.thermionics.transport.FluidTransport.java
License:Open Source License
public static FluidActionResult tryEmptyOrFillContainer(ItemStack stack, IFluidHandler handler, int maxTransfer, EntityPlayer player) {//from w ww . java 2 s . c o m FluidActionResult result = FluidUtil.tryEmptyContainer(stack, handler, maxTransfer, player, true); if (result.success) { return result; } else { FluidActionResult resultFill = FluidUtil.tryFillContainer(stack, handler, maxTransfer, player, true); return resultFill; } }
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:hellfirepvp.astralsorcery.common.util.ItemUtils.java
License:Open Source License
public static FluidActionResult drainFluidFromItem(ItemStack stack, FluidStack fluidStack, boolean doDrain) { return FluidUtil.tryEmptyContainer(stack, FluidHandlerVoid.INSTANCE, fluidStack.amount, null, doDrain); }
From source file:mod.rankshank.arbitraria.common.recipes.RecipeSprayBottleFiller.java
@Override @SuppressWarnings("ConstantConditions") public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) { ItemStack[] remnants = new ItemStack[inv.getSizeInventory()]; if (isLiquid) { FluidActionResult flar = FluidUtil.tryEmptyContainer(source, new FluidHandlerBottomless(bottles * 1000), bottles * 1000, null, true); remnants[sourceIndex] = flar.getResult(); for (int index : indices) { //set other containers ItemStack add = inv.getStackInSlot(index).copy(); boolean replace = add.getCount() == 1; add.setCount(1);//from www.j a v a2s . c o m add.getCapability(ArbitraryCaps.SPRAY_BOTTLE_CAPABILITY, null).fillWith(fluid.getFluid()); if (replace) remnants[index] = add; else { remnants[empty.get(0)] = add; empty.remove(0); } } } else if (source.getItem() instanceof ItemPotion) { //Since ItemPotion doesn't have a container Item remnants[sourceIndex] = new ItemStack(Items.GLASS_BOTTLE); } else remnants[sourceIndex] = source.getItem().getContainerItem(source); NonNullList<ItemStack> ret = NonNullList.create(); for (ItemStack remnant : remnants) ret.add(remnant == null ? ItemStack.EMPTY : remnant.isEmpty() ? ItemStack.EMPTY : remnant); return ret; }
From source file:therogue.storehouse.tile.machine.TileCrystaliser.java
License:Open Source License
@Override public void update() { if (GeneralUtils.isServerSide(world)) { IItemHandler internalView = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null, ModuleContext.INTERNAL); if (!internalView.getStackInSlot(2).isEmpty()) { ItemStack tankItem = internalView.extractItem(2, -1, true); ItemStack outputSlot = internalView.getStackInSlot(3); ItemStack result = FluidUtil.tryEmptyContainer(tankItem, tank, tank.getCapacity() - tank.getFluidAmount(), null, false).result; if (ItemStackUtils.areStacksMergableWithLimit(internalView.getSlotLimit(3), result, outputSlot)) { internalView.extractItem(2, -1, false); internalView.insertItem(3, ItemStackUtils.mergeStacks(internalView.getSlotLimit(3), true, internalView.extractItem(3, -1, false), FluidUtil.tryEmptyContainer(tankItem, tank, tank.getCapacity() - tank.getFluidAmount(), null, true).result), false);/*from w w w. j ava 2 s. c o m*/ } } } }
From source file:therogue.storehouse.tile.machine.TileLiquidGenerator.java
License:Open Source License
@Override public void update() { super.update(); if (GeneralUtils.isServerSide(world)) { IItemHandler internalView = inventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null, ModuleContext.INTERNAL); if (!internalView.getStackInSlot(2).isEmpty()) { ItemStack tankItem = internalView.extractItem(2, -1, true); ItemStack outputSlot = internalView.getStackInSlot(3); ItemStack result = FluidUtil.tryEmptyContainer(tankItem, tank, tank.getCapacity() - tank.getFluidAmount(), null, false).result; if (ItemStackUtils.areStacksMergableWithLimit(internalView.getSlotLimit(3), result, outputSlot)) { internalView.extractItem(2, -1, false); internalView.insertItem(3, ItemStackUtils.mergeStacks(internalView.getSlotLimit(3), true, internalView.extractItem(3, -1, false), FluidUtil.tryEmptyContainer(tankItem, tank, tank.getCapacity() - tank.getFluidAmount(), null, true).result), false);//w ww .ja v a2s. co m } } } }