package openmods.liquids;

import javax.annotation.Nullable;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.FluidTankProperties;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;

public class SingleFluidBucketHandler implements IFluidHandler {

	private final ItemStack container;

	private final int volume;

	private final Fluid fluid;

	private final ItemStack emptyContainer;

	private final FluidStack contents;

	private final IFluidTankProperties properties;

	public SingleFluidBucketHandler(ItemStack container, String fluidId, int volume, ItemStack emptyContainer) {
		this.container = container;
		this.fluid = FluidRegistry.getFluid(fluidId);
		this.volume = volume;
		this.emptyContainer = emptyContainer.copy();

		this.contents = new FluidStack(fluid, volume);
		this.properties = new FluidTankProperties(contents, volume);
	}

	@Override
	public IFluidTankProperties[] getTankProperties() {
		return new IFluidTankProperties[] { properties };
	}

	@Override
	public int fill(FluidStack resource, boolean doFill) {
		return 0;
	}

	protected void switchToEmptyBucket() {
		container.deserializeNBT(emptyContainer.serializeNBT());
	}

	@Override
	@Nullable
	public FluidStack drain(FluidStack resource, boolean doDrain) {
		if (container.stackSize != 1 || resource == null || resource.amount < volume)
			return null;

		if (resource.getFluid() == fluid)
			if (doDrain)
				switchToEmptyBucket();

		return contents;
	}

	@Override
	@Nullable
	public FluidStack drain(int maxDrain, boolean doDrain) {
		if (container.stackSize != 1 || maxDrain < Fluid.BUCKET_VOLUME)
			return null;

		if (doDrain)
			switchToEmptyBucket();

		return contents;
	}

}
@@ -135,56 +135,8 @@
 id;%0A
-import net.minecraftforge.fluids.FluidRegistry;%0A
 impo
@@ -450,33 +450,39 @@
 final ItemStack 
-c
+filledC
 ontainer;%0A%0A%09priv
@@ -508,37 +508,8 @@
 e;%0A%0A
-%09private final Fluid fluid;%0A%0A
 %09pri
@@ -632,16 +632,43 @@
 rties;%0A%0A
+%09private boolean isEmpty;%0A%0A
 %09public 
@@ -702,17 +702,23 @@
 emStack 
-c
+filledC
 ontainer
@@ -723,164 +723,125 @@
 er, 
-String fluidId, int volume, ItemStack emptyContainer) %7B%0A%09%09this.container = container;%0A%09%09this.fluid = FluidRegistry.getFluid(fluidId);%0A%09%09this.volume = volume
+ItemStack emptyContainer, Fluid fluid, int volume) %7B%0A%09%09this.volume = volume;%0A%09%09this.filledContainer = filledContainer
 ;%0A%09%09
@@ -1247,17 +1247,23 @@
 t() %7B%0A%09%09
-c
+filledC
 ontainer
@@ -1310,16 +1310,165 @@
 NBT());%0A
+%09%09isEmpty = true;%0A%09%7D%0A%0A%09private boolean isValidResource(FluidStack resource) %7B%0A%09%09return contents.isFluidEqual(resource) && resource.amount %3E= volume;%0A
 %09%7D%0A%0A%09@Ov
@@ -1548,33 +1548,50 @@
 oDrain) %7B%0A%09%09if (
-c
+isEmpty %7C%7C filledC
 ontainer.stackSi
@@ -1605,108 +1605,53 @@
  %7C%7C 
-resource == null %7C%7C resource.amount %3C volume)%0A%09%09%09return null;%0A%0A%09%09if (resource.getFluid() == fluid)%0A%09
+!isValidResource(resource))%0A%09%09%09return null;%0A%0A
 %09%09if
@@ -1661,17 +1661,16 @@
 oDrain)%0A
-%09
 %09%09%09switc
@@ -1701,24 +1701,31 @@
 urn contents
+.copy()
 ;%0A%09%7D%0A%0A%09@Over
@@ -1804,17 +1804,34 @@
 %7B%0A%09%09if (
-c
+isEmpty %7C%7C filledC
 ontainer
@@ -1864,27 +1864,14 @@
 n %3C 
-Fluid.BUCKET_VOLUME
+volume
 )%0A%09%09
@@ -1944,15 +1944,22 @@
 contents
+.copy()
 ;%0A%09%7D%0A%0A%7D

Tweak bucket handler