From f23ddb476aae45e1fe1cf7d9a04cba169b0388c0 Mon Sep 17 00:00:00 2001 From: NeverCast Date: Sat, 15 Dec 2012 21:04:25 +1300 Subject: [PATCH] Liquid Packets should not send before chunks on client join --- .../transport/PipeTransportLiquids.java | 163 +++++++++++------- .../transport/network/PacketLiquidUpdate.java | 5 + 2 files changed, 101 insertions(+), 67 deletions(-) diff --git a/common/buildcraft/transport/PipeTransportLiquids.java b/common/buildcraft/transport/PipeTransportLiquids.java index 52252ce6..e4614714 100644 --- a/common/buildcraft/transport/PipeTransportLiquids.java +++ b/common/buildcraft/transport/PipeTransportLiquids.java @@ -20,6 +20,7 @@ import buildcraft.core.utils.Utils; import buildcraft.transport.network.PacketLiquidUpdate; import java.util.BitSet; import net.minecraft.src.NBTTagCompound; +import net.minecraft.src.Packet; import net.minecraft.src.TileEntity; import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.liquids.ILiquidTank; @@ -195,76 +196,99 @@ public class PipeTransportLiquids extends PipeTransport implements ITankContaine if (tracker.markTimeIfDelay(worldObj, BuildCraftCore.updateFactor)) { - boolean changed = false; - BitSet delta = new BitSet(21); - - if (initClient > 0) { - initClient--; - if (initClient == 1) { - changed = true; - delta.set(0, 21); - } - } - - for (ForgeDirection dir : orientations) { - LiquidStack current = internalTanks[dir.ordinal()].getLiquid(); - LiquidStack prev = renderCache[dir.ordinal()]; - - if (prev == null && current == null) { - continue; - } - - if (prev == null && current != null) { - changed = true; - renderCache[dir.ordinal()] = current.copy(); - delta.set(dir.ordinal() * 3 + 0); - delta.set(dir.ordinal() * 3 + 1); - delta.set(dir.ordinal() * 3 + 2); - continue; - } - - if (prev != null && current == null) { - changed = true; - renderCache[dir.ordinal()] = null; - delta.set(dir.ordinal() * 3 + 0); - delta.set(dir.ordinal() * 3 + 1); - delta.set(dir.ordinal() * 3 + 2); - continue; - } - - if (prev.itemID != current.itemID) { - changed = true; - renderCache[dir.ordinal()].itemID = current.itemID; - delta.set(dir.ordinal() * 3 + 0); - } - - if (prev.itemMeta != current.itemMeta) { - changed = true; - renderCache[dir.ordinal()].itemMeta = current.itemMeta; - delta.set(dir.ordinal() * 3 + 1); - } - - int displayQty = (prev.amount * 4 + current.amount) / 5; - if (displayQty == 0 && current.amount > 0) { - displayQty = current.amount; - } - displayQty = Math.min(getCapacity(), displayQty); - - if (prev.amount != displayQty) { - changed = true; - renderCache[dir.ordinal()].amount = displayQty; - delta.set(dir.ordinal() * 3 + 2); - } - } - - if (changed) { - PacketLiquidUpdate packet = new PacketLiquidUpdate(xCoord, yCoord, zCoord); - packet.renderCache = this.renderCache; - packet.delta = delta; + PacketLiquidUpdate packet = computeLiquidUpdate(false, true); + if(packet != null){ CoreProxy.proxy.sendToPlayers(packet.getPacket(), worldObj, xCoord, yCoord, zCoord, DefaultProps.PIPE_CONTENTS_RENDER_DIST); } } } + + /** + * Computes the PacketLiquidUpdate packet for transmission to a client + * @param initPacket everything is sent, no delta stuff ( first packet ) + * @param persistChange The render cache change is persisted + * @return PacketLiquidUpdate liquid update packet + */ + private PacketLiquidUpdate computeLiquidUpdate(boolean initPacket, boolean persistChange){ + + boolean changed = false; + BitSet delta = new BitSet(21); + + if (initClient > 0) { + initClient--; + if (initClient == 1) { + changed = true; + delta.set(0, 21); + } + } + + LiquidStack[] renderCache = this.renderCache.clone(); + + for (ForgeDirection dir : orientations) { + LiquidStack current = internalTanks[dir.ordinal()].getLiquid(); + LiquidStack prev = renderCache[dir.ordinal()]; + + if (prev == null && current == null) { + continue; + } + + if (prev == null && current != null) { + changed = true; + renderCache[dir.ordinal()] = current.copy(); + delta.set(dir.ordinal() * 3 + 0); + delta.set(dir.ordinal() * 3 + 1); + delta.set(dir.ordinal() * 3 + 2); + continue; + } + + if (prev != null && current == null) { + changed = true; + renderCache[dir.ordinal()] = null; + delta.set(dir.ordinal() * 3 + 0); + delta.set(dir.ordinal() * 3 + 1); + delta.set(dir.ordinal() * 3 + 2); + continue; + } + + if (prev.itemID != current.itemID || initPacket) { + changed = true; + renderCache[dir.ordinal()].itemID = current.itemID; + delta.set(dir.ordinal() * 3 + 0); + } + + if (prev.itemMeta != current.itemMeta || initPacket) { + changed = true; + renderCache[dir.ordinal()].itemMeta = current.itemMeta; + delta.set(dir.ordinal() * 3 + 1); + } + + int displayQty = (prev.amount * 4 + current.amount) / 5; + if (displayQty == 0 && current.amount > 0 || initPacket) { + displayQty = current.amount; + } + displayQty = Math.min(getCapacity(), displayQty); + + if (prev.amount != displayQty || initPacket) { + changed = true; + renderCache[dir.ordinal()].amount = displayQty; + delta.set(dir.ordinal() * 3 + 2); + } + } + + if(persistChange){ + this.renderCache = renderCache; + } + + if (changed || initPacket) { + PacketLiquidUpdate packet = new PacketLiquidUpdate(xCoord, yCoord, zCoord, initPacket); + packet.renderCache = renderCache; + packet.delta = delta; + return packet; + } + + return null; + + } /** * Initializes client @@ -272,10 +296,15 @@ public class PipeTransportLiquids extends PipeTransport implements ITankContaine @Override public void sendDescriptionPacket() { super.sendDescriptionPacket(); - + + PacketLiquidUpdate packet = computeLiquidUpdate(true, false); + if(packet != null){ + CoreProxy.proxy.sendToPlayers(packet.getPacket(), worldObj, xCoord, yCoord, zCoord, DefaultProps.PIPE_CONTENTS_RENDER_DIST); + } initClient = 6; } + @Override public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); diff --git a/common/buildcraft/transport/network/PacketLiquidUpdate.java b/common/buildcraft/transport/network/PacketLiquidUpdate.java index ffd148bc..52a8b63f 100644 --- a/common/buildcraft/transport/network/PacketLiquidUpdate.java +++ b/common/buildcraft/transport/network/PacketLiquidUpdate.java @@ -22,6 +22,11 @@ public class PacketLiquidUpdate extends PacketCoordinates { public PacketLiquidUpdate(int xCoord, int yCoord, int zCoord) { super(PacketIds.PIPE_LIQUID, xCoord, yCoord, zCoord); } + + public PacketLiquidUpdate(int xCoord, int yCoord, int zCoord, boolean chunkPacket) { + super(PacketIds.PIPE_LIQUID, xCoord, yCoord, zCoord); + this.isChunkDataPacket = chunkPacket; + } public PacketLiquidUpdate() { }