mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
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:
1 parent
443ed9af65
commit
ccdfad8ba5
6 files changed
+289
-8
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user