Merge pull request #1694 from abel1502/abel-cache-packets

Cache tile entity network packets
This commit is contained in:
HbmMods 2024-09-24 08:04:38 +02:00 committed by GitHub
commit d0a61f964a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 9 deletions

View File

@ -2626,7 +2626,7 @@ public class ModBlocks {
GameRegistry.registerBlock(reinforced_ducrete, ItemBlockBlastInfo.class, reinforced_ducrete.getUnlocalizedName());
GameRegistry.registerBlock(concrete_smooth, ItemBlockBlastInfo.class, concrete_smooth.getUnlocalizedName());
GameRegistry.registerBlock(concrete_colored, ItemBlockColoredConcrete.class, concrete_colored.getUnlocalizedName());
register(concrete_colored_ext);
GameRegistry.registerBlock(concrete_colored_ext, ItemBlockBlastInfo.class, concrete_colored_ext.getUnlocalizedName());
GameRegistry.registerBlock(concrete, ItemBlockBlastInfo.class, concrete.getUnlocalizedName());
GameRegistry.registerBlock(concrete_asbestos, ItemBlockBlastInfo.class, concrete_asbestos.getUnlocalizedName());
GameRegistry.registerBlock(concrete_super, ItemBlockBlastInfo.class, concrete_super.getUnlocalizedName());

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) {

View File

@ -32,6 +32,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
public boolean isWhitelist = false;
public ModulePatternMatcher matcher;
public long lastGrabbedTick = 0;
public TileEntityCraneGrabber() {
super(11);
@ -63,7 +64,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
}
}
if(worldObj.getTotalWorldTime() % delay == 0 && !this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) {
if(worldObj.getTotalWorldTime() >= lastGrabbedTick + delay && !this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) {
int amount = 1;
if(slots[9] != null && slots[9].getItem() == ModItems.upgrade_stack) {
@ -111,6 +112,8 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
boolean match = this.matchesFilter(stack);
if(this.isWhitelist && !match || !this.isWhitelist && match) continue;
lastGrabbedTick = worldObj.getTotalWorldTime();
ItemStack copy = stack.copy();
int toAdd = Math.min(stack.stackSize, amount);
copy.stackSize = toAdd;
@ -132,8 +135,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
NBTTagCompound data = new NBTTagCompound();
data.setBoolean("isWhitelist", isWhitelist);
this.matcher.writeToNBT(data);
this.writeToNBT(data);
this.networkPack(data, 15);
}
}
@ -141,9 +143,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.isWhitelist = nbt.getBoolean("isWhitelist");
this.matcher.modes = new String[this.matcher.modes.length];
this.matcher.readFromNBT(nbt);
this.readFromNBT(nbt);
}
public boolean matchesFilter(ItemStack stack) {
@ -175,6 +175,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
super.readFromNBT(nbt);
this.isWhitelist = nbt.getBoolean("isWhitelist");
this.matcher.readFromNBT(nbt);
this.lastGrabbedTick = nbt.getLong("lastGrabbedTick");
}
@Override
@ -182,6 +183,7 @@ public class TileEntityCraneGrabber extends TileEntityCraneBase implements IGUIP
super.writeToNBT(nbt);
nbt.setBoolean("isWhitelist", this.isWhitelist);
this.matcher.writeToNBT(nbt);
nbt.setLong("lastGrabbedTick", lastGrabbedTick);
}
@Override