Skip to content

Commit d1198be

Browse files
authored
Fix Packet Data Memory Leak (#2823)
1 parent f73b285 commit d1198be

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/java/gregtech/api/metatileentity/SyncedTileEntityBase.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
1414
import net.minecraft.tileentity.TileEntity;
1515
import net.minecraft.util.EnumFacing;
16+
import net.minecraft.world.WorldServer;
1617
import net.minecraftforge.common.util.Constants;
1718

1819
import io.netty.buffer.ByteBuf;
@@ -57,7 +58,25 @@ public void addPacketsFrom(SyncedTileEntityBase syncedTileEntityBase) {
5758
private void notifyWorld() {
5859
@SuppressWarnings("deprecation")
5960
IBlockState blockState = getBlockType().getStateFromMeta(getBlockMetadata());
60-
world.notifyBlockUpdate(getPos(), blockState, blockState, 0);
61+
if (canNotifyWorld()) {
62+
world.notifyBlockUpdate(getPos(), blockState, blockState, 0);
63+
}
64+
}
65+
66+
private boolean canNotifyWorld() {
67+
// short circuit with packet size to avoid too many hash lookups and instanceof casts
68+
if (updates.size() > 10 && getWorld() instanceof WorldServer server) {
69+
int x = getPos().getX() >> 4;
70+
int z = getPos().getZ() >> 4;
71+
if (server.getPlayerChunkMap().contains(x, z)) {
72+
return true;
73+
} else {
74+
// cannot send, so clear
75+
updates.clear();
76+
return false;
77+
}
78+
}
79+
return false;
6180
}
6281

6382
@Override

src/main/java/gregtech/api/network/PacketDataList.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import org.jetbrains.annotations.NotNull;
77

8+
import java.util.Arrays;
9+
810
/**
911
* An optimised data structure backed by two arrays.
1012
* This is essentially equivalent to <code>List<Pair<Integer, byte[]>></code>, but more efficient.
@@ -84,9 +86,8 @@ public boolean isEmpty() {
8486
* remove all data packets
8587
*/
8688
public void clear() {
87-
for (int i = 0; i < this.size; i++) {
88-
this.data[i] = null;
89-
}
89+
Arrays.fill(this.discriminators, 0);
90+
Arrays.fill(this.data, null);
9091
this.size = 0;
9192
}
9293

0 commit comments

Comments
 (0)