Skip to content

Commit 0e9e318

Browse files
committed
Fix preview crash caused by large NBT data
This should fix #359
1 parent 7ff5993 commit 0e9e318

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/main/java/org/dave/compactmachines3/network/MessageMachineChunk.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.dave.compactmachines3.network;
22

33
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.Unpooled;
45
import net.minecraft.nbt.NBTTagCompound;
56
import net.minecraft.util.math.BlockPos;
67
import net.minecraft.world.chunk.Chunk;
@@ -10,6 +11,7 @@
1011
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
1112
import org.dave.compactmachines3.CompactMachines3;
1213
import org.dave.compactmachines3.utility.ChunkUtils;
14+
import org.dave.compactmachines3.utility.Logz;
1315
import org.dave.compactmachines3.world.tools.DimensionTools;
1416

1517
public class MessageMachineChunk implements IMessage, IMessageHandler<MessageMachineChunk, IMessage> {
@@ -34,7 +36,29 @@ public void fromBytes(ByteBuf buf) {
3436
@Override
3537
public void toBytes(ByteBuf buf) {
3638
buf.writeInt(coords);
37-
ByteBufUtils.writeTag(buf, data);
39+
40+
ByteBuf tmpBuf = Unpooled.buffer();
41+
ByteBufUtils.writeTag(tmpBuf, data);
42+
43+
if(tmpBuf.writerIndex() >= 2097152 - 8) {
44+
Logz.debug("Chunk data to big to send. Stripping TileEntity nbt data!");
45+
data.removeTag("TileEntities");
46+
47+
ByteBuf tmpBufNoTiles = Unpooled.buffer();
48+
ByteBufUtils.writeTag(tmpBufNoTiles, data);
49+
50+
if(tmpBufNoTiles.writerIndex() >= 2097152 - 8) {
51+
Logz.warn("Chunk data to big even without nbt data. Sending no chunk data!");
52+
ByteBufUtils.writeTag(buf, new NBTTagCompound());
53+
} else {
54+
buf.writeBytes(tmpBufNoTiles.readerIndex(0));
55+
}
56+
tmpBufNoTiles.release();
57+
} else {
58+
buf.writeBytes(tmpBuf.readerIndex(0));
59+
}
60+
61+
tmpBuf.release();
3862
}
3963

4064
@Override

0 commit comments

Comments
 (0)