Check native structures against the real transformed object bounds

The native-structure veto used a square envelope of half the largest object side, so rotated or off-centre objects reaching past it were never checked. It now uses the prepared transformed bounds, with Y widened by the warp margin. IrisObject.copy() now reads under the read lock instead of the object monitor.

Upstream: d55c77572
This commit is contained in:
piexel committed 2026-11-10 01:00:40 +01:00
1 parent a81a3abb9f
commit 8e0e6d7d45
4 files changed
+140 -25

No files matched your search

@@ -135,17 +135,22 @@ public class IrisObject extends IrisRegistrant implements PlaceableObject {
return aabb.aquire(() -> getAABBFor(new IrisBlockVector(w, h, d)));
}
public synchronized IrisObject copy() {
IrisObject o = new IrisObject(w, h, d);
o.setLoadKey(getLoadKey());
o.setLoader(getLoader());
o.setLoadFile(getLoadFile());
o.setCenter(getCenter().clone());
public IrisObject copy() {
readLock.lock();
try {
IrisObject o = new IrisObject(w, h, d);
o.setLoadKey(getLoadKey());
o.setLoader(getLoader());
o.setLoadFile(getLoadFile());
o.setCenter(getCenter().clone());
blocks.forEach((i, v) -> o.blocks.put(i.clone(), v));
states.forEach((i, v) -> o.states.put(i.clone(), v.clone()));
blocks.forEach((i, v) -> o.blocks.put(i.clone(), v));
states.forEach((i, v) -> o.states.put(i.clone(), v.clone()));
return o;
return o;
} finally {
readLock.unlock();
}
}
public void readLegacy(InputStream in) throws IOException {
@@ -37,6 +37,7 @@ import art.arcane.iris.spi.PlatformRegistries;
import art.arcane.iris.util.project.stream.ProceduralStream;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.math.RNG;
import art.arcane.volmlib.util.math.Vector3i;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.ModuleSetup;
@@ -585,6 +586,41 @@ public class ObjectPlacementRegressionTest {
assertFalse(placer.writes().isEmpty());
}
@Test
public void obliqueRotationRejectsNativePiecesBeyondTheUnrotatedRadius() {
int reach = 14;
when(engine.getNativeStructureVolumes(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(new KList<>(
NativeStructureVolume.of("minecraft:village", reach, ANCHOR_Y, 0, reach, ANCHOR_Y, 0),
NativeStructureVolume.of("minecraft:village", -reach, ANCHOR_Y, 0, -reach, ANCHOR_Y, 0),
NativeStructureVolume.of("minecraft:village", 0, ANCHOR_Y, reach, 0, ANCHOR_Y, reach),
NativeStructureVolume.of("minecraft:village", 0, ANCHOR_Y, -reach, 0, ANCHOR_Y, -reach)));
IrisObject object = new IrisObject(21, 1, 21);
object.setUnsigned(20, 0, 20, solid);
RecordingPlacer placer = new RecordingPlacer(engine);
ObjectPlacementResult result = ModuleObjectPlacement.place(object, 0, ANCHOR_Y, 0, placer,
placement().rotation(ObjectRotation.of(0, 45, 0)).build(), new RNG(2L), null, null, data);
assertEquals(Optional.of("native-structure"), result.rejection());
assertTrue(placer.writes().isEmpty());
}
@Test
public void offCenterOriginRejectsNativePiecesAtTheFarEdge() {
when(engine.getNativeStructureVolumes(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(
new KList<>(NativeStructureVolume.of("minecraft:village", 8, ANCHOR_Y, 0, 8, ANCHOR_Y, 0)));
IrisObject object = new IrisObject(9, 1, 1);
object.setCenter(new Vector3i(0, 0, 0));
object.setUnsigned(8, 0, 0, solid);
RecordingPlacer placer = new RecordingPlacer(engine);
ObjectPlacementResult result = ModuleObjectPlacement.place(object, 0, ANCHOR_Y, 0, placer,
placement().rotation(ObjectRotation.of(0, 0, 0)).build(), new RNG(2L), null, null, data);
assertEquals(Optional.of("native-structure"), result.rejection());
assertTrue(placer.writes().isEmpty());
}
@Test
public void collisionRejectionPrecedesBoreAndAllowedObjectsOverrideForbiddenOnes() {
PlacedObject forbidden = new PlacedObject(object("forbidden"), 1, 0, 0);
@@ -0,0 +1,80 @@
package art.arcane.iris.engine.object.prefab;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.volmlib.util.collection.KMap;
import art.arcane.volmlib.util.math.IrisBlockVector;
import art.arcane.volmlib.util.math.Vector3i;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
public class IrisObjectCopyTest {
private static final long WAIT_SECONDS = 5;
private static final long BLOCKED_PROBE_MILLIS = 150;
@Test
public void copyWaitsForVolumeMutationToComplete() throws Exception {
IrisObject source = new IrisObject(1, 1, 1);
PlatformBlockState block = mock(PlatformBlockState.class);
CountDownLatch started = new CountDownLatch(1);
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
source.writeLock.lock();
Future<IrisObject> result;
try {
result = executor.submit(() -> {
started.countDown();
return source.copy();
});
assertTrue(started.await(WAIT_SECONDS, TimeUnit.SECONDS));
assertThrows(TimeoutException.class, () -> result.get(BLOCKED_PROBE_MILLIS, TimeUnit.MILLISECONDS));
source.setW(3);
source.setCenter(new Vector3i(1, 0, 0));
source.setUnsigned(2, 0, 0, block);
} finally {
source.writeLock.unlock();
}
IrisObject copy = result.get(WAIT_SECONDS, TimeUnit.SECONDS);
assertEquals(3, copy.getW());
assertEquals(new Vector3i(1, 0, 0), copy.getCenter());
assertSame(block, copy.getBlocks().get(new IrisBlockVector(1, 0, 0)));
} finally {
executor.shutdownNow();
}
}
@Test
public void copyOwnsItsCenterAndTiles() {
IrisObject source = new IrisObject(1, 1, 1);
PlatformBlockState block = mock(PlatformBlockState.class);
TileData tile = new TileData("minecraft:chest", new KMap<>());
tile.getProperties().put("name", "source");
source.setUnsigned(0, 0, 0, block);
source.setUnsignedTile(0, 0, 0, tile);
IrisObject copy = source.copy();
IrisBlockVector position = new IrisBlockVector(0, 0, 0);
TileData copiedTile = copy.getStates().get(position);
assertNotSame(source.getCenter(), copy.getCenter());
assertNotSame(tile, copiedTile);
assertNotSame(tile.getProperties(), copiedTile.getProperties());
copiedTile.getProperties().put("name", "copy");
copy.getBlocks().remove(position);
assertEquals("source", tile.getProperties().get("name"));
assertSame(block, source.getBlocks().get(position));
}
}
@@ -22,7 +22,7 @@ import art.arcane.iris.builtin.anchor.PlaceTrait;
import art.arcane.iris.builtin.carve.CarvingMode;
import art.arcane.iris.builtin.objects.ObjectRun;
import art.arcane.iris.builtin.terrain.BedrockSettings;
import art.arcane.iris.modules.placement.ObjectShape;
import art.arcane.iris.builtin.prepare.TransformedBounds;
import art.arcane.iris.modules.placement.PlacementSettings;
import art.arcane.iris.modules.placement.PlacementStages;
import art.arcane.iris.modules.placement.PreparedPlacement;
@@ -156,15 +156,19 @@ public final class ValidateModule implements IrisModule {
}
int x = placement.x();
int z = placement.z();
int margin = nativeStructureMargin(placement.object(), prepared);
StructureVolumes volumes = terrain.structureVolumes(x - margin, z - margin, x + margin, z + margin);
TransformedBounds bounds = prepared.bounds();
int minX = x + bounds.minX();
int maxX = x + bounds.maxX();
int minZ = z + bounds.minZ();
int maxZ = z + bounds.maxZ();
StructureVolumes volumes = terrain.structureVolumes(minX, minZ, maxX, maxZ);
if (volumes.isEmpty()) {
return false;
}
boolean painted = WorldVolume.isPainted(placement, prepared);
int envelopeMinY = painted ? Integer.MIN_VALUE : y - margin;
int envelopeMaxY = painted ? Integer.MAX_VALUE : y + margin;
if (!volumes.intersects(x - margin, envelopeMinY, z - margin, x + margin, envelopeMaxY, z + margin)) {
int envelopeMinY = painted ? Integer.MIN_VALUE : y + bounds.minY() - prepared.warpMargin();
int envelopeMaxY = painted ? Integer.MAX_VALUE : y + bounds.maxY() + prepared.warpMargin();
if (!volumes.intersects(minX, envelopeMinY, minZ, maxX, envelopeMaxY, maxZ)) {
return false;
}
for (ObjectBlock block : context.request().object().blocks()) {
@@ -185,16 +189,6 @@ public final class ValidateModule implements IrisModule {
return false;
}
/** How far around the anchor any block of the transformed object can reach. */
private static int nativeStructureMargin(ObjectShape object, PreparedPlacement prepared) {
int margin = (Math.max(object.width(), Math.max(object.height(), object.depth())) / 2) + 1 + prepared.warpMargin();
if (prepared.translation() == null) {
return margin;
}
return margin + Math.max(Math.abs(prepared.translation().getBlockX()),
Math.max(Math.abs(prepared.translation().getBlockY()), Math.abs(prepared.translation().getBlockZ())));
}
private static boolean isAir(BlockState state) {
return AIR_MATERIALS.contains(state.material().toString());
}