Clean up Fluid Update Packet

It was sending a ton of unused bytes, whoever did the Liquid->Fluid API
update left a bunch of old metadata related stuff in.

Also checking for valid fluids is necessary because its possible for
FluidStack.getFluid() to return null.
This commit is contained in:
CovertJaguar 2013-12-19 20:36:58 -08:00
parent 10daf6f202
commit ae3a89bfe9
2 changed files with 35 additions and 33 deletions

View file

@ -219,13 +219,13 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
private PacketFluidUpdate computeFluidUpdate(boolean initPacket, boolean persistChange) {
boolean changed = false;
BitSet delta = new BitSet(21);
BitSet delta = new BitSet(PacketFluidUpdate.FLUID_DATA_NUM * ForgeDirection.VALID_DIRECTIONS.length);
if (initClient > 0) {
initClient--;
if (initClient == 1) {
changed = true;
delta.set(0, 21);
delta.set(0, PacketFluidUpdate.FLUID_DATA_NUM * ForgeDirection.VALID_DIRECTIONS.length);
}
}
@ -236,36 +236,34 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
FluidStack current = internalTanks[dir.ordinal()].getFluid();
FluidStack prev = renderCache[dir.ordinal()];
if (prev == null && current == null) {
if (current != null && current.getFluid() == null)
continue;
}
if (prev == null && current != null) {
if (prev == null && current == null)
continue;
if (prev == null ^ current == null) {
changed = true;
if (current != null) {
renderCache[dir.ordinal()] = current.copy();
colorRenderCache[dir.ordinal()] = current.getFluid().getColor(current);
delta.set(dir.ordinal() * 3 + 0);
delta.set(dir.ordinal() * 3 + 1);
delta.set(dir.ordinal() * 3 + 2);
} else {
renderCache[dir.ordinal()] = null;
colorRenderCache[dir.ordinal()] = 0xFFFFFF;
}
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT);
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_AMOUNT_BIT);
continue;
}
if (prev != null && current == null) {
changed = true;
renderCache[dir.ordinal()] = null;
colorRenderCache[dir.ordinal()] = 0xFFFFFF;
delta.set(dir.ordinal() * 3 + 0);
delta.set(dir.ordinal() * 3 + 1);
delta.set(dir.ordinal() * 3 + 2);
if (prev == null || current == null)
continue;
}
if (!prev.equals(current) || initPacket) {
changed = true;
renderCache[dir.ordinal()] = current;
colorRenderCache[dir.ordinal()] = current.getFluid().getColor(current);
delta.set(dir.ordinal() * 3 + 0);
delta.set(dir.ordinal() * 3 + 1);
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT);
}
int displayQty = (prev.amount * 4 + current.amount) / 5;
@ -277,7 +275,7 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
if (prev.amount != displayQty || initPacket) {
changed = true;
renderCache[dir.ordinal()].amount = displayQty;
delta.set(dir.ordinal() * 3 + 2);
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_AMOUNT_BIT);
}
}

View file

@ -16,6 +16,10 @@ import net.minecraftforge.fluids.FluidStack;
public class PacketFluidUpdate extends PacketCoordinates {
public static int FLUID_ID_BIT = 0;
public static int FLUID_AMOUNT_BIT = 1;
public static int FLUID_DATA_NUM = 2;
public FluidStack[] renderCache = new FluidStack[ForgeDirection.values().length];
public int[] colorRenderCache = new int[ForgeDirection.values().length];
public BitSet delta;
@ -56,19 +60,19 @@ public class PacketFluidUpdate extends PacketCoordinates {
renderCache = transLiq.renderCache;
colorRenderCache = transLiq.colorRenderCache;
byte[] dBytes = new byte[3];
byte[] dBytes = new byte[2];
data.read(dBytes);
delta = fromByteArray(dBytes);
// System.out.printf("read %d, %d, %d = %s, %s%n", posX, posY, posZ, Arrays.toString(dBytes), delta);
for (ForgeDirection dir : ForgeDirection.values()) {
if (delta.get(dir.ordinal() * 3 + 0)) {
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) {
int amt = renderCache[dir.ordinal()] != null ? renderCache[dir.ordinal()].amount : 0;
renderCache[dir.ordinal()] = new FluidStack(data.readInt(), amt);
renderCache[dir.ordinal()] = new FluidStack(data.readShort(), amt);
colorRenderCache[dir.ordinal()] = data.readInt();
}
if (delta.get(dir.ordinal() * 3 + 2)) {
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_AMOUNT_BIT)) {
if (renderCache[dir.ordinal()] == null) {
renderCache[dir.ordinal()] = new FluidStack(0,0);
}
@ -88,16 +92,16 @@ public class PacketFluidUpdate extends PacketCoordinates {
for (ForgeDirection dir : ForgeDirection.values()) {
FluidStack liquid = renderCache[dir.ordinal()];
if (delta.get(dir.ordinal() * 3 + 0)) {
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) {
if (liquid != null) {
data.writeInt(liquid.fluidID);
data.writeShort(liquid.fluidID);
data.writeInt(colorRenderCache[dir.ordinal()]);
} else {
data.writeInt(0);
data.writeShort(0);
data.writeInt(0xFFFFFF);
}
}
if (delta.get(dir.ordinal() * 3 + 2)) {
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_AMOUNT_BIT)) {
if (liquid != null) {
data.writeInt(liquid.amount);
} else {
@ -118,7 +122,7 @@ public class PacketFluidUpdate extends PacketCoordinates {
}
public static byte[] toByteArray(BitSet bits) {
byte[] bytes = new byte[3];
byte[] bytes = new byte[2];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);