Java Code Examples for net.minecraftforge.fluids.capability.IFluidHandlerItem#fill()

The following examples show how to use net.minecraftforge.fluids.capability.IFluidHandlerItem#fill() . 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: ItemFluidContainer.java    From customstuff4 with GNU General Public License v3.0 6 votes vote down vote up
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(@Nullable CreativeTabs tab, @Nonnull NonNullList<ItemStack> subItems)
{
    if (isInCreativeTab(tab))
    {
        subItems.add(new ItemStack(this));

        for (Fluid fluid : FluidRegistry.getRegisteredFluids().values())
        {
            if (!fluid.getName().equals("milk"))
            {
                // add all fluids that the bucket can be filled  with
                FluidStack fs = new FluidStack(fluid, content.capacity);
                ItemStack stack = new ItemStack(this);
                IFluidHandlerItem fluidHandler = new FluidHandlerItemStack(stack, content.capacity);
                if (fluidHandler.fill(fs, true) == fs.amount)
                {
                    ItemStack filled = fluidHandler.getContainer();
                    subItems.add(filled);
                }
            }
        }
    }
}
 
Example 2
Source File: DefaultSubItemHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 3
Source File: GlassBottleFluidHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int fill(FluidStack resource, boolean doFill) {
    if(resource == null || resource.amount <= 0) {
        return 0;
    }
    int myOwnResult = fillImpl(resource, doFill);
    if(myOwnResult == 0) {
        IFluidHandlerItem nextInChain = getNextInChain();
        return nextInChain == null ? 0 : nextInChain.fill(resource, doFill);
    }
    return 0;
}
 
Example 4
Source File: GTMaterialGen.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** How to get a GTFluidTube of any fluid by name **/
public static ItemStack getModdedTube(String name, int count) {
	FluidStack fluid = FluidRegistry.getFluidStack(name, 1000);
	ItemStack stack = new ItemStack(GTItems.testTube);
	IFluidHandlerItem handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
	handler.fill(fluid, true);
	stack.setCount(count);
	return stack;
}
 
Example 5
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToGetFluidFromCow(final ItemStack currentItemStack,
                                         final EntityPlayer entityPlayer) {
  boolean canGetFluid = false;

  if (!currentItemStack.isEmpty() && entityFluid != null) {
    ItemStack filledItemStack = ItemHandlerHelper.copyStackWithSize(currentItemStack, 1);
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(filledItemStack);
    if (fluidHandlerItem != null) {
      if (fluidHandlerItem.fill(
              new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), true) == Fluid.BUCKET_VOLUME) {

        filledItemStack = fluidHandlerItem.getContainer();

        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
              entityPlayer.inventory.currentItem,
              filledItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, filledItemStack.copy());
        }

        canGetFluid = true;
      }
    }
  }

  return canGetFluid;
}
 
Example 6
Source File: RecipeMapFluidCanner.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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 7
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
/**
 * Increments 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 inserted into the suit
 */
@Override
public int increment(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()) {
				
				if( i < 2 && inv.getStackInSlot(i).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
					list.add(inv.getStackInSlot(i));
				}
				else if(FluidUtils.containsFluid(inv.getStackInSlot(i))) {
					
					FluidStack fstack = FluidUtils.getFluidForItem(inv.getStackInSlot(i));
					if(fstack != null && FluidUtils.areFluidsSameType(fstack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
						list.add(inv.getStackInSlot(i));
				}
				
			}
		}


		int amtDrained = amt;
		//At this point the list contains ONLY capable items
		for(ItemStack component : list) {
				IFluidHandlerItem fHandler = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
				FluidStack fluidStack = fHandler.getTankProperties()[0].getContents();

				if(fluidStack == null || FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
					amtDrained -= fHandler.fill(new FluidStack(AdvancedRocketryFluids.fluidOxygen, amtDrained), true);

				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.min(prevAmt + amt, getMaxAir());
	nbt.setInteger("air", newAmt);
	stack.setTagCompound(nbt);

	return newAmt - prevAmt;*/
}