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

79 lines
2.6 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 {
private final String name;
2015-04-24 15:20:09 +02:00
private FluidStack lastBeforeUpdate = null;
2015-06-06 08:52:00 +02:00
public Tank(String name, int capacity, TileEntity tile) {
super(capacity);
this.name = name;
this.tile = tile;
}
2015-04-24 15:20:09 +02:00
public boolean isEmpty() {
return getFluid() == null || getFluid().amount <= 0;
}
2015-04-24 15:20:09 +02:00
public boolean isFull() {
return getFluid() != null && getFluid().amount >= getCapacity();
}
2015-04-24 15:20:09 +02:00
public Fluid getFluidType() {
return getFluid() != null ? getFluid().getFluid() : null;
}
2015-04-24 15:20:09 +02:00
@Override
public final NBTTagCompound writeToNBT(NBTTagCompound nbt) {
NBTTagCompound tankData = new NBTTagCompound();
super.writeToNBT(tankData);
nbt.setTag(name, tankData);
return nbt;
}
2015-04-24 15:20:09 +02:00
@Override
public final FluidTank readFromNBT(NBTTagCompound nbt) {
if (nbt.hasKey(name)) {
// allow to read empty tanks
setFluid(null);
2015-04-24 15:20:09 +02:00
NBTTagCompound tankData = nbt.getCompoundTag(name);
super.readFromNBT(tankData);
}
return this;
}
2015-04-10 01:18:13 +02:00
public void compareAndUpdate() {
if (tile.getWorldObj().isRemote) {
return;
}
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-06-06 08:52:00 +02:00
2015-04-10 01:18:13 +02:00
}