Skip to content

Commit

Permalink
Revert "Improve TE Networking Performance (#1883)"
Browse files Browse the repository at this point in the history
This reverts commit 35c0c5a
  • Loading branch information
ALongStringOfNumbers committed Jul 5, 2023
1 parent 6ef3688 commit 40aa597
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/gregtech/api/metatileentity/SyncedTileEntityBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagByteArray;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraftforge.common.util.Constants;

import javax.annotation.Nonnull;
import java.util.Arrays;
Expand Down Expand Up @@ -41,23 +43,27 @@ public SPacketUpdateTileEntity getUpdatePacket() {
if (this.updates.isEmpty()) {
return null;
}

NBTTagCompound updateTag = new NBTTagCompound();
for (var entry : this.updates.int2ObjectEntrySet()) {
updateTag.setByteArray(String.valueOf(entry.getIntKey()), entry.getValue());
NBTTagList listTag = new NBTTagList();
for (Int2ObjectMap.Entry<byte[]> entry : updates.int2ObjectEntrySet()) {
NBTTagCompound entryTag = new NBTTagCompound();
entryTag.setByteArray(Integer.toString(entry.getIntKey()), entry.getValue());
listTag.appendTag(entryTag);
}

updateTag.setTag("d", listTag);
this.updates.clear();
return new SPacketUpdateTileEntity(getPos(), 0, updateTag);
}

@Override
public void onDataPacket(@Nonnull NetworkManager net, SPacketUpdateTileEntity pkt) {
for (var entry : pkt.getNbtCompound().tagMap.entrySet()) {
if (entry.getValue() instanceof NBTTagByteArray compound) {
String key = entry.getKey();
ByteBuf buf = Unpooled.wrappedBuffer(compound.getByteArray()).asReadOnly();
receiveCustomData(Integer.parseInt(key), new PacketBuffer(buf));
NBTTagCompound updateTag = pkt.getNbtCompound();
NBTTagList listTag = updateTag.getTagList("d", Constants.NBT.TAG_COMPOUND);
for (NBTBase entryBase : listTag) {
NBTTagCompound entryTag = (NBTTagCompound) entryBase;
for (String discriminatorKey : entryTag.getKeySet()) {
ByteBuf backedBuffer = Unpooled.copiedBuffer(entryTag.getByteArray(discriminatorKey));
receiveCustomData(Integer.parseInt(discriminatorKey), new PacketBuffer(backedBuffer));
}
}
}
Expand All @@ -77,7 +83,7 @@ public NBTTagCompound getUpdateTag() {
public void handleUpdateTag(@Nonnull NBTTagCompound tag) {
super.readFromNBT(tag); // deserializes Forge data and capabilities
byte[] updateData = tag.getByteArray("d");
ByteBuf backedBuffer = Unpooled.wrappedBuffer(updateData).asReadOnly();
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
receiveInitialSyncData(new PacketBuffer(backedBuffer));
}

Expand Down

0 comments on commit 40aa597

Please sign in to comment.