Mirror objects along X and Z in the object transformer, mirroring block states through the platform's own mirror

This commit is contained in:
piexel committed 2026-10-08 15:16:25 +02:00
1 parent 563c2cd276
commit 844e3205ac
9 files changed
+221 -12

No files matched your search

@@ -21,6 +21,7 @@ package art.arcane.iris.modded;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.spi.PlatformFace;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
@@ -282,6 +283,26 @@ public final class ModdedBlockState implements PlatformBlockState {
return cached;
}
@Override
public PlatformBlockState mirrorX() {
return mirrored(Mirror.FRONT_BACK);
}
@Override
public PlatformBlockState mirrorZ() {
return mirrored(Mirror.LEFT_RIGHT);
}
private PlatformBlockState mirrored(Mirror mirror) {
BlockState mirroredState = state.mirror(mirror);
if (mirroredState == state) {
return this;
}
return deferredPlacementKey == null
? of(mirroredState, null)
: deferred(mirroredState, null, deferredPlacementKey);
}
@Override
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
return ModdedBlockResolution.canAttach((BlockState) support.nativeHandle(), face);
@@ -20,6 +20,7 @@ package art.arcane.iris.core.modules;
import art.arcane.iris.engine.object.prefab.IrisObjectRotation;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.util.common.math.IrisBlockVector;
import com.volmit.mantle.BlockBox;
import com.volmit.object.GeneratedObject;
@@ -37,8 +38,10 @@ import java.util.Map;
/**
* Transforms geometry with the rotation Iris places objects with ({@link IrisObjectRotation}, its axis order
* included) about the anchor, turning block states with the object so stairs and logs face the way it does.
* A Y mirror flips positions the way Iris hangs an object from a ceiling, without changing block states.
* Iris has no way to mirror along X or Z, or to scale geometry, so those requests fail with
* Mirrors come first, as {@link ObjectTransform} says: an X or Z mirror flips positions about the anchor and
* mirrors block states through the platform's own mirror, so stairs and doors face the mirrored way. A Y mirror
* flips positions the way Iris hangs an object from a ceiling, without changing block states, since blocks have
* no mirror along Y. Iris has no way to scale geometry, so that request fails with
* {@link UnsupportedOperationException} rather than changing the request. Blocks that a turn by an angle that is
* not a quarter turn sends to one position keep the last one, as Iris overwrites them when it places.
*/
@@ -66,9 +69,6 @@ final class IrisObjectTransformer implements ObjectTransformer {
}
private static void requireSupported(ObjectTransform transform) {
if (transform.mirrorX() || transform.mirrorZ()) {
throw new UnsupportedOperationException("Iris can mirror an object along Y only, not along X or Z: " + transform);
}
if (transform.scaleX() != NO_SCALE || transform.scaleY() != NO_SCALE || transform.scaleZ() != NO_SCALE) {
throw new UnsupportedOperationException("Iris cannot scale an object's geometry: " + transform);
}
@@ -97,28 +97,43 @@ final class IrisObjectTransformer implements ObjectTransformer {
/** One requested turn about an anchor. */
private static final class Turn {
private final BlockPosition anchor;
private final boolean mirrorX;
private final boolean mirrorY;
private final boolean mirrorZ;
private final IrisObjectRotation rotation;
private final PlatformBlockStates states = new PlatformBlockStates(key -> IrisPlatforms.get().registries().block(key));
Turn(BlockPosition anchor, ObjectTransform transform) {
this.anchor = anchor;
this.mirrorX = transform.mirrorX();
this.mirrorY = transform.mirrorY();
this.mirrorZ = transform.mirrorZ();
this.rotation = IrisObjectRotation.of(transform.rotationX(), transform.rotationY(), transform.rotationZ());
}
BlockPosition position(BlockPosition local) {
IrisBlockVector offset = new IrisBlockVector(local.x() - anchor.x(), local.y() - anchor.y(), local.z() - anchor.z());
if (mirrorX) {
offset.setX(-offset.getX());
}
if (mirrorY) {
offset.setY(-offset.getY());
}
if (mirrorZ) {
offset.setZ(-offset.getZ());
}
IrisBlockVector turned = rotation.rotate(offset, NO_SPIN, NO_SPIN, NO_SPIN);
return new BlockPosition(anchor.x() + (int) Math.round(turned.getX()), anchor.y() + (int) Math.round(turned.getY()),
anchor.z() + (int) Math.round(turned.getZ()));
}
com.volmit.block.BlockState state(ObjectBlock block) {
return states.toApi(rotation.rotate(states.toPlatform(block.state()), NO_SPIN, NO_SPIN, NO_SPIN));
return states.toApi(rotation.rotate(mirrored(states.toPlatform(block.state())), NO_SPIN, NO_SPIN, NO_SPIN));
}
private PlatformBlockState mirrored(PlatformBlockState state) {
PlatformBlockState result = mirrorX ? state.mirrorX() : state;
return mirrorZ ? result.mirrorZ() : result;
}
}
}
@@ -27,6 +27,7 @@ import org.bukkit.Tag;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockSupport;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.structure.Mirror;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -297,6 +298,22 @@ public final class BukkitBlockState implements PlatformBlockState {
return cached;
}
@Override
public PlatformBlockState mirrorX() {
return mirrored(Mirror.FRONT_BACK);
}
@Override
public PlatformBlockState mirrorZ() {
return mirrored(Mirror.LEFT_RIGHT);
}
private PlatformBlockState mirrored(Mirror mirror) {
BlockData copy = data.clone();
copy.mirror(mirror);
return of(copy);
}
@Override
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
return ((BlockData) support.nativeHandle()).isFaceSturdy(blockFaceOf(face), BlockSupport.FULL);
@@ -2,7 +2,9 @@ package art.arcane.iris.core.modules;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.object.prefab.IrisObjectRotation;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.spi.PlatformRegistries;
import com.volmit.block.BlockState;
import com.volmit.mantle.BlockBox;
import com.volmit.object.GeneratedObject;
@@ -21,8 +23,9 @@ import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
/** Objects turn about their anchor the way Iris places them; what Iris cannot do is refused. */
/** Objects turn and mirror about their anchor the way Iris places them; what Iris cannot do is refused. */
public class IrisObjectTransformerTest {
private static final BlockState LOG = BlockState.parse("minecraft:oak_log");
private static final BlockState LEAVES = BlockState.parse("minecraft:oak_leaves");
@@ -95,12 +98,86 @@ public class IrisObjectTransformerTest {
assertEquals(2, squeezed.blocks().size());
}
private static ObjectTransform mirrored(boolean x, boolean z) {
return new ObjectTransform(0, 0, 0, 1, 1, 1, x, false, z);
}
/** What the platform answers for a state and its mirrors: the platform, not Iris, decides how a block looks mirrored. */
private static void mirrorsBy(String key, String alongX, String alongZ) {
PlatformRegistries registries = IrisPlatforms.get().registries();
PlatformBlockState state = registries.block(key);
PlatformBlockState mirroredAlongX = registries.block(alongX);
PlatformBlockState mirroredAlongZ = registries.block(alongZ);
when(state.mirrorX()).thenReturn(mirroredAlongX);
when(state.mirrorZ()).thenReturn(mirroredAlongZ);
}
/** A stair east of the anchor and a marker beside it, in a world whose stairs mirror as the test platform says. */
private GeneratedObject stairsEastOfTheAnchor() {
mirrorsBy("minecraft:oak_stairs[facing=east]", "minecraft:oak_stairs[facing=west]", "minecraft:oak_stairs[facing=east]");
mirrorsBy("minecraft:oak_stairs[facing=west]", "minecraft:oak_stairs[facing=east]", "minecraft:oak_stairs[facing=west]");
mirrorsBy("minecraft:oak_stairs[facing=north]", "minecraft:oak_stairs[facing=north]", "minecraft:oak_stairs[facing=south]");
return new GeneratedObject(new BlockBox(0, 0, 0, 2, 1, 2), new BlockPosition(0, 0, 0),
List.of(new ObjectBlock(new BlockPosition(1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=east]")),
new ObjectBlock(new BlockPosition(0, 0, 1), BlockState.parse("minecraft:oak_stairs[facing=north]"))),
List.of(new ObjectMarker(new BlockPosition(1, 0, 1), SPAWN, Map.of("kind", "bird"))));
}
@Test
public void mirroringAlongXOrZAndScalingAreRefused() {
assertThrows(UnsupportedOperationException.class,
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, true, false, false)));
assertThrows(UnsupportedOperationException.class,
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, false, false, true)));
public void anXMirrorFlipsXAboutTheAnchorAndMirrorsStatesThroughThePlatform() {
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(true, false));
assertEquals(List.of(new ObjectBlock(new BlockPosition(-1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=west]")),
new ObjectBlock(new BlockPosition(0, 0, 1), BlockState.parse("minecraft:oak_stairs[facing=north]"))),
mirrored.blocks());
assertEquals(new BlockPosition(-1, 0, 1), mirrored.markers().getFirst().position());
assertEquals(new BlockBox(-1, 0, 0, 1, 1, 2), mirrored.bounds());
assertEquals(new BlockPosition(0, 0, 0), mirrored.anchor());
}
@Test
public void aZMirrorFlipsZAboutTheAnchorAndMirrorsStatesThroughThePlatform() {
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(false, true));
assertEquals(List.of(new ObjectBlock(new BlockPosition(0, 0, -1), BlockState.parse("minecraft:oak_stairs[facing=south]")),
new ObjectBlock(new BlockPosition(1, 0, 0), BlockState.parse("minecraft:oak_stairs[facing=east]"))),
mirrored.blocks().stream().sorted(java.util.Comparator.comparing(ObjectBlock::position)).toList());
assertEquals(new BlockPosition(1, 0, -1), mirrored.markers().getFirst().position());
}
@Test
public void mirroringBothAxesMirrorsStatesAlongBoth() {
GeneratedObject mirrored = transformer.transform(stairsEastOfTheAnchor(), mirrored(true, true));
assertEquals(List.of(new BlockPosition(-1, 0, 0), new BlockPosition(0, 0, -1)),
mirrored.blocks().stream().map(ObjectBlock::position).sorted().toList());
assertEquals(List.of("minecraft:oak_stairs[facing=south]", "minecraft:oak_stairs[facing=west]"),
mirrored.blocks().stream().map(block -> block.state().key()).sorted().toList());
assertEquals(new BlockPosition(-1, 0, -1), mirrored.markers().getFirst().position());
}
@Test
public void mirrorsComeBeforeRotation() {
GeneratedObject object = new GeneratedObject(new BlockBox(0, 0, 0, 2, 1, 1), new BlockPosition(0, 0, 0),
List.of(new ObjectBlock(new BlockPosition(1, 0, 0), LOG)), List.of());
GeneratedObject both = transformer.transform(object, new ObjectTransform(0, 90, 0, 1, 1, 1, true, false, false));
// Mirrored first (1,0,0) -> (-1,0,0), then a quarter turn about Y sends +X to -Z, so -X goes to +Z.
assertEquals(new BlockPosition(0, 0, 1), both.blocks().getFirst().position());
}
@Test
public void aMirrorTheBlockIgnoresLeavesItsStateAsItWas() {
GeneratedObject mirrored = transformer.transform(tree, mirrored(true, false));
assertEquals(List.of(LEAVES, LOG), mirrored.blocks().stream().map(ObjectBlock::state).toList());
assertEquals(List.of(new BlockPosition(-1, 1, 0), new BlockPosition(0, 0, 0)),
mirrored.blocks().stream().map(ObjectBlock::position).toList());
}
@Test
public void scalingIsRefused() {
assertThrows(UnsupportedOperationException.class,
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 2, 1, 1, false, false, false)));
}
@@ -213,6 +213,16 @@ public class TreePlausibilizerTest {
return false;
}
@Override
public PlatformBlockState mirrorX() {
return this;
}
@Override
public PlatformBlockState mirrorZ() {
return this;
}
@Override
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
return false;
@@ -59,6 +59,8 @@ public final class UsableBlockPlatform {
when(block.materialKey()).thenReturn(key.split("\\[", 2)[0]);
when(block.isSolid()).thenReturn(!key.contains("air"));
when(block.isOccluding()).thenReturn(!key.contains("air"));
when(block.mirrorX()).thenReturn(block);
when(block.mirrorZ()).thenReturn(block);
return block;
}
}
@@ -190,6 +190,16 @@ public final class StubPlatform implements IrisPlatform {
|| blockKey.endsWith("anvil");
}
@Override
public PlatformBlockState mirrorX() {
return mirrorState(this, "east", "west", 16);
}
@Override
public PlatformBlockState mirrorZ() {
return mirrorState(this, "north", "south", 8);
}
/** The stub has no shapes: every solid block is sturdy on every face. */
@Override
public boolean canAttach(PlatformBlockState support, PlatformFace face) {
@@ -283,6 +293,33 @@ public final class StubPlatform implements IrisPlatform {
return StubBlockState.of(parsed.serialize());
}
/**
* Swaps what the two sides name: {@code facing}, and the connected side properties of fences and the like,
* and mirrors the 16 step {@code rotation} of signs and banners. {@code rotationMirror} is the segment that
* maps to itself, 16 for a mirror along X and 8 for one along Z, as Minecraft mirrors them. The stub knows
* no other shape-dependent property.
*/
private static PlatformBlockState mirrorState(StubBlockState state, String side, String opposite, int rotationMirror) {
ParsedState parsed = ParsedState.parse(state.key());
Map<String, String> properties = parsed.properties();
String facing = properties.get("facing");
if (side.equals(facing) || opposite.equals(facing)) {
properties.put("facing", side.equals(facing) ? opposite : side);
}
String sideValue = properties.get(side);
String oppositeValue = properties.get(opposite);
if (sideValue != null && oppositeValue != null) {
properties.put(side, oppositeValue);
properties.put(opposite, sideValue);
}
String rotation = properties.get("rotation");
if (rotation != null) {
properties.put("rotation", Integer.toString(Math.floorMod(rotationMirror - Integer.parseInt(rotation), 16)));
}
String mirrored = parsed.serialize();
return mirrored.equals(state.key()) ? state : StubBlockState.of(mirrored);
}
private static PlatformBlockState mergeStates(PlatformBlockState base, PlatformBlockState update) {
if (base == null) {
return update;
@@ -10,6 +10,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public final class StubPlatformStateTest {
@@ -84,6 +85,21 @@ public final class StubPlatformStateTest {
assertFalse(torch.canAttach(state("minecraft:air"), PlatformFace.NORTH));
}
@Test
public void mirrorsFacingSidesAndRotationLikeMinecraftDoes() {
assertEquals("minecraft:oak_stairs[facing=west,half=bottom]", state("minecraft:oak_stairs[facing=east,half=bottom]").mirrorX().key());
assertEquals("minecraft:oak_stairs[facing=north,half=bottom]", state("minecraft:oak_stairs[facing=north,half=bottom]").mirrorX().key());
assertEquals("minecraft:oak_stairs[facing=south,half=bottom]", state("minecraft:oak_stairs[facing=north,half=bottom]").mirrorZ().key());
assertEquals("minecraft:oak_fence[north=true,east=false,south=false,west=false]",
state("minecraft:oak_fence[north=true,east=false,south=false,west=false]").mirrorX().key());
assertEquals("minecraft:oak_fence[north=false,east=false,south=true,west=false]",
state("minecraft:oak_fence[north=true,east=false,south=false,west=false]").mirrorZ().key());
assertEquals("minecraft:oak_sign[rotation=12]", state("minecraft:oak_sign[rotation=4]").mirrorX().key());
assertEquals("minecraft:oak_sign[rotation=4]", state("minecraft:oak_sign[rotation=4]").mirrorZ().key());
PlatformBlockState stone = state("minecraft:stone");
assertSame("a block with nothing to mirror is itself", stone, stone.mirrorX());
}
private PlatformBlockState state(String key) {
return IrisPlatforms.get().registries().block(key);
}
@@ -182,6 +182,20 @@ public interface PlatformBlockState {
*/
boolean canPlaceOnto(PlatformBlockState onto);
/**
* This state as it looks when the block is mirrored along X, so that a block at x lands at -x: east and west
* swap, and so does everything that faces or leans toward them (stairs, doors, signs, vines). The platform's
* own mirror decides what changes. Returns this state when mirroring changes nothing. Never mutates the
* receiver.
*/
PlatformBlockState mirrorX();
/**
* This state as it looks when the block is mirrored along Z, so that a block at z lands at -z: north and south
* swap, and so does everything that faces or leans toward them. See {@link #mirrorX()}.
*/
PlatformBlockState mirrorZ();
/**
* Whether this block can attach to the {@code face} of {@code support}, ignoring every other neighbour: the
* support's face is sturdy enough to hold a torch, a ladder or a button against it, which is the platform's own