Clear deferred custom block and tile records under native structure writes

Native structures overwrite blocks that still have deferred records in the mantle,
so the later materialization pass replayed them onto the structure. Record the
positions each structure placement wrote and drop the records there.

Upstream: ff537f4ab
This commit is contained in:
piexel committed 2026-11-10 01:18:03 +01:00
1 parent 443ed9af65
commit ccdfad8ba5
6 files changed
+289 -8

No files matched your search

@@ -10,6 +10,7 @@ import art.arcane.iris.engine.framework.NativeStructureGenerationPolicy;
import art.arcane.iris.engine.framework.NativeStructureLayouts;
import art.arcane.iris.engine.framework.nativefit.NativeStructureFitting;
import art.arcane.iris.engine.IrisEngine;
import art.arcane.iris.engine.mantle.DeferredPlacementRecords;
import art.arcane.iris.engine.platform.BukkitChunkGenerator;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.nativegen.NativeStructureFitBridge;
@@ -906,7 +907,7 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
private void placeVanillaStructure(WorldGenLevel world, StructureManager structureManager, WorldgenRandom random,
BoundingBox area, ChunkPos chunkPos, String structureId,
NativePlacement placement) {
WorldGenLevel boundedWorld = NativeStructureWorldgenAccess.create(
NativeStructureWorldgenAccess boundedWorld = NativeStructureWorldgenAccess.create(
world, chunkPos, worldgenSurfaceHeight(), worldgenFloorHeight());
world.setCurrentlyGenerating(() -> "Iris native structure " + structureId);
try {
@@ -916,6 +917,7 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
getSeaLevel(), area.minY(), area.maxY() + 1);
start.placeInChunk(boundedWorld, structureManager, this, random, area, chunkPos);
} finally {
DeferredPlacementRecords.clearWritten(engine, boundedWorld.writtenBlocks());
world.setCurrentlyGenerating(null);
}
}
@@ -1,5 +1,6 @@
package art.arcane.iris.core.nms.v26_2_R1;
import art.arcane.iris.engine.mantle.DeferredPlacementRecords.WrittenBlocks;
import art.arcane.iris.nativegen.WorldgenTerrainHeightmaps;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -71,6 +72,7 @@ final class NativeStructureWorldgenAccess implements WorldGenLevel {
private final LevelTickAccess<Fluid> fluidTicks;
private final Long2LongOpenHashMap terrainHeights;
private final Long2ObjectOpenHashMap<ChunkAccess> outsideChunks;
private final WrittenBlocks writtenBlocks = new WrittenBlocks();
private NativeStructureWorldgenAccess(WorldGenLevel delegate, Boundary boundary) {
this.delegate = Objects.requireNonNull(delegate, "Native structure world access requires a delegate");
@@ -424,17 +426,28 @@ final class NativeStructureWorldgenAccess implements WorldGenLevel {
@Override
public boolean setBlock(BlockPos position, BlockState state, int updateFlags, int updateLimit) {
return isWritable(position) && delegate.setBlock(position, state, updateFlags, updateLimit);
return recordWrite(position, isWritable(position) && delegate.setBlock(position, state, updateFlags, updateLimit));
}
@Override
public boolean removeBlock(BlockPos position, boolean move) {
return isWritable(position) && delegate.removeBlock(position, move);
return recordWrite(position, isWritable(position) && delegate.removeBlock(position, move));
}
@Override
public boolean destroyBlock(BlockPos position, boolean drop, Entity source, int updateLimit) {
return isWritable(position) && delegate.destroyBlock(position, drop, source, updateLimit);
return recordWrite(position, isWritable(position) && delegate.destroyBlock(position, drop, source, updateLimit));
}
WrittenBlocks writtenBlocks() {
return writtenBlocks;
}
private boolean recordWrite(BlockPos position, boolean written) {
if (written) {
writtenBlocks.add(position.getX(), position.getY(), position.getZ());
}
return written;
}
@Override
@@ -21,6 +21,7 @@ package art.arcane.iris.modded;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.core.modules.ModuleServices;
import art.arcane.iris.engine.framework.NativeStructureGenerationPolicy;
import art.arcane.iris.engine.mantle.DeferredPlacementRecords;
import art.arcane.iris.engine.framework.NativeStructureLayouts;
import art.arcane.iris.engine.framework.nativefit.NativeStructureFitting;
import art.arcane.iris.engine.framework.NativeStructureOwnershipRecord;
@@ -401,7 +402,7 @@ final class ModdedNativeStructureStage {
String structureId, NativePlacement placement) {
Engine current = generator.engine();
int runtimeMinY = world.getMinY();
WorldGenLevel boundedWorld = ModdedNativeStructureWorldgenAccess.create(
ModdedNativeStructureWorldgenAccess boundedWorld = ModdedNativeStructureWorldgenAccess.create(
world, chunkPos,
worldgenSurfaceHeight(current, runtimeMinY),
worldgenFloorHeight(current, runtimeMinY));
@@ -413,6 +414,7 @@ final class ModdedNativeStructureStage {
generator.getSeaLevel(), area.minY(), area.maxY() + 1);
start.placeInChunk(boundedWorld, structureManager, generator, random, area, chunkPos);
} finally {
DeferredPlacementRecords.clearWritten(current, boundedWorld.writtenBlocks());
world.setCurrentlyGenerating(null);
}
}
@@ -1,5 +1,6 @@
package art.arcane.iris.modded;
import art.arcane.iris.engine.mantle.DeferredPlacementRecords.WrittenBlocks;
import art.arcane.iris.nativegen.WorldgenTerrainHeightmaps;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -70,6 +71,7 @@ final class ModdedNativeStructureWorldgenAccess implements WorldGenLevel {
private final LevelTickAccess<Fluid> fluidTicks;
private final Long2LongOpenHashMap terrainHeights;
private final Long2ObjectOpenHashMap<ChunkAccess> outsideChunks;
private final WrittenBlocks writtenBlocks = new WrittenBlocks();
private ModdedNativeStructureWorldgenAccess(WorldGenLevel delegate, Boundary boundary) {
this.delegate = Objects.requireNonNull(delegate, "Native structure world access requires a delegate");
@@ -405,17 +407,28 @@ final class ModdedNativeStructureWorldgenAccess implements WorldGenLevel {
@Override
public boolean setBlock(BlockPos position, BlockState state, int updateFlags, int updateLimit) {
return isWritable(position) && delegate.setBlock(position, state, updateFlags, updateLimit);
return recordWrite(position, isWritable(position) && delegate.setBlock(position, state, updateFlags, updateLimit));
}
@Override
public boolean removeBlock(BlockPos position, boolean move) {
return isWritable(position) && delegate.removeBlock(position, move);
return recordWrite(position, isWritable(position) && delegate.removeBlock(position, move));
}
@Override
public boolean destroyBlock(BlockPos position, boolean drop, Entity source, int updateLimit) {
return isWritable(position) && delegate.destroyBlock(position, drop, source, updateLimit);
return recordWrite(position, isWritable(position) && delegate.destroyBlock(position, drop, source, updateLimit));
}
WrittenBlocks writtenBlocks() {
return writtenBlocks;
}
private boolean recordWrite(BlockPos position, boolean written) {
if (written) {
writtenBlocks.add(position.getX(), position.getY(), position.getZ());
}
return written;
}
@Override
@@ -0,0 +1,114 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.engine.mantle;
import art.arcane.iris.core.link.Identifier;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.util.project.matter.TileWrapper;
import art.arcane.volmlib.util.mantle.runtime.Mantle;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
import art.arcane.volmlib.util.matter.Matter;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
/**
* Drops the deferred custom-block and tile records that a native structure write has overwritten, so the
* chunk's later materialization pass does not replay them onto the structure's blocks.
*/
public final class DeferredPlacementRecords {
private DeferredPlacementRecords() {
}
public static void clearWritten(Engine engine, WrittenBlocks written) {
if (written.isEmpty()) {
return;
}
Mantle<Matter> mantle = engine.getMantle().getMantle();
int minHeight = engine.getMinHeight();
Long2ObjectOpenHashMap<MantleChunk<Matter>> acquired = new Long2ObjectOpenHashMap<>();
try {
for (int index = 0; index < written.size(); index++) {
int x = written.x(index);
int z = written.z(index);
MantleChunk<Matter> chunk = acquired.computeIfAbsent(chunkKey(x >> 4, z >> 4),
key -> mantle.getChunk(x >> 4, z >> 4).use());
synchronized (chunk) {
clearRecords(chunk, x & 15, written.y(index) - minHeight, z & 15);
}
}
} finally {
acquired.values().forEach(MantleChunk::release);
}
}
private static long chunkKey(int chunkX, int chunkZ) {
return ((long) chunkX << 32) | (chunkZ & 0xFFFFFFFFL);
}
private static void clearRecords(MantleChunk<Matter> chunk, int x, int y, int z) {
if (y < 0 || (y >> 4) >= chunk.sectionCount() || !chunk.exists(y >> 4)) {
return;
}
Matter section = chunk.get(y >> 4);
if (section == null) {
return;
}
clearRecord(section, Identifier.class, x, y & 15, z);
clearRecord(section, TileWrapper.class, x, y & 15, z);
}
private static void clearRecord(Matter section, Class<?> type, int x, int y, int z) {
if (section.hasSlice(type)) {
section.getSlice(type).set(x, y, z, null);
}
}
/** World positions a native structure wrote, in write order, duplicates allowed. */
public static final class WrittenBlocks {
private final IntArrayList xs = new IntArrayList();
private final IntArrayList ys = new IntArrayList();
private final IntArrayList zs = new IntArrayList();
public void add(int x, int y, int z) {
xs.add(x);
ys.add(y);
zs.add(z);
}
public boolean isEmpty() {
return xs.isEmpty();
}
int size() {
return xs.size();
}
int x(int index) {
return xs.getInt(index);
}
int y(int index) {
return ys.getInt(index);
}
int z(int index) {
return zs.getInt(index);
}
}
}
@@ -0,0 +1,137 @@
package art.arcane.iris.engine.mantle;
import art.arcane.iris.core.link.Identifier;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.mantle.DeferredPlacementRecords.WrittenBlocks;
import art.arcane.iris.spi.IrisPlatform;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.spi.PlatformRegistries;
import art.arcane.iris.util.project.matter.TileWrapper;
import art.arcane.volmlib.util.mantle.runtime.Mantle;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
import art.arcane.volmlib.util.matter.Matter;
import art.arcane.volmlib.util.matter.MatterSlice;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
public class DeferredPlacementRecordsTest {
private static final int MIN_HEIGHT = -64;
private static final int SECTION_COUNT = 24;
private Engine engine;
private Mantle<Matter> mantle;
private MantleChunk<Matter> chunk;
private Matter section;
private MatterSlice<Identifier> identifiers;
private MatterSlice<TileWrapper> tiles;
@After
public void tearDown() {
IrisPlatforms.unbind();
}
@Before
@SuppressWarnings("unchecked")
public void setUp() {
IrisPlatforms.unbind();
PlatformRegistries registries = mock(PlatformRegistries.class);
IrisPlatform platform = mock(IrisPlatform.class);
when(registries.block(anyString())).thenReturn(mock(PlatformBlockState.class));
when(platform.registries()).thenReturn(registries);
IrisPlatforms.bind(platform);
engine = mock(Engine.class);
EngineMantle engineMantle = mock(EngineMantle.class);
mantle = mock(Mantle.class);
chunk = mock(MantleChunk.class);
section = mock(Matter.class);
identifiers = mock(MatterSlice.class);
tiles = mock(MatterSlice.class);
when(engine.getMantle()).thenReturn(engineMantle);
when(engine.getMinHeight()).thenReturn(MIN_HEIGHT);
when(engineMantle.getMantle()).thenReturn(mantle);
when(mantle.getChunk(anyInt(), anyInt())).thenReturn(chunk);
when(chunk.use()).thenReturn(chunk);
when(chunk.sectionCount()).thenReturn(SECTION_COUNT);
when(chunk.exists(anyInt())).thenReturn(true);
when(chunk.get(anyInt())).thenReturn(section);
when(section.hasSlice(Identifier.class)).thenReturn(true);
when(section.hasSlice(TileWrapper.class)).thenReturn(true);
when(section.getSlice(Identifier.class)).thenReturn(identifiers);
when(section.getSlice(TileWrapper.class)).thenReturn(tiles);
}
@Test
public void writtenPositionsLoseTheirCustomAndTileRecords() {
WrittenBlocks written = new WrittenBlocks();
written.add(19, 70, 5);
DeferredPlacementRecords.clearWritten(engine, written);
int mantleY = 70 - MIN_HEIGHT;
verify(chunk).get(mantleY >> 4);
verify(identifiers).set(3, mantleY & 15, 5, null);
verify(tiles).set(3, mantleY & 15, 5, null);
verify(mantle).getChunk(1, 0);
verify(chunk).release();
}
@Test
public void eachTouchedChunkIsAcquiredOnceAndReleasedOnce() {
WrittenBlocks written = new WrittenBlocks();
written.add(1, 70, 1);
written.add(2, 71, 2);
written.add(-1, 70, 1);
DeferredPlacementRecords.clearWritten(engine, written);
verify(mantle).getChunk(0, 0);
verify(mantle).getChunk(-1, 0);
verify(chunk, times(2)).release();
}
@Test
public void positionsBelowTheWorldOrInMissingSectionsAreIgnored() {
when(chunk.exists(anyInt())).thenReturn(false);
WrittenBlocks written = new WrittenBlocks();
written.add(1, MIN_HEIGHT - 1, 1);
written.add(1, 70, 1);
DeferredPlacementRecords.clearWritten(engine, written);
verify(identifiers, never()).set(anyInt(), anyInt(), anyInt(), any());
verify(tiles, never()).set(anyInt(), anyInt(), anyInt(), any());
}
@Test
public void slicesThatWereNeverCreatedAreNotCreatedByClearing() {
when(section.hasSlice(Identifier.class)).thenReturn(false);
WrittenBlocks written = new WrittenBlocks();
written.add(1, 70, 1);
DeferredPlacementRecords.clearWritten(engine, written);
verify(section, never()).getSlice(Identifier.class);
verify(tiles).set(1, (70 - MIN_HEIGHT) & 15, 1, null);
}
@Test
public void noWritesLeaveTheMantleUntouched() {
DeferredPlacementRecords.clearWritten(engine, new WrittenBlocks());
verifyNoInteractions(mantle);
}
}