Make smartBored return a bored copy instead of changing the pack's object

Boring an object mutated the shared IrisObject, so what a placement saw
depended on which placement bored it first. IrisObject now keeps one bored
copy, made on the first ask and dropped when its blocks change, so later
asks cost a field read. The bore runs under the read lock without
changing the original.
This commit is contained in:
piexel committed 2026-11-06 12:59:35 +01:00
1 parent 699ad3f6fe
commit 0b5b0190f5
5 files changed
+139 -60

No files matched your search

@@ -0,0 +1,87 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.object.prefab.IrisObject;
import com.volmit.object.PackObjects;
import com.volmit.object.PlaceableObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
/** Smart boring hands back a bored copy and leaves the pack's own object as it was. */
public class IrisPackObjectsSmartBoredTest {
private static final int SIZE = 3;
private static final int SHELL_BLOCKS = SIZE * SIZE * SIZE - 1;
private static final int MIDDLE = 1;
private final PackObjects objects = new IrisPackObjects(mock(IrisData.class), mock(IrisPackResources.class));
private static IrisSettings previousSettings;
@BeforeClass
public static void bindPlatform() {
UsableBlockPlatform.bind();
previousSettings = IrisSettings.settings;
IrisSettings.settings = new IrisSettings();
}
@AfterClass
public static void unbindPlatform() {
IrisSettings.settings = previousSettings;
UsableBlockPlatform.unbind();
}
@Test
public void theBoredObjectIsACopyAndThePacksObjectKeepsItsBlocks() {
IrisObject shell = hollowCube();
PlaceableObject bored = objects.smartBored(shell);
assertNotSame(shell, bored);
assertEquals(SHELL_BLOCKS, shell.getBlocks().size());
assertEquals(SHELL_BLOCKS + 1, ((IrisObject) bored).getBlocks().size());
assertEquals(IrisObject.States.VAIR, ((IrisObject) bored).getBlocks().get(shell.getSigned(MIDDLE, MIDDLE, MIDDLE)));
}
@Test
public void boringAgainGivesTheSameCopyWithoutBoringAgain() {
IrisObject shell = hollowCube();
PlaceableObject bored = objects.smartBored(shell);
assertSame(bored, objects.smartBored(shell));
assertSame(bored, objects.smartBored(bored));
}
@Test
public void changingTheObjectMakesTheNextBoredCopyFromItsNewBlocks() {
IrisObject shell = hollowCube();
PlaceableObject before = objects.smartBored(shell);
shell.setUnsigned(0, 0, 0, null);
PlaceableObject after = objects.smartBored(shell);
assertNotSame(before, after);
}
private static IrisObject hollowCube() {
IrisObject cube = new IrisObject(SIZE, SIZE, SIZE);
for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
for (int z = 0; z < SIZE; z++) {
if (x != MIDDLE || y != MIDDLE || z != MIDDLE) {
cube.setUnsigned(x, y, z, IrisObject.States.STONE);
}
}
}
}
return cube;
}
}
@@ -18,6 +18,7 @@ import art.arcane.iris.builtin.prepare.ObjectRotation;
import art.arcane.iris.builtin.prepare.ObjectTranslate;
import art.arcane.iris.engine.object.prefab.TileData;
import art.arcane.iris.engine.object.terrain.carving.CarveResult;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.BuiltInPack;
import art.arcane.iris.core.modules.InMemoryMantle;
@@ -246,16 +247,20 @@ public class ObjectPlacementRegressionTest {
public void debugPlacementDoesNotMutateSmartBoreCache() {
IrisObject object = new IrisObject(1, 1, 1);
object.setUnsigned(0, 0, 0, IrisObject.States.VAIR);
object.setSmartBored(true);
ObjectPlacement.Builder placement = placement();
placement.smartBore(true);
RecordingPlacer debug = new RecordingPlacer(engine);
RecordingPlacer normal = new RecordingPlacer(null);
SmartBoreDebugSettings shown = TestSettings.of(SmartBoreDebugSettings.class, "{\"show\":true}");
when(engine.getDimension().getModuleSettings()).thenReturn(BuiltInPack.settings(shown));
placed(object, 0, ANCHOR_Y, 0, debug, placement.build(), new RNG(2L), data);
placed(object, 0, ANCHOR_Y, 0, normal, placement.build(), new RNG(2L), data);
IrisSettings previousSettings = IrisSettings.settings;
IrisSettings.settings = new IrisSettings();
try {
placed(object, 0, ANCHOR_Y, 0, debug, placement.build(), new RNG(2L), data);
placed(object, 0, ANCHOR_Y, 0, normal, placement.build(), new RNG(2L), data);
} finally {
IrisSettings.settings = previousSettings;
}
PlatformBlockState cachedState = object.getBlocks().get(object.getSigned(0, 0, 0));
assertSame(IrisObject.States.VAIR, cachedState);