Add caching for TileEntityMachineBase update packages

Now they aren't send unless something has changed,
except for one every second as a workaround
for an apparent issue
This commit is contained in:
abel1502 2024-09-23 18:36:03 +03:00
parent 8aad7fb44b
commit df92d913da
No known key found for this signature in database
GPG Key ID: 076926596A536338

View File

@ -8,6 +8,7 @@ import com.hbm.util.fauxpointtwelve.DirPos;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
@ -23,6 +24,9 @@ public abstract class TileEntityMachineBase extends TileEntityLoadedBase impleme
private String customName;
private NBTTagCompound lastPackedNBT = null;
private ByteBuf lastPackedBuf = null;
public TileEntityMachineBase(int slotCount) {
slots = new ItemStack[slotCount];
}
@ -157,7 +161,18 @@ public abstract class TileEntityMachineBase extends TileEntityLoadedBase impleme
@Deprecated public void networkPack(NBTTagCompound nbt, int range) {
nbt.setBoolean("muffled", muffled);
if(!worldObj.isRemote) PacketDispatcher.wrapper.sendToAllAround(new NBTPacket(nbt, xCoord, yCoord, zCoord), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, range));
if(worldObj.isRemote) {
return;
}
// Same as networkPackNT
if (lastPackedNBT != null && lastPackedNBT.equals(nbt) && worldObj.getWorldTime() % 20 != 0) {
return;
}
this.lastPackedNBT = nbt;
PacketDispatcher.wrapper.sendToAllAround(new NBTPacket(nbt, xCoord, yCoord, zCoord), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, range));
}
@Deprecated
@ -167,7 +182,27 @@ public abstract class TileEntityMachineBase extends TileEntityLoadedBase impleme
/** Sends a sync packet that uses ByteBuf for efficient information-cramming */
public void networkPackNT(int range) {
if(!worldObj.isRemote) PacketDispatcher.wrapper.sendToAllAround(new BufPacket(xCoord, yCoord, zCoord, this), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, range));
if(worldObj.isRemote) {
return;
}
BufPacket packet = new BufPacket(xCoord, yCoord, zCoord, this);
ByteBuf buf = Unpooled.buffer();
packet.toBytes(buf);
// Don't send unnecessary packets, except for maybe one every second or so.
// If we stop sending duplicate packets entirely, this causes issues when
// a client unloads and then loads back a chunk with an unchanged tile entity.
// For that client, the tile entity will appear default until anything changes about it.
// In my testing, this can be reliably reproduced with a full fluid barrel, for instance.
// I think it might be fixable by doing something with getDescriptionPacket() and onDataPacket(),
// but this sidesteps the problem for the mean time.
if (lastPackedBuf != null && buf.equals(lastPackedBuf) && worldObj.getWorldTime() % 20 != 0) {
return;
}
this.lastPackedBuf = buf;
PacketDispatcher.wrapper.sendToAllAround(packet, new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, range));
}
@Override public void serialize(ByteBuf buf) {