Skip to content

Commit 5dcee83

Browse files
committed
Fix server crashing on previews of machines
Fixes #387
1 parent 47b55d2 commit 5dcee83

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/main/java/org/dave/compactmachines3/utility/ChunkUtils.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
import net.minecraft.world.chunk.NibbleArray;
1515
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
1616
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
17+
import net.minecraftforge.fml.relauncher.ReflectionHelper;
1718
import org.dave.compactmachines3.world.data.provider.AbstractExtraTileDataProvider;
1819
import org.dave.compactmachines3.world.data.provider.ExtraTileDataProviderRegistry;
1920

21+
import java.lang.reflect.Field;
2022
import java.util.List;
2123

2224
public class ChunkUtils {
25+
private static Field updatePacketNBTField;
26+
2327
public static IBlockAccess getBlockAccessFromChunk(Chunk chunk) {
2428
return new ChunkBlockAccess(chunk);
2529
}
@@ -96,15 +100,20 @@ public static NBTTagCompound writeChunkToNBT(Chunk chunkIn, World worldIn, NBTTa
96100
}
97101
catch (Exception e)
98102
{
99-
net.minecraftforge.fml.common.FMLLog.log.error("An Entity type {} has thrown an exception trying to write state. It will not persist. Report this to the mod author",
103+
net.minecraftforge.fml.common.FMLLog.log.error("An Entity type {} has thrown an exception trying to write state. It will not be visible in compact machines. Report this to the Compact Machines author.",
100104
entity.getClass().getName(), e);
101105
}
102106
}
103107
}
104108

105109
compound.setTag("Entities", nbttaglist1);
106-
NBTTagList nbttaglist2 = new NBTTagList();
107110

111+
if(updatePacketNBTField == null) {
112+
updatePacketNBTField = ReflectionHelper.findField(SPacketUpdateTileEntity.class, "nbt", "field_148860_e");
113+
updatePacketNBTField.setAccessible(true);
114+
}
115+
116+
NBTTagList nbttaglist2 = new NBTTagList();
108117
for (TileEntity tileentity : chunkIn.getTileEntityMap().values())
109118
{
110119
try
@@ -117,23 +126,23 @@ public static NBTTagCompound writeChunkToNBT(Chunk chunkIn, World worldIn, NBTTa
117126
}
118127

119128
SPacketUpdateTileEntity updatePacket = tileentity.getUpdatePacket();
120-
if(updatePacket != null && updatePacket.getNbtCompound() != null) {
121-
NBTTagCompound updateData = updatePacket.getNbtCompound();
129+
if(updatePacket != null) {
130+
NBTTagCompound updateData = (NBTTagCompound) updatePacketNBTField.get(updatePacket);
122131
nbttagcompound3.setTag("cm3_update", updateData);
123132
}
124133

125134
nbttaglist2.appendTag(nbttagcompound3);
126135
}
127136
catch (Exception e)
128137
{
129-
net.minecraftforge.fml.common.FMLLog.log.error("A TileEntity type {} has throw an exception trying to write state. It will not persist. Report this to the mod author",
138+
net.minecraftforge.fml.common.FMLLog.log.error("A TileEntity type {} has throw an exception trying to write state. It will not be visible in compact machines. Report this to the Compact Machines author.",
130139
tileentity.getClass().getName(), e);
131140
}
132141
}
133-
134142
compound.setTag("TileEntities", nbttaglist2);
135-
List<NextTickListEntry> list = worldIn.getPendingBlockUpdates(chunkIn, false);
136143

144+
145+
List<NextTickListEntry> list = worldIn.getPendingBlockUpdates(chunkIn, false);
137146
if (list != null)
138147
{
139148
long j = worldIn.getTotalWorldTime();

0 commit comments

Comments
 (0)