/*
 * Copyright (c) 2017 modmuss50 and Gigabit101
 *
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 *
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package reborncore.common.util;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import reborncore.common.network.packet.CustomDescriptionPacket;

public class Tank extends FluidTank {

	private final String name;

	private FluidStack lastBeforeUpdate = null;

	Fluid lastFluid;
	int lastAmmount;

	public Tank(String name, int capacity, TileEntity tile) {
		super(capacity);
		this.name = name;
		this.tile = tile;
	}

	public boolean isEmpty() {
		return getFluid() == null || getFluid().amount <= 0;
	}

	public boolean isFull() {
		return getFluid() != null && getFluid().amount >= getCapacity();
	}

	public Fluid getFluidType() {
		return getFluid() != null ? getFluid().getFluid() : null;
	}

	@Override
	public final NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		NBTTagCompound tankData = new NBTTagCompound();
		super.writeToNBT(tankData);
		nbt.setTag(name, tankData);
		return nbt;
	}

	public void setFluidAmount(int amount) {
		if (fluid != null) {
			fluid.amount = amount;
		}
	}

	@Override
	public final FluidTank readFromNBT(NBTTagCompound nbt) {
		if (nbt.hasKey(name)) {
			// allow to read empty tanks
			setFluid(null);

			NBTTagCompound tankData = nbt.getCompoundTag(name);
			super.readFromNBT(tankData);
		}
		return this;
	}

	//TODO optimise
	public void compareAndUpdate() {
		if (tile == null || tile.getWorld().isRemote) {
			return;
		}
		if (lastFluid == null || (lastFluid != null && (this.getFluid() == null) || this.getFluid().getFluid() == null) || (lastFluid != this.getFluid().getFluid()) || lastAmmount != this.getFluidAmount()) {
			if (this.getFluid() == null) {
				lastFluid = null;
				lastAmmount = 0;
			} else {
				lastFluid = this.getFluid().getFluid();
				lastAmmount = this.getFluidAmount();
			}
			reborncore.common.network.NetworkManager.sendToAllAround(new CustomDescriptionPacket(tile), new NetworkRegistry.TargetPoint(tile.getWorld().provider.getDimension(), tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), 20));
		}
	}

}

@@ -1400,31 +1400,19 @@
 ort 
-net.minecraftf
+rebornc
 or
-g
 e.
-fml.
 comm
@@ -1429,24 +1429,23 @@
 .Network
-Registry
+Manager
 ;%0Aimport
@@ -2736,181 +2736,428 @@
 %7D%0A%09%09
-if (lastFluid == null %7C%7C (lastFluid != null && (this.getFluid() == null) %7C%7C this.getFluid().getFluid() == null) %7C%7C (lastFluid != this.getFluid().getFluid()) %7C%7C lastAm
+FluidStack current = this.getFluid();%0A%09%09if (current != null) %7B%0A%09%09%09if (lastBeforeUpdate != null) %7B%0A%09%09%09%09if (Math.abs(current.amount - lastBeforeUpdate.amount) %3E= 500) %7B%0A%09%09%09%09%09NetworkManager.sendToWorld(new CustomDescriptionPacket(tile), tile.getWorld());%0A%09%09%09%09%09lastBeforeUpdate = current.copy();%0A%09%09%09%09%7D else if (lastBeforeUpdate.amount %3C this.getCapacity() && current.amount == this.getCapacity() %7C%7C lastBeforeUpdate.a
 mount 
-!
+=
 = th
@@ -3166,156 +3166,286 @@
 .get
-FluidAmount()) %7B%0A%09%09%09if (this.getFluid() == null) %7B%0A%09%09%09%09lastFluid = null
+Capacity() && current.amount %3C this.getCapacity()) %7B%0A%09%09%09%09%09NetworkManager.sendToWorld(new CustomDescriptionPacket(tile), tile.getWorld())
 ;%0A%09%09%09%09
+%09
 last
-Ammount = 0;%0A%09%09%09%7D else %7B%0A%09%09%09%09lastFluid = this.getFluid().getFlui
+BeforeUpdate = current.copy();%0A%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09NetworkManager.sendToWorld(new CustomDescriptionPacket(tile), tile.getWorl
 d()
+)
 ;%0A%09%09
@@ -3454,75 +3454,88 @@
 last
-Ammount = this.getFluidAmount();%0A%09%09%09%7D%0A%09%09%09reborncore.common.network.
+BeforeUpdate = current.copy();%0A%09%09%09%7D%0A%09%09%7D else if (lastBeforeUpdate != null) %7B%0A%09%09%09
 Netw
@@ -3555,16 +3555,12 @@
 ndTo
-AllAroun
+Worl
 d(ne
@@ -3596,151 +3596,52 @@
 e), 
-new NetworkRegistry.TargetPoint(tile.getWorld().provider.getDimension(), tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), 20))
+tile.getWorld());%0A%09%09%09lastBeforeUpdate = null
 ;%0A%09%09

Update old tank syncing code, fixes TechReborn/TechReborn/issues/1377