Java Code Examples for net.minecraftforge.fluids.capability.IFluidHandlerItem#drain()
The following examples show how to use
net.minecraftforge.fluids.capability.IFluidHandlerItem#drain() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhantomFluidWidget.java From GregTech with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void handleClientAction(int id, PacketBuffer buffer) { if (id == 1) { ItemStack itemStack = gui.entityPlayer.inventory.getItemStack().copy(); if (!itemStack.isEmpty()) { itemStack.setCount(1); IFluidHandlerItem fluidHandler = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandler != null) { FluidStack resultFluid = fluidHandler.drain(Integer.MAX_VALUE, false); fluidStackUpdater.accept(resultFluid); } } else { fluidStackUpdater.accept(null); } } else if (id == 2) { FluidStack fluidStack; try { fluidStack = FluidStack.loadFluidStackFromNBT(buffer.readCompoundTag()); } catch (IOException e) { throw new RuntimeException(e); } fluidStackUpdater.accept(fluidStack); } }
Example 2
Source File: MetaItem.java From GregTech with GNU Lesser General Public License v3.0 | 6 votes |
@Override @SideOnly(Side.CLIENT) public String getItemStackDisplayName(ItemStack stack) { if (stack.getItemDamage() >= metaItemOffset) { T item = getItem(stack); if (item == null) { return "invalid item"; } String unlocalizedName = String.format("metaitem.%s.name", item.unlocalizedName); if (item.getNameProvider() != null) { return item.getNameProvider().getItemStackDisplayName(stack, unlocalizedName); } IFluidHandlerItem fluidHandlerItem = ItemHandlerHelper.copyStackWithSize(stack, 1) .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandlerItem != null) { FluidStack fluidInside = fluidHandlerItem.drain(Integer.MAX_VALUE, false); return I18n.format(unlocalizedName, fluidInside == null ? I18n.format("metaitem.fluid_cell.empty") : fluidInside.getLocalizedName()); } return I18n.format(unlocalizedName); } return super.getItemStackDisplayName(stack); }
Example 3
Source File: RecipeShapedFluid.java From Wizardry with GNU Lesser General Public License v3.0 | 6 votes |
@Override public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) { NonNullList<ItemStack> remains = ForgeHooks.defaultRecipeGetRemainingItems(inv); for (int i = 0; i < height * width; i++) { ItemStack stack = inv.getStackInSlot(i); NonNullList<Ingredient> matchedIngredients = this.input; if (matchedIngredients.get(i) instanceof IngredientFluidStack) { if (!stack.isEmpty()) { ItemStack copy = stack.copy(); copy.setCount(1); remains.set(i, copy); } IFluidHandlerItem handler = FluidUtil.getFluidHandler(remains.get(i)); if (handler != null) { FluidStack fluid = ((IngredientFluidStack) matchedIngredients.get(i)).getFluid(); handler.drain(fluid.amount, true); remains.set(i, handler.getContainer()); } } } return remains; }
Example 4
Source File: PhantomFluidWidget.java From GregTech with GNU Lesser General Public License v3.0 | 5 votes |
private FluidStack drainFrom(Object ingredient) { if (ingredient instanceof ItemStack) { ItemStack itemStack = (ItemStack) ingredient; IFluidHandlerItem fluidHandler = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandler != null) return fluidHandler.drain(Integer.MAX_VALUE, false); } return null; }
Example 5
Source File: DefaultSubItemHandler.java From GregTech with GNU Lesser General Public License v3.0 | 5 votes |
public static void addFluidContainerVariants(ItemStack itemStack, List<ItemStack> subItems) { for (Fluid fluid : FluidRegistry.getRegisteredFluids().values()) { ItemStack containerStack = itemStack.copy(); IFluidHandlerItem fluidContainer = containerStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidContainer != null) { fluidContainer.fill(new FluidStack(fluid, Integer.MAX_VALUE), true); if (fluidContainer.drain(Integer.MAX_VALUE, false) == null) continue; subItems.add(fluidContainer.getContainer()); } } }
Example 6
Source File: MetaItem.java From GregTech with GNU Lesser General Public License v3.0 | 5 votes |
@SideOnly(Side.CLIENT) protected int getColorForItemStack(ItemStack stack, int tintIndex) { T metaValueItem = getItem(stack); if (metaValueItem != null && metaValueItem.getColorProvider() != null) { return metaValueItem.getColorProvider().getItemStackColor(stack, tintIndex); } IFluidHandlerItem fluidContainerItem = ItemHandlerHelper.copyStackWithSize(stack, 1) .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (tintIndex == 0 && fluidContainerItem != null) { FluidStack fluidStack = fluidContainerItem.drain(Integer.MAX_VALUE, false); return fluidStack == null ? 0x666666 : RenderUtil.getFluidColor(fluidStack); } return 0xFFFFFF; }
Example 7
Source File: EntityFluidCow.java From Moo-Fluids with GNU General Public License v3.0 | 5 votes |
private boolean attemptToHealCowWithFluidContainer(final ItemStack currentItemStack, final EntityPlayer entityPlayer) { boolean cowHealed = false; if (!currentItemStack.isEmpty() && entityFluid != null) { IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(currentItemStack); if (fluidHandlerItem != null) { FluidStack containedFluid = fluidHandlerItem .drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false); ItemStack emptyItemStack; if (containedFluid != null && containedFluid.getFluid().getName().equalsIgnoreCase(entityFluid.getName())) { fluidHandlerItem.drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false); emptyItemStack = fluidHandlerItem.getContainer(); currentItemStack.shrink(1); if (currentItemStack.isEmpty()) { entityPlayer.inventory.setInventorySlotContents( entityPlayer.inventory.currentItem, emptyItemStack.copy()); } else { ItemHandlerHelper.giveItemToPlayer(entityPlayer, emptyItemStack.copy()); } heal(4F); cowHealed = true; } } } return cowHealed; }
Example 8
Source File: ContainerBucketFillHandler.java From OpenModsLib with MIT License | 5 votes |
@SubscribeEvent public void onBucketFill(FillBucketEvent evt) { if (evt.getResult() != Result.DEFAULT) return; if (evt.getEmptyBucket().getItem() != EMPTY_BUCKET) return; final RayTraceResult target = evt.getTarget(); if (target == null || target.typeOfHit != RayTraceResult.Type.BLOCK) return; final TileEntity te = evt.getWorld().getTileEntity(target.getBlockPos()); if (te == null) return; if (!canFill(evt.getWorld(), target.getBlockPos(), te)) { return; } if (te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit)) { final IFluidHandler source = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit); final FluidStack fluidInContainer = source.drain(Fluid.BUCKET_VOLUME, false); if (fluidInContainer != null) { final ItemStack filledBucket = getFilledBucket(fluidInContainer); if (!filledBucket.isEmpty()) { final IFluidHandlerItem container = FluidUtil.getFluidHandler(filledBucket); if (container != null) { final FluidStack fluidInBucket = container.drain(Integer.MAX_VALUE, false); if (fluidInBucket != null && fluidInBucket.isFluidStackIdentical(source.drain(fluidInBucket, false))) { source.drain(fluidInBucket, true); evt.setFilledBucket(filledBucket.copy()); evt.setResult(Result.ALLOW); } } } } } }
Example 9
Source File: RecipeMapFluidCanner.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
@Override @Nullable public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs, int outputFluidTankCapacity) { Recipe recipe = super.findRecipe(voltage, inputs, fluidInputs, outputFluidTankCapacity); if (inputs.size() == 0 || inputs.get(0).isEmpty() || recipe != null) return recipe; // Fail early if input isn't a fluid container if (!inputs.get(0).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) return null; // Make a copy to use for creating recipes ItemStack inputStack = inputs.get(0).copy(); inputStack.setCount(1); // Make another copy to use for draining and filling ItemStack fluidHandlerItemStack = inputStack.copy(); IFluidHandlerItem fluidHandlerItem = fluidHandlerItemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandlerItem == null) return null; FluidStack containerFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, true); if (containerFluid != null) { //if we actually drained something, then it's draining recipe return recipeBuilder() //we can reuse recipe as long as input container stack fully matches our one .inputs(new CountableIngredient(new NBTIngredient(inputStack), 1)) .outputs(fluidHandlerItem.getContainer()) .fluidOutputs(containerFluid) .duration(Math.max(16, containerFluid.amount / 64)).EUt(4) .build().getResult(); } //if we didn't drain anything, try filling container if (!fluidInputs.isEmpty() && fluidInputs.get(0) != null) { FluidStack inputFluid = fluidInputs.get(0).copy(); inputFluid.amount = fluidHandlerItem.fill(inputFluid, true); if (inputFluid.amount > 0) { return recipeBuilder() //we can reuse recipe as long as input container stack fully matches our one .inputs(new CountableIngredient(new NBTIngredient(inputStack), 1)) .fluidInputs(inputFluid) .outputs(fluidHandlerItem.getContainer()) .duration(Math.max(16, inputFluid.amount / 64)).EUt(4) .build().getResult(); } } return null; }
Example 10
Source File: GlassBottleFluidHandler.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
@Nullable @Override public FluidStack drain(FluidStack resource, boolean doDrain) { IFluidHandlerItem nextInChain = getNextInChain(); return nextInChain == null ? null : nextInChain.drain(resource, doDrain); }
Example 11
Source File: GlassBottleFluidHandler.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
@Nullable @Override public FluidStack drain(int maxDrain, boolean doDrain) { IFluidHandlerItem nextInChain = getNextInChain(); return nextInChain == null ? null : nextInChain.drain(maxDrain, doDrain); }
Example 12
Source File: EntityRocket.java From AdvancedRocketry with MIT License | 4 votes |
protected boolean interact(EntityPlayer player) { //Actual interact code needs to be moved to a packet receive on the server ItemStack heldItem = player.getHeldItem(EnumHand.MAIN_HAND); //Handle linkers and right-click with fuel if(heldItem != null) { float fuelMult; FluidStack fluidStack; if(heldItem.getItem() instanceof ItemLinker) { if(ItemLinker.isSet(heldItem)) { TileEntity tile = this.world.getTileEntity(ItemLinker.getMasterCoords(heldItem)); if(tile instanceof IInfrastructure) { IInfrastructure infrastructure = (IInfrastructure)tile; if(this.getDistance(ItemLinker.getMasterX(heldItem), this.posY, ItemLinker.getMasterZ(heldItem)) < infrastructure.getMaxLinkDistance() + Math.max(storage.getSizeX(), storage.getSizeZ())) { if(!connectedInfrastructure.contains(tile)) { linkInfrastructure(infrastructure); if(!world.isRemote) { player.sendMessage(new TextComponentString("Linked Sucessfully")); } ItemLinker.resetPosition(heldItem); return true; } else if(!world.isRemote) player.sendMessage(new TextComponentString("Already linked!")); } else if(!world.isRemote) player.sendMessage(new TextComponentString("The object you are trying to link is too far away")); } else if(!world.isRemote) player.sendMessage(new TextComponentString("This cannot be linked to a rocket!")); } else if(!world.isRemote) player.sendMessage(new TextComponentString("Nothing to be linked")); return false; } else if((FluidUtils.containsFluid(heldItem) && (fluidStack = FluidUtils.getFluidForItem(heldItem)) != null && (fuelMult = FuelRegistry.instance.getMultiplier(FuelType.LIQUID, fluidStack.getFluid())) > 0 )) { int amountToAdd = (int) (fuelMult*fluidStack.amount); this.addFuelAmount(amountToAdd); //if the player is not in creative then try to use the fluid container if(!player.capabilities.isCreativeMode) { heldItem = heldItem.copy(); heldItem.setCount(1); IFluidHandlerItem handler = FluidUtils.getFluidHandler(heldItem); handler.drain(fluidStack.amount, true); ItemStack emptyStack = handler.getContainer(); if(player.inventory.addItemStackToInventory(emptyStack)) { player.getHeldItem(EnumHand.MAIN_HAND).splitStack(1); if(player.getHeldItem(EnumHand.MAIN_HAND).isEmpty()) player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY); } } return true; } } //If player is holding shift open GUI if(player.isSneaking()) { openGui(player); } else if(stats.hasSeat()) { //If pilot seat is open mount entity there if(stats.hasSeat() && this.getPassengers().isEmpty()) { if(!world.isRemote) player.startRiding(this); } /*else if(stats.getNumPassengerSeats() > 0) { //If a passenger seat exists and one is empty, mount the player to it for(int i = 0; i < stats.getNumPassengerSeats(); i++) { if(this.mountedEntities[i] == null || this.mountedEntities[i].get() == null) { player.ridingEntity = this; this.mountedEntities[i] = new WeakReference<Entity>(player); break; } } }*/ } return true; }
Example 13
Source File: ItemSpaceChest.java From AdvancedRocketry with MIT License | 4 votes |
/** * Decrements air in the suit by amt * @param stack the item stack to operate on * @param amt amount of air by which to decrement * @return The amount of air extracted from the suit */ @Override public int decrementAir(ItemStack stack, int amt) { if(stack.hasTagCompound()) { EmbeddedInventory inv = new EmbeddedInventory(getNumSlots(stack)); inv.readFromNBT(stack.getTagCompound()); List<ItemStack> list = new LinkedList<ItemStack>(); for(int i = 0; i < inv.getSizeInventory(); i++) { if(!inv.getStackInSlot(i).isEmpty()) list.add(inv.getStackInSlot(i)); } int amtDrained = amt; for(ItemStack component : list) { if(component.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) { IFluidHandlerItem fluidItem = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP); FluidStack fluidStack = FluidUtils.getFluidForItem(component); FluidStack fluidDrained = null; if(fluidStack != null && FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen)) fluidDrained = fluidItem.drain(amtDrained, true); if(fluidDrained != null) amtDrained -= fluidDrained.amount; if(amtDrained == 0) break; } } saveEmbeddedInventory(stack, inv); return amt - amtDrained; } return 0; /*NBTTagCompound nbt; if(stack.hasTagCompound()) { nbt = stack.getTagCompound(); } else { nbt = new NBTTagCompound(); } int prevAmt = nbt.getInteger("air"); int newAmt = Math.max(prevAmt - amt,0); nbt.setInteger("air", newAmt); stack.setTagCompound(nbt); return prevAmt - newAmt;*/ }