Search in sources :

Example 6 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class FluidUtil method getFluidContained.

/**
     * Helper method to get the fluid contained in an itemStack
     */
@Nullable
public static FluidStack getFluidContained(@Nonnull ItemStack container) {
    if (!container.isEmpty()) {
        container = container.copy();
        container.setCount(1);
        IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(container);
        if (fluidHandler != null) {
            return fluidHandler.drain(Integer.MAX_VALUE, false);
        }
    }
    return null;
}
Also used : IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) Nullable(javax.annotation.Nullable)

Example 7 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class FluidHandlerTest method testFluidContainer.

private static void testFluidContainer(ItemStack stack) {
    ItemStack originalStack = stack.copy();
    ItemStack preDrainStack = stack.copy();
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(preDrainStack);
    if (fluidHandler != null) {
        FluidStack drain = fluidHandler.drain(Integer.MAX_VALUE, true);
        ItemStack drainedStack = fluidHandler.getContainer();
        FMLLog.info("Draining " + stackString(stack) + " gives " + fluidString(drain) + " and " + stackString(drainedStack));
        if (drain == null && !ItemStack.areItemStacksEqual(originalStack, preDrainStack)) {
            throw new RuntimeException("ItemStack was altered by its fluid handler when drain did nothing.");
        }
        for (Fluid fluid : FluidRegistry.getRegisteredFluids().values()) {
            ItemStack preFillStack = stack.copy();
            fluidHandler = FluidUtil.getFluidHandler(preFillStack);
            if (fluidHandler != null) {
                int filled = fluidHandler.fill(new FluidStack(fluid, Integer.MAX_VALUE), true);
                ItemStack filledStack = fluidHandler.getContainer();
                if (filled > 0) {
                    FMLLog.info("Filling " + stackString(stack) + " with " + fluidString(new FluidStack(fluid, filled)) + " gives " + stackString(filledStack));
                } else {
                    if (!ItemStack.areItemStacksEqual(originalStack, preFillStack)) {
                        throw new RuntimeException("ItemStack was altered by its fluid handler when fill did nothing.");
                    }
                    if (!ItemStack.areItemStacksEqual(preFillStack, filledStack)) {
                        throw new RuntimeException("ItemStack was altered by its fluid handler when fill did nothing.");
                    }
                }
            }
        }
    }
}
Also used : IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IFluidHandlerItem (net.minecraftforge.fluids.capability.IFluidHandlerItem)7 ItemStack (net.minecraft.item.ItemStack)6 Nonnull (javax.annotation.Nonnull)3 SoundEvent (net.minecraft.util.SoundEvent)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1 TestFluid (net.minecraftforge.debug.ModelFluidDebug.TestFluid)1 IFluidBlock (net.minecraftforge.fluids.IFluidBlock)1 FluidBucketWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBucketWrapper)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1