fix: preserve scheduler callbacks and inventory size bounds

This commit is contained in:
Algent committed 2026-09-09 17:13:30 +02:00
1 parent 92a1f82427
commit d22b1433b2
6 files changed
+108 -12

No files matched your search

+2 -2
View File
@@ -16,8 +16,8 @@ migration checkout; `codex/tile-compatibility-fixes` was deleted after its fixes
## Current state and next target
**576 plain-JVM tests and 289 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **230 Java
files and 9 Scala files / 747 nonblank Scala lines**. The packaged inventory has 450 classes.
**577 plain-JVM tests and 290 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **230 Java
files and 9 Scala files / 747 nonblank Scala lines**. The packaged inventory has 451 classes.
The agreed milestone order is the documented consumer-facing Java API, then consumer release and adoption, then final
Scala removal; see the plan's API migration design and Phases 8-10. Per-change fixes, their regression cases and their
+13
View File
@@ -13,6 +13,19 @@ comparisons, formatting/checkstyle results and stale "X is next" pointers. Findi
ABI observations were kept. A passing build was the baseline expectation for every entry, so its absence here does not
mean a check was skipped. The last two sections hold records moved from the retired profile and downgrader documents.
### 2026-09-09 — Scheduler unload and inventory size regressions
- Restored the original Scala mutable-set filtering in `TickScheduler.WorldTickScheduler.postTick`, matching the
retained-collection approach in `PacketScheduler`. Java's fail-fast set iterator threw when a scheduled callback
unloaded a chunk. The regression test covers multiple unloading callbacks and an unrelated delayed tick, including
exactly-once delivery on later ticks. This restores the reference traversal rather than defining new unload ordering.
- Captured each inventory's size once in the slot-building pass of `JInventoryTile.rebuildSlotMap`, preserving the
original two-pass evaluation. A 54-slot inventory now receives two size queries instead of 56. The generated-trait
regression covers empty, single-slot and 54-slot inventories and checks every flattened slot's owner and local index.
- Both new tests failed before their respective fixes. Formatting, checkstyle, the build, all 577 JVM tests and all
290 Java 8 Forge tests pass with zero failures/errors/skips. The added scheduler callback brings the packaged class
count to 451; all remain Java 8 bytecode (version 52). No public API changes or TPS improvement are claimed.
### 2026-08-14
- Confirmed that the codebase is feasible to migrate incrementally to Java.
@@ -130,6 +130,28 @@ class TIInventoryTileFunctionalTest {
assertEquals(2, ((Number) api.slotMap()[2]._2()).intValue());
}
@Test
void rebuildingQueriesEachInventorySizeOncePerPassAndPreservesSlotOrder() {
TIInventoryTile api = inventory(newInventoryTile());
InventoryPart empty = new InventoryPart("empty", 0);
InventoryPart single = new InventoryPart("single", 1);
InventoryPart large = new InventoryPart("large", 54);
api.invList().addAll(Arrays.asList(empty, single, large));
api.rebuildSlotMap();
assertEquals(2, empty.sizeCalls);
assertEquals(2, single.sizeCalls);
assertEquals(2, large.sizeCalls);
assertEquals(55, api.getSizeInventory());
assertSame(single, api.slotMap()[0]._1());
assertEquals(0, api.slotMap()[0]._2());
for (int slot = 0; slot < 54; slot++) {
assertSame(large, api.slotMap()[slot + 1]._1());
assertEquals(slot, api.slotMap()[slot + 1]._2());
}
}
@Test
void tileInventoryMetadataAndLifecycleRemainFixed() {
TileMultipart tile = newInventoryTile();
@@ -219,6 +241,7 @@ class TIInventoryTileFunctionalTest {
private ItemStack lastValidityStack;
private int openCalls;
private int closeCalls;
private int sizeCalls;
private InventoryPart(String name, int size) {
this.name = name;
@@ -232,6 +255,7 @@ class TIInventoryTileFunctionalTest {
@Override
public int getSizeInventory() {
sizeCalls++;
return stacks.length;
}
@@ -5,10 +5,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
@@ -24,6 +22,9 @@ import codechicken.lib.vec.BlockCoord;
import codechicken.lib.world.ChunkExtension;
import codechicken.lib.world.WorldExtension;
import codechicken.multipart.handler.MultipartProxy;
import scala.collection.mutable.HashSet;
import scala.collection.mutable.Set;
import scala.runtime.AbstractFunction1;
/**
* Used for scheduling delayed callbacks to parts. Do not use this for redstone applications that require precise
@@ -109,13 +110,15 @@ public final class TickScheduler {
@Override
public void postTick() {
if (!tickChunks.isEmpty()) {
Set<ChunkTickScheduler> remaining = new HashSet<>();
for (ChunkTickScheduler chunk : tickChunks) {
if (chunk.processTicks()) {
remaining.add(chunk);
}
}
tickChunks = remaining;
// Callbacks may unload chunks. Keep the reference's mutation-tolerant Scala traversal.
tickChunks = (Set<ChunkTickScheduler>) tickChunks
.filter(new AbstractFunction1<ChunkTickScheduler, Object>() {
@Override
public Object apply(ChunkTickScheduler chunk) {
return chunk.processTicks();
}
});
}
processing = false;
@@ -107,7 +107,8 @@ public class JInventoryTile extends TileMultipart implements TIInventoryTile {
Tuple2<IInventory, Object>[] rebuilt = (Tuple2<IInventory, Object>[]) new Tuple2[totalSlots];
int index = 0;
for (IInventory inventory : invList) {
for (int slot = 0; slot < inventory.getSizeInventory(); slot++) {
int size = inventory.getSizeInventory();
for (int slot = 0; slot < size; slot++) {
rebuilt[index++] = new Tuple2<IInventory, Object>(inventory, Integer.valueOf(slot));
}
}
@@ -0,0 +1,55 @@
package codechicken.multipart;
import static org.junit.jupiter.api.Assertions.assertEquals;
import net.minecraft.world.chunk.Chunk;
import org.junit.jupiter.api.Test;
class TickSchedulerCallbackRegressionTest {
@Test
void callbacksCanUnloadTheirChunksWithoutLosingOtherScheduledTicks() {
TickScheduler.WorldTickScheduler world = new TickScheduler.WorldTickScheduler(null);
int[] unloadedCalls = { 0 };
for (int i = 0; i < 3; i++) {
TickScheduler.ChunkTickScheduler chunk = new TickScheduler.ChunkTickScheduler(new Chunk(null, i, 0), world);
chunk.scheduleTick(part(() -> {
unloadedCalls[0]++;
chunk.unload();
}), 0, false);
}
int[] retainedCalls = { 0 };
TickScheduler.ChunkTickScheduler retained = new TickScheduler.ChunkTickScheduler(new Chunk(null, 3, 0), world);
retained.scheduleTick(part(() -> retainedCalls[0]++), 1, false);
world.preTick();
world.postTick();
assertEquals(3, unloadedCalls[0]);
assertEquals(0, retainedCalls[0]);
world.preTick();
world.postTick();
world.preTick();
world.postTick();
assertEquals(3, unloadedCalls[0]);
assertEquals(1, retainedCalls[0]);
}
private static TMultiPart part(Runnable callback) {
TMultiPart part = new TMultiPart() {
@Override
public String getType() {
return "test:scheduled-callback";
}
@Override
public void scheduledTick() {
callback.run();
}
};
part.bind(new TileMultipart());
return part;
}
}