package reborncore.common.util;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;

public class FluidUtils
{

	public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot)
	{
		ItemStack input = inv.getStackInSlot(inputSlot);
		ItemStack output = inv.getStackInSlot(outputSlot);

		if (input != null)
		{
			FluidStack fluidInContainer = getFluidStackInContainer(input);
			ItemStack emptyItem = input.getItem().getContainerItem(input);
			if (fluidInContainer != null
					&& (emptyItem == null || output == null || (output.stackSize < output.getMaxStackSize()
							&& ItemUtils.isItemEqual(output, emptyItem, true, true))))
			{
				int used = fluidHandler.fill(null, fluidInContainer, false);
				if (used >= fluidInContainer.amount && fluidHandler.canFill(EnumFacing.UP, fluidInContainer.getFluid()))
				{
					fluidHandler.fill(null, fluidInContainer, true);
					if (emptyItem != null)
						if (output == null)
							inv.setInventorySlotContents(outputSlot, emptyItem);
						else
							output.stackSize++;
					inv.decrStackSize(inputSlot, 1);
					return true;
				}
			}
		}
		return false;
	}

	public static boolean fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot,
			Fluid fluidToFill)
	{
		ItemStack input = inv.getStackInSlot(inputSlot);
		ItemStack output = inv.getStackInSlot(outputSlot);
		ItemStack filled = getFilledContainer(fluidToFill, input);
		if (filled != null && (output == null
				|| (output.stackSize < output.getMaxStackSize() && ItemUtils.isItemEqual(filled, output, true, true))))
		{
			FluidStack fluidInContainer = getFluidStackInContainer(filled);
			FluidStack drain = fluidHandler.drain(null, fluidInContainer, false);
			if (drain != null && drain.amount == fluidInContainer.amount)
			{
				fluidHandler.drain(null, fluidInContainer, true);
				if (output == null)
					inv.setInventorySlotContents(outputSlot, filled);
				else
					output.stackSize++;
				inv.decrStackSize(inputSlot, 1);
				return true;
			}
		}
		return false;
	}

	public static FluidStack getFluidStackInContainer(ItemStack stack)
	{
		return FluidContainerRegistry.getFluidForFilledItem(stack);
	}

	public static ItemStack getFilledContainer(Fluid fluid, ItemStack empty)
	{
		if (fluid == null || empty == null)
			return null;
		return FluidContainerRegistry.fillFluidContainer(new FluidStack(fluid, Integer.MAX_VALUE), empty);
	}

}

@@ -181,163 +181,9 @@
 ids.
-Fluid;%0Aimport net.minecraftforge.fluids.FluidContainerRegistry;%0Aimport net.minecraftforge.fluids.FluidStack;%0Aimport net.minecraftforge.fluids.IFluidHandler
+*
 ;%0A%0Ap
@@ -587,16 +587,136 @@
 input);%0A
+%09%09%09if(input.getItem() instanceof UniversalBucket)%7B%0A%09%09%09%09emptyItem = ((UniversalBucket) input.getItem()).getEmpty();%0A%09%09%09%7D%0A
 %09%09%09if (f
@@ -2366,16 +2366,142 @@
 ack)%0A%09%7B%0A
+%09%09if(stack.getItem() instanceof IFluidContainerItem)%7B%0A%09%09%09return  ((IFluidContainerItem) stack.getItem()).getFluid(stack);%0A%09%09%7D%0A
 %09%09return

Fix cells not working correctly in the tanks