From d22b1433b20f7f0aa60a1f8dabc10ee6084bcbaf Mon Sep 17 00:00:00 2001 From: Algent Date: Wed, 9 Sep 2026 17:13:30 +0200 Subject: [PATCH] fix: preserve scheduler callbacks and inventory size bounds --- JAVA_MIGRATION_HANDOFF.md | 4 +- docs/migration/HISTORY.md | 13 +++++ .../test/TIInventoryTileFunctionalTest.java | 24 ++++++++ .../codechicken/multipart/TickScheduler.java | 21 ++++--- .../multipart/scalatraits/JInventoryTile.java | 3 +- .../TickSchedulerCallbackRegressionTest.java | 55 +++++++++++++++++++ 6 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 src/test/java/codechicken/multipart/TickSchedulerCallbackRegressionTest.java diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 5c00a44..332efda 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -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 diff --git a/docs/migration/HISTORY.md b/docs/migration/HISTORY.md index 812e7aa..7456519 100644 --- a/docs/migration/HISTORY.md +++ b/docs/migration/HISTORY.md @@ -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. diff --git a/src/functionalTest/java/codechicken/multipart/test/TIInventoryTileFunctionalTest.java b/src/functionalTest/java/codechicken/multipart/test/TIInventoryTileFunctionalTest.java index 5e6ec52..08e8986 100644 --- a/src/functionalTest/java/codechicken/multipart/test/TIInventoryTileFunctionalTest.java +++ b/src/functionalTest/java/codechicken/multipart/test/TIInventoryTileFunctionalTest.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; } diff --git a/src/main/scala/codechicken/multipart/TickScheduler.java b/src/main/scala/codechicken/multipart/TickScheduler.java index b93ae64..83bc203 100644 --- a/src/main/scala/codechicken/multipart/TickScheduler.java +++ b/src/main/scala/codechicken/multipart/TickScheduler.java @@ -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 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) tickChunks + .filter(new AbstractFunction1() { + + @Override + public Object apply(ChunkTickScheduler chunk) { + return chunk.processTicks(); + } + }); } processing = false; diff --git a/src/main/scala/codechicken/multipart/scalatraits/JInventoryTile.java b/src/main/scala/codechicken/multipart/scalatraits/JInventoryTile.java index 9471eb2..905475c 100644 --- a/src/main/scala/codechicken/multipart/scalatraits/JInventoryTile.java +++ b/src/main/scala/codechicken/multipart/scalatraits/JInventoryTile.java @@ -107,7 +107,8 @@ public class JInventoryTile extends TileMultipart implements TIInventoryTile { Tuple2[] rebuilt = (Tuple2[]) 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(inventory, Integer.valueOf(slot)); } } diff --git a/src/test/java/codechicken/multipart/TickSchedulerCallbackRegressionTest.java b/src/test/java/codechicken/multipart/TickSchedulerCallbackRegressionTest.java new file mode 100644 index 0000000..90b0662 --- /dev/null +++ b/src/test/java/codechicken/multipart/TickSchedulerCallbackRegressionTest.java @@ -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; + } +}