package org.cyclops.cyclopscore.fluid;

import net.minecraft.fluid.Fluid;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.fluids.capability.templates.FluidTank;

/**
 * A simple fluid tank.
 * Based on the Buildcraft Tank
 * @author rubensworks
 *
 */
public class Tank extends FluidTank {

    /**
     * Make a new fluid tank.
     * @param capacity The capacity (mB) for the tank.
     */
    public Tank(int capacity) {
        super(capacity);
    }
    
    /**
     * Check if this tank is empty.
     * @return If the tank is empty; no fluid is inside of it.
     */
    public boolean isEmpty() {
        return getFluid().isEmpty() || getFluid().getAmount() <= 0;
    }

    /**
     * Check if this tank is full; the capacity is reached.
     * @return If this tank is full.
     */
    public boolean isFull() {
        return !getFluid().isEmpty() && getFluid().getAmount() >= getCapacity();
    }

    /**
     * Get the fluid that currently occupies this tank, will return null if there is no fluid.
     * @return The inner fluid.
     */
    public Fluid getFluidType() {
        return !getFluid().isEmpty() ? getFluid().getFluid() : null;
    }

    protected boolean replaceInnerFluid() {
        return true;
    }

    @Override
    public CompoundNBT writeToNBT(CompoundNBT nbt) {
        if(replaceInnerFluid()) {
            super.writeToNBT(nbt);
        }
        writeTankToNBT(nbt);
        return nbt;
    }

    @Override
    public FluidTank readFromNBT(CompoundNBT nbt) {
        if(replaceInnerFluid()) {
            if (nbt.contains("Empty")) {
                setFluid(null);
            }
            super.readFromNBT(nbt);
        }
        readTankFromNBT(nbt);
        return this;
    }

    /**
     * Write the tank contents to NBT.
     * @param nbt The NBT tag to write to.
     */
    public void writeTankToNBT(CompoundNBT nbt) {
    	nbt.putInt("capacity", getCapacity());
    }

    /**
     * Read the tank contents from NBT.
     * @param nbt The NBT tag to write from.
     */
    public void readTankFromNBT(CompoundNBT nbt) {
    	if(nbt.contains("capacity")) { // Backwards compatibility.
    		setCapacity(nbt.getInt("capacity"));
    	}
    }

}

@@ -1440,32 +1440,230 @@
 urn nbt;%0A    %7D%0A%0A
+    public CompoundNBT writeToNBT(CompoundNBT nbt, String tag) %7B%0A        CompoundNBT subTag = new CompoundNBT();%0A        writeToNBT(subTag);%0A        nbt.put(tag, subTag);%0A        return nbt;%0A    %7D%0A%0A
     @Override%0A  
@@ -1929,32 +1929,192 @@
 rn this;%0A    %7D%0A%0A
+    public FluidTank readFromNBT(CompoundNBT data, String tag) %7B%0A        CompoundNBT subTag = data.getCompound(tag);%0A        return readFromNBT(subTag);%0A    %7D%0A%0A
     /**%0A     * W

Add NBT-related convenience methods to Tank