mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-22 21:19:49 +00:00
fix: preserve scheduler callbacks and inventory size bounds
This commit is contained in:
1 parent
92a1f82427
commit
d22b1433b2
6 files changed
+108
-12
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user