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;
}
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.");
}
}
}
}
}
}
Aggregations