List of usage examples for net.minecraftforge.fluids.capability IFluidHandlerItem fill
int fill(FluidStack resource, FluidAction action);
From source file:blusunrize.immersiveengineering.common.crafting.RecipeJerrycan.java
@Nonnull @Override//from w ww . j av a 2 s .com public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) { ItemStack jerrycan = ItemStack.EMPTY; ItemStack container = ItemStack.EMPTY; FluidStack fs = null; int[] slots = getRelevantSlots(inv); if (slots[0] >= 0) { jerrycan = inv.getStackInSlot(slots[0]); fs = FluidUtil.getFluidContained(jerrycan); } if (slots[1] >= 0) container = inv.getStackInSlot(slots[1]); if (fs != null && !container.isEmpty()) { ItemStack newContainer = Utils.copyStackWithAmount(container, 1); IFluidHandlerItem handler = FluidUtil.getFluidHandler(newContainer); int accepted = handler.fill(fs, false); if (accepted > 0) { handler.fill(fs, true); newContainer = handler.getContainer();// Because buckets are silly // FluidUtil.getFluidHandler(jerrycan).drain(accepted,true); ItemNBTHelper.setInt(jerrycan, "jerrycanDrain", accepted); } return newContainer; } return ItemStack.EMPTY; }
From source file:buildcraft.lib.fluids.Tank.java
License:Mozilla Public License
public void onGuiClicked(ContainerBC_Neptune container) { EntityPlayer player = container.player; ItemStack held = player.inventory.getItemStack(); if (held.isEmpty()) { return;/*from w w w . ja va 2 s . c o m*/ } // first try to fill this tank from the item boolean hasFilled = false; ItemStack copy = held.copy(); copy.setCount(1); int space = capacity - getFluidAmount(); boolean isCreative = player.capabilities.isCreativeMode; boolean isSurvival = !isCreative; FluidGetResult result = map(copy, space); if (result != null && result.fluidStack != null && result.fluidStack.amount > 0) { if (isCreative) { held = copy;// so we don't change the stack held by the player. } int potential = held.getCount(); // Insert a single item until a fluid was not accepted. for (int p = 0; p < potential; p++) { int accepted = fill(result.fluidStack, false); if (isCreative ? (accepted > 0) : (accepted == result.fluidStack.amount)) { hasFilled = true; int reallyAccepted = fill(result.fluidStack, true); if (reallyAccepted != accepted) { throw new IllegalStateException("We seem to be buggy! (accepted = " + accepted + ", reallyAccepted = " + reallyAccepted + ")"); } held.shrink(1); if (isSurvival) { if (held.isEmpty()) { held = result.itemStack; break; } else if (!result.itemStack.isEmpty()) { player.inventory.addItemStackToInventory(result.itemStack); player.inventoryContainer.detectAndSendChanges(); } else { continue; } } else if (held.isEmpty()) { break; } } else { break; } } if (isSurvival) { player.inventory.setItemStack(held.isEmpty() ? StackUtil.EMPTY : held); ((EntityPlayerMP) player).updateHeldItem(); } if (hasFilled) { FluidStack fl = getFluid(); if (fl != null) { SoundEvent sound = fl.getFluid().getEmptySound(container.player.world, container.player.getPosition()); container.player.world.playSound(null, player.getPosition(), sound, SoundCategory.BLOCKS, 1, 1); } return; } } // Now try to drain the fluid into the item IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(held.copy()); if (fluidHandler == null) return; FluidStack drained = drain(capacity, false); if (drained == null || drained.amount <= 0) return; int filled = fluidHandler.fill(drained, true); if (filled > 0) { FluidStack reallyDrained = drain(filled, true); if ((reallyDrained == null || reallyDrained.amount != filled)) { throw new IllegalStateException("Somehow drained differently than expected! ( drained = "// + drained + ", filled = " + filled + ", reallyDrained = " + reallyDrained + " )"); } if (isSurvival) { ItemStack filledContainer = fluidHandler.getContainer(); player.inventory.setItemStack(filledContainer); ((EntityPlayerMP) player).updateHeldItem(); } SoundEvent sound = reallyDrained.getFluid().getFillSound(container.player.world, container.player.getPosition()); container.player.world.playSound(null, player.getPosition(), sound, SoundCategory.BLOCKS, 1, 1); } }