fix: preserve saved tick targets across loading

This commit is contained in:
Algent committed 2026-09-14 19:49:13 +02:00
1 parent e7aa53694f
commit 001d25d0d6
3 files changed
+89 -5

No files matched your search

@@ -0,0 +1,70 @@
package codechicken.multipart.test;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Field;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.chunk.Chunk;
import org.junit.jupiter.api.Test;
import codechicken.lib.world.ChunkExtension;
import codechicken.multipart.TickScheduler;
import codechicken.multipart.TileMultipart;
class SavedTickLoadingFunctionalTest {
@Test
@SuppressWarnings("unchecked")
void missingPartsKeepSurvivingTicksAtTheirOriginalIndices() throws Exception {
NBTTagList parts = new NBTTagList();
for (String type : new String[] { "test:missing-part", "mc_button", "mc_button" }) {
NBTTagCompound part = new NBTTagCompound();
part.setString("id", type);
part.setByte("meta", (byte) 1);
parts.appendTag(part);
}
NBTTagCompound savedTile = new NBTTagCompound();
savedTile.setTag("parts", parts);
TileMultipart[] loaded = new TileMultipart[1];
ExpectedErrorLog.silence(() -> loaded[0] = TileMultipart.createFromNBT(savedTile));
TileMultipart tile = loaded[0];
assertEquals(2, tile.jPartList().size());
Chunk chunk = new Chunk(null, 0, 0);
chunk.chunkTileEntityMap.put(new ChunkPosition(0, 0, 0), tile);
NBTTagList ticks = new NBTTagList();
for (int index : new int[] { 0, 1, 2, 99 }) {
NBTTagCompound tick = new NBTTagCompound();
tick.setByte("i", (byte) index);
tick.setLong("time", index + 10);
ticks.appendTag(tick);
}
NBTTagCompound data = new NBTTagCompound();
data.setTag("multipartTicks", ticks);
ChunkExtension scheduler = TickScheduler.createChunkExtension(chunk, TickScheduler.createWorldExtension(null));
scheduler.loadData(data);
Field tickList = scheduler.getClass().getDeclaredField("tickList");
tickList.setAccessible(true);
List<TickScheduler.PartTickEntry> restored = (List<TickScheduler.PartTickEntry>) tickList.get(scheduler);
assertEquals(2, restored.size());
assertSame(tile.jPartList().get(0), restored.get(0).part());
assertEquals(11, restored.get(0).time());
assertSame(tile.jPartList().get(1), restored.get(1).part());
assertEquals(12, restored.get(1).time());
tile.onChunkLoad();
NBTTagCompound resaved = new NBTTagCompound();
scheduler.saveData(resaved);
scheduler.loadData(resaved);
restored = (List<TickScheduler.PartTickEntry>) tickList.get(scheduler);
assertEquals(2, restored.size());
assertSame(tile.jPartList().get(0), restored.get(0).part());
assertSame(tile.jPartList().get(1), restored.get(1).part());
}
}
@@ -292,11 +292,10 @@ public final class TickScheduler {
BlockCoord pos = MultipartProxy.indexInChunk(cc, tag.getShort("pos"));
Object tile = chunk.chunkTileEntityMap.get(new ChunkPosition(pos.x, pos.y, pos.z));
if (tile instanceof TileMultipart) {
tickList.add(
new PartTickEntry(
((TileMultipart) tile).partList().apply(tag.getByte("i")),
tag.getLong("time"),
false));
TMultiPart part = ((TileMultipart) tile).partFromSavedIndex(tag.getByte("i") & 0xFF);
if (part != null) {
tickList.add(new PartTickEntry(part, tag.getLong("time"), false));
}
}
}
if (!tickList.isEmpty()) {
@@ -44,6 +44,17 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
private boolean doesTick = false;
// Original NBT indices are needed until chunk tick data has been restored.
private int[] loadedPartIndices;
TMultiPart partFromSavedIndex(int index) {
if (loadedPartIndices != null) {
if (index < 0 || index >= loadedPartIndices.length) return null;
index = loadedPartIndices[index];
}
return index >= 0 && index < partList().size() ? partList().apply(index) : null;
}
/**
* @deprecated Use {@link #jPartList()} for Java collection access. Retained as the virtual storage accessor for
* existing subclasses and binaries.
@@ -205,6 +216,7 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
@Override
public void onChunkLoad() {
loadedPartIndices = null;
operate(action(TMultiPart::onChunkLoad));
}
@@ -987,11 +999,13 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
public static TileMultipart createFromNBT(NBTTagCompound tag) {
NBTTagList partList = tag.getTagList("parts", 10);
List<TMultiPart> parts = new ArrayList<>();
int[] loadedIndices = new int[partList.tagCount()];
for (int i = 0; i < partList.tagCount(); i++) {
NBTTagCompound partTag = partList.getCompoundTagAt(i);
String partID = partTag.getString("id");
TMultiPart part = MultiPartRegistry.loadPart(partID, partTag);
loadedIndices[i] = part == null ? -1 : parts.size();
if (part != null) {
part.load(partTag);
parts.add(part);
@@ -1005,6 +1019,7 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
TileMultipart tmb = MultipartGenerator$.MODULE$.generateCompositeTile(null, toSeq(parts), false);
tmb.readFromNBT(tag);
tmb.loadParts(toSeq(parts));
tmb.loadedPartIndices = loadedIndices;
return tmb;
}