package org.cyclops.cyclopscore.fluid;

import net.minecraft.fluid.Fluid;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.registries.ForgeRegistries;

/**
 * A simple tank that can accept and drain fluids until the capacity is reached.
 * Only one fluid can be accepted, which must be specified with {@link SingleUseTank#setAcceptedFluid(Fluid)}.
 * Based on the Buildcraft SingleUseTank.
 *
 * @author rubensworks
 *
 */
public class SingleUseTank extends Tank {
    
    /**
     * The NBT name for the fluid tank.
     */
    public static final String NBT_ACCEPTED_FLUID = "acceptedFluid";

    private Fluid acceptedFluid;

    /**
     * Make a new tank instance.
     * @param capacity The capacity (mB) for the tank.
     */
    public SingleUseTank(int capacity) {
        super(capacity);
    }

    @Override
    public int fill(FluidStack resource, FluidAction action) {
        Fluid acceptedFluid = getAcceptedFluid();
    	int filled = 0;
        if (resource.isEmpty()) {
        	filled = 0;
        } else {
        	if (action.execute() && acceptedFluid == null) {
        		acceptedFluid = resource.getFluid();
        	}
        	if (acceptedFluid == null || acceptedFluid == resource.getFluid()) {
                filled = super.fill(resource, action);
            }
        }
        if(filled > 0) {
        	sendUpdate();
        }
        return filled;
    }
    
    @Override
    public FluidStack drain(int maxDrain, FluidAction action) {
        FluidStack drained = super.drain(maxDrain, action);
    	if (!drained.isEmpty()) {
    		sendUpdate();
    	}
    	return drained;
    }

    @Override
    public FluidStack drain(FluidStack resource, FluidAction action) {
        FluidStack drained = super.drain(resource, action);
        if (!drained.isEmpty()) {
            sendUpdate();
        }
        return drained;
    }

    protected void sendUpdate() {

    }

    /**
     * Reset the tank by setting the inner fluid to null.
     */
    public void reset() {
        acceptedFluid = null;
        setValidator(fluidStack -> true);
    }

    /**
     * Set the accepted fluid for this tank.
     * @param fluid The accepted fluid
     */
    public void setAcceptedFluid(Fluid fluid) {
        this.acceptedFluid = fluid;
        setValidator(fluidStack -> fluidStack.getFluid() == fluid);
    }

    /**
     * Get the accepted fluid for this tank.
     * @return The accepted fluid.
     */
    public Fluid getAcceptedFluid() {
        return acceptedFluid;
    }

    @Override
    public void writeTankToNBT(CompoundNBT nbt) {
        super.writeTankToNBT(nbt);
        if (acceptedFluid != null)
            nbt.putString(NBT_ACCEPTED_FLUID, acceptedFluid.getRegistryName().toString());
    }

    @Override
    public void readTankFromNBT(CompoundNBT nbt) {
        super.readTankFromNBT(nbt);
        setAcceptedFluid(ForgeRegistries.FLUIDS.getValue(new ResourceLocation(nbt.getString(NBT_ACCEPTED_FLUID))));
    }
    
}

@@ -67,16 +67,51 @@
 .Fluid;%0A
+import net.minecraft.fluid.Fluids;%0A
 import n
@@ -284,16 +284,43 @@
 tries;%0A%0A
+import java.util.Objects;%0A%0A
 /**%0A * A
@@ -959,16 +959,56 @@
 acity);%0A
+        setAcceptedFluid(Fluids.EMPTY);%0A
     %7D%0A%0A 
@@ -1276,20 +1276,28 @@
 luid == 
-null
+Fluids.EMPTY
 ) %7B%0A    
@@ -1380,20 +1380,28 @@
 luid == 
-null
+Fluids.EMPTY
  %7C%7C acce
@@ -2243,20 +2243,28 @@
 Fluid = 
-null
+Fluids.EMPTY
 ;%0A      
@@ -2483,23 +2483,151 @@
 Fluid = 
+Objects.requireNonNull(
 fluid
+)
 ;%0A
+        if (fluid == Fluids.EMPTY) %7B%0A            setValidator(fluidStack -%3E true);%0A        %7D else %7B%0A    
         
@@ -2682,24 +2682,34 @@
  == fluid);%0A
+        %7D%0A
     %7D%0A%0A    /
@@ -2977,47 +2977,8 @@
 t);%0A
-        if (acceptedFluid != null)%0A    
     

Fix invalid fluid validation in tanks