TechReborn/src/main/java/techreborn/util/Tank.java

86 lines
2.2 KiB
Java
Raw Normal View History

2015-04-10 01:18:13 +02:00
package techreborn.util;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.Fluid;
2015-06-06 08:52:00 +02:00
import net.minecraftforge.fluids.FluidStack;
2015-04-10 01:18:13 +02:00
import net.minecraftforge.fluids.FluidTank;
2015-06-06 08:52:00 +02:00
import techreborn.packets.PacketHandler;
2015-04-10 01:18:13 +02:00
public class Tank extends FluidTank {
2015-04-24 15:20:09 +02:00
private final String name;
2015-06-06 08:52:00 +02:00
private FluidStack lastBeforeUpdate = null;
2015-04-24 15:20:09 +02:00
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;
}
@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;
}
2015-04-10 01:18:13 +02:00
2015-06-06 08:52:00 +02:00
public void compareAndUpdate() {
FluidStack current = this.getFluid();
if (current != null) {
if (lastBeforeUpdate != null) {
if (Math.abs(current.amount - lastBeforeUpdate.amount) >= 500) {
PacketHandler.sendPacketToAllPlayers(tile.getDescriptionPacket(), tile.getWorldObj());
lastBeforeUpdate = current.copy();
}
else if (lastBeforeUpdate.amount < this.getCapacity() && current.amount == this.getCapacity() || lastBeforeUpdate.amount == this.getCapacity() && current.amount < this.getCapacity()) {
PacketHandler.sendPacketToAllPlayers(tile.getDescriptionPacket(), tile.getWorldObj());
lastBeforeUpdate = current.copy();
}
}
else {
PacketHandler.sendPacketToAllPlayers(tile.getDescriptionPacket(), tile.getWorldObj());
lastBeforeUpdate = current.copy();
}
}
else if (lastBeforeUpdate != null) {
PacketHandler.sendPacketToAllPlayers(tile.getDescriptionPacket(), tile.getWorldObj());
lastBeforeUpdate = null;
}
}
2015-04-10 01:18:13 +02:00
}