Read settings per column with every level as its own entry, add the mantle pipeline, host types and the one-option shorthand

Settings no longer resolve to one value per type. FeatureSettings.at(type, x, z) returns one entry per
level that declares the type at that column (dimension, region, surface biome, placement), each with
its level, source and value; levels stack and a module decides what several mean. Conditional stage
bodies receive every configured entry. Chunk and mantle runners collect a chunk's settings themselves,
so a stage runs when any column of the chunk configures it.

Work that writes the mantle or reaches beyond its chunk moves to a new MANTLE pipeline (Stage.mantle,
MantleRunner) that the host runs around a chunk before it generates. Chunk stages may no longer declare
a reach or mantle writes and see the mantle read-only.

Built-in settings may hold host types the host describes itself (ModuleHost.describingHostTypesIn,
ValueKind.HostKind); pack modules cannot. FeatureSchema.shorthand() names the only option of settings
packs may write as a bare value.
This commit is contained in:
piexel committed 2026-10-08 11:31:03 +02:00
1 parent 09df1af88e
commit 97ac2a2982
61 files changed
+1316 -184

No files matched your search

@@ -1,28 +1,54 @@
package com.volmit.example;
import com.volmit.FeatureAttachment.Target;
import com.volmit.IrisFeature;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.config.Feature;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.example.chunk.GrassModule;
import com.volmit.example.chunk.GrassSettings;
import com.volmit.example.chunk.TerrainModule;
import com.volmit.example.chunk.VeinModule;
import com.volmit.example.chunk.VeinSettings;
import com.volmit.host.ArrayHunk;
import com.volmit.host.ChunkRunner;
import com.volmit.host.ConfiguredSettings;
import com.volmit.host.DiscoveredModule;
import com.volmit.host.LoadedModules;
import com.volmit.host.MantleRunner;
import com.volmit.host.StageGraphBuilder;
import com.volmit.hunk.Hunk;
import com.volmit.mantle.BlockBox;
import com.volmit.resource.ResourceId;
import com.volmit.stage.Pipeline;
import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import com.volmit.support.ColumnSettings;
import com.volmit.support.MemoryMantle;
import com.volmit.support.TestContexts;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** The chunk pipeline runs on the same graph machinery as placement. */
/** The chunk pipeline runs on the same graph machinery as placement; the mantle pipeline feeds it. */
class ChunkPipelineTest {
private static final int SIZE = 4;
private static final int HEIGHT = 8;
private static final int CHUNK = 16;
private static final int MIN_Y = -64;
private static final int MAX_Y = 320;
private static final BlockState AIR = new BlockState("minecraft:air");
private static final BlockState GRASS = new BlockState("minecraft:grass_block");
private static final ResourceId DIMENSION = ResourceId.parse("pack:dimension/overworld");
private static final ResourceId PLAINS = ResourceId.parse("pack:biome/plains");
private static final ResourceId SWAMP = ResourceId.parse("pack:biome/swamp");
@Feature(id = "test:levels", name = "Levels", targets = {Target.DIMENSION, Target.BIOME})
static final class Marker implements IrisFeature {}
private static ChunkRunner runner() {
var builder = new StageGraphBuilder();
@@ -35,15 +61,19 @@ class ChunkPipelineTest {
return new ChunkRunner(graph);
}
private static Hunk<BlockState> generate(ConfiguredSettings settings) {
private static Hunk<BlockState> generate(ColumnSettings settings) {
var blocks = new ArrayHunk<>(BlockState.class, SIZE, HEIGHT, SIZE, AIR);
runner().generate(TestContexts.generation(Map.of(BlockState.class, blocks)), TestContexts.SEED, settings);
runner().generate(TestContexts.generation(Map.of(BlockState.class, blocks), settings, null), TestContexts.SEED);
return blocks;
}
private static Entry<GrassSettings> grass(ResourceId biome) {
return new Entry<>(Target.BIOME, biome, new GrassSettings());
}
@Test
void decorationReadsTerrainOutputWhenConfigured() {
var blocks = generate(ConfiguredSettings.of(new GrassSettings()));
var blocks = generate(ColumnSettings.everywhere(grass(PLAINS)));
for (int x = 0; x < SIZE; x++) {
for (int z = 0; z < SIZE; z++) {
@@ -57,9 +87,90 @@ class ChunkPipelineTest {
@Test
void decorationIsInactiveWithoutItsSettings() {
var blocks = generate(ConfiguredSettings.none());
var blocks = generate(ColumnSettings.NONE);
int height = TerrainModule.surfaceHeight(0, 0);
assertEquals(TerrainModule.STONE, blocks.get(0, height - 1, 0));
}
@Test
void aStageRunsWhenAnyColumnHasItsSettingsAndReadsThemPerColumn() {
var farCorner = new ColumnSettings((x, z) -> x == CHUNK - 1 && z == CHUNK - 1 ? List.of(grass(PLAINS)) : List.of());
var blocks = generate(farCorner);
assertEquals(TerrainModule.STONE, blocks.get(0, TerrainModule.surfaceHeight(0, 0) - 1, 0),
"the stage ran because one column has grass, but this column has none");
var border = new ColumnSettings((x, z) -> x < 2 ? List.of(grass(PLAINS)) : List.of());
blocks = generate(border);
assertEquals(GRASS, blocks.get(1, TerrainModule.surfaceHeight(1, 0) - 1, 0));
assertEquals(TerrainModule.STONE, blocks.get(2, TerrainModule.surfaceHeight(2, 0) - 1, 0));
}
@Test
void levelsStackAndTheBodyGetsEveryDistinctEntryOfTheChunkInLevelOrder() {
var dimension = new Entry<>(Target.DIMENSION, DIMENSION, new Marker());
var plains = new Entry<>(Target.BIOME, PLAINS, new Marker());
var swamp = new Entry<>(Target.BIOME, SWAMP, new Marker());
var received = new ArrayList<List<ResourceId>>();
var stage = Stage.chunk("test:levels/read");
IrisModule module = new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("test:levels", "Levels", "1"); }
public void install(StageGraph graph) {
graph.define(stage).when(Marker.class).run((context, entries) -> {
received.add(entries.stream().map(Entry::source).toList());
received.add(context.settings().at(Marker.class, 0, 0).stream().map(Entry::source).toList());
});
}
};
var builder = new StageGraphBuilder();
builder.install(DiscoveredModule.builtIn(module));
var settings = new ColumnSettings((x, z) -> List.of(dimension, x < 8 ? swamp : plains));
new ChunkRunner(builder.resolve(Pipeline.CHUNK)).generate(TestContexts.generation(Map.of(), settings, null), TestContexts.SEED);
assertEquals(List.of(List.of(DIMENSION, SWAMP, PLAINS), List.of(DIMENSION, SWAMP)), received,
"the body sees every level of every column, a column read that column's levels");
}
@Test
void mantleStagesWriteBeyondTheirChunkAndChunkStagesReadTheirOwnPart() {
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(new VeinModule())), Map.of(), Set.of(), Set.of());
assertEquals(VeinModule.REACH, loaded.mantle().reach().blocks());
assertEquals(Map.of(VeinModule.ORE.id(), VeinModule.REACH), loaded.mantle().reach().mantleLayers());
var mantle = new MemoryMantle();
var veins = ColumnSettings.everywhere(new Entry<>(Target.BIOME, PLAINS, new VeinSettings(12, -60)));
var reachArea = new BlockBox(-VeinModule.REACH, MIN_Y, -VeinModule.REACH, CHUNK + VeinModule.REACH, MAX_Y, CHUNK + VeinModule.REACH);
new MantleRunner(loaded.mantle()).generate(TestContexts.mantle(0, 0, veins, mantle.within(reachArea)), TestContexts.SEED);
assertEquals(1, mantle.get(VeinModule.ORE, 18, -60, 7), "the vein runs on into the next chunk");
var blocks = new ArrayHunk<>(BlockState.class, CHUNK, HEIGHT, CHUNK, AIR);
var chunkArea = new BlockBox(0, MIN_Y, 0, CHUNK, MAX_Y, CHUNK);
new ChunkRunner(loaded.chunk()).generate(
TestContexts.generation(Map.of(BlockState.class, blocks), ColumnSettings.NONE, mantle.within(chunkArea)), TestContexts.SEED);
assertEquals(VeinModule.ORE_BLOCK, blocks.get(7, 4, 7));
assertEquals(VeinModule.ORE_BLOCK, blocks.get(15, 4, 7));
assertEquals(AIR, blocks.get(6, 4, 7));
}
@Test
void chunkStagesReadTheMantleButCannotWriteIt() {
var stage = Stage.chunk("test:writer/work");
var mantle = new MemoryMantle();
IrisModule module = new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("test:writer", "Writer", "1"); }
public void install(StageGraph graph) {
graph.define(stage).run(context -> context.mantle().layer(VeinModule.ORE).set(0, 0, 0, 1));
}
};
var builder = new StageGraphBuilder();
builder.install(DiscoveredModule.builtIn(module));
var inputs = TestContexts.generation(Map.of(), ColumnSettings.NONE, mantle.within(new BlockBox(0, MIN_Y, 0, CHUNK, MAX_Y, CHUNK)));
var failure = assertThrows(UnsupportedOperationException.class,
() -> new ChunkRunner(builder.resolve(Pipeline.CHUNK)).generate(inputs, TestContexts.SEED));
assertEquals("Chunk stages read the mantle but do not write it; write it from a mantle stage (Stage.mantle),"
+ " which runs before the chunks it reaches", failure.getMessage());
assertNull(mantle.get(VeinModule.ORE, 0, 0, 0));
}
}
@@ -38,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.*;
/** The ported slice of Iris's place() lifecycle, composed only from module-owned stages. */
class PlacementLifecyclePortTest {
private static final ResourceId PLACEMENT = ResourceId.parse("pack:object/tree");
private static final BlockState PLANKS = new BlockState("minecraft:oak_planks");
private static final int ANCHOR_GROUND = 10;
private static final int SURROUNDING_GROUND = 7;
@@ -149,7 +150,7 @@ class PlacementLifecyclePortTest {
@Test
void placesObjectThenStiltSnowAndMossFromTypedOutputs() {
var edit = place(resolve(PlacementModules.all()),
ConfiguredSettings.of(new StiltSettings(), new SnowSettings(2), new MossSettings()));
ConfiguredSettings.placement(PLACEMENT, new StiltSettings(), new SnowSettings(2), new MossSettings()));
var bottom = placed(0, 0, 0);
var corner = placed(1, 0, 0);
@@ -191,17 +192,17 @@ class PlacementLifecyclePortTest {
modules.add(capModule);
var graph = resolve(modules);
var withoutStilt = place(graph, ConfiguredSettings.of(new SnowSettings(), new MossSettings()));
var withoutStilt = place(graph, ConfiguredSettings.placement(PLACEMENT, new SnowSettings(), new MossSettings()));
assertEquals(TestTerrain.AIR, withoutStilt.publishedAt(5, 20, 5));
assertEquals(MossModule.MOSS, withoutStilt.publishedAt(0, ANCHOR_GROUND - 1, 0));
var withStilt = place(graph, ConfiguredSettings.of(new StiltSettings()));
var withStilt = place(graph, ConfiguredSettings.placement(PLACEMENT, new StiltSettings()));
assertEquals(PLANKS, withStilt.publishedAt(5, 20, 5));
}
@Test
void loadingThePortCatalogsEveryConditioningSettingsType() {
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of(), Set.of());
var loaded = LoadedModules.load(PlacementModules.builtIns(), Map.of(), Set.of(), Set.of());
assertEquals(List.of("example:moss", "iris:snow", "iris:stilt"),
loaded.catalog().settings().stream().map(settings -> settings.id().toString()).toList());
@@ -3,12 +3,17 @@ package com.volmit.example.chunk;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.stage.ChunkStage;
import com.volmit.stage.ChunkStageContext;
import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import java.util.List;
/** Third-party decoration: tops terrain columns using the terrain module's typed output. */
/** Third-party decoration: tops terrain columns using the terrain module's typed output. It runs where any
* column of the chunk has grass and reads the settings per column, so a biome border inside the chunk
* keeps grass on its own side.
*/
public final class GrassModule implements IrisModule {
public static final ChunkStage<Void> TOP = Stage.chunk("example:grass/top");
@@ -25,16 +30,17 @@ public final class GrassModule implements IrisModule {
.run(GrassModule::cover);
}
private static void cover(ChunkStageContext context, GrassSettings grass) {
var top = grass.block();
private static void cover(ChunkStageContext context, List<Entry<GrassSettings>> inChunk) {
var heights = context.output(TerrainModule.SURFACE);
var blocks = context.hunk(BlockState.class);
for (int x = 0; x < blocks.getWidth(); x++) {
for (int z = 0; z < blocks.getDepth(); z++) {
var here = context.settings().at(GrassSettings.class, context.originX() + x, context.originZ() + z);
int height = heights.at(x, z);
if (height > 0) {
blocks.set(x, height - 1, z, top);
if (here.isEmpty() || height <= 0) {
continue;
}
blocks.set(x, height - 1, z, here.getLast().value().block()); // the most specific level wins here
}
}
}
@@ -0,0 +1,77 @@
package com.volmit.example.chunk;
import com.volmit.FeatureRegistry;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.mantle.BlockBox;
import com.volmit.mantle.MantleCodec;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLifetime;
import com.volmit.stage.ChunkStage;
import com.volmit.stage.ChunkStageContext;
import com.volmit.stage.MantleStage;
import com.volmit.stage.MantleStageContext;
import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.List;
/** Work across chunks: a mantle stage plans veins into the mantle, past its own chunk, and a chunk stage
* later turns its own chunk's part of the mantle into blocks. The two pipelines meet only in the mantle.
*/
public final class VeinModule implements IrisModule {
public static final MantleKey<Integer> ORE = new MantleKey<>("example:veins/ore", Integer.class);
public static final MantleStage<Void> PLAN = Stage.mantle("example:veins/plan");
public static final ChunkStage<Void> PLACE = Stage.chunk("example:veins/place");
public static final BlockState ORE_BLOCK = new BlockState("minecraft:iron_ore");
public static final int REACH = 16;
private static final int CHUNK_WIDTH = 16;
private static final int CHUNK_CENTRE = 7;
private static final int PLANNED = 1;
@Override
public ModuleInfo info() {
return ModuleInfo.of("example:veins", "Veins", "1.0.0");
}
@Override
public void install(StageGraph graph) {
graph.define(PLAN).when(VeinSettings.class).reach(REACH).writesMantle(ORE).run(VeinModule::plan);
graph.define(PLACE).run(VeinModule::place);
}
@Override
public void register(FeatureRegistry registry) {
registry.registerMantle(ORE, MantleLifetime.GENERATION, new IntegerCodec());
}
/** One vein per level that configures veins at the chunk's centre column: levels stack. */
private static void plan(MantleStageContext context, List<Entry<VeinSettings>> inChunk) {
int startX = context.originX() + CHUNK_CENTRE;
int z = context.originZ() + CHUNK_CENTRE;
var ore = context.mantle().layer(ORE);
for (var vein : context.settings().at(VeinSettings.class, startX, z)) {
for (int step = 0; step < Math.min(vein.value().length(), REACH + CHUNK_WIDTH - CHUNK_CENTRE); step++) {
ore.set(startX + step, vein.value().y(), z, PLANNED);
}
}
}
private static void place(ChunkStageContext context) {
var blocks = context.hunk(BlockState.class);
var chunk = new BlockBox(context.originX(), context.originY(), context.originZ(),
context.originX() + blocks.getWidth(), context.originY() + blocks.getHeight(), context.originZ() + blocks.getDepth());
context.mantle().layer(ORE).visit(chunk, (x, y, z, planned) ->
blocks.set(x - context.originX(), y - context.originY(), z - context.originZ(), ORE_BLOCK));
}
private static final class IntegerCodec implements MantleCodec<Integer> {
public int version() { return 0; }
public void write(DataOutput output, Integer value) throws IOException { output.writeInt(value); }
public Integer read(DataInput input, int storedVersion) throws IOException { return input.readInt(); }
}
}
@@ -0,0 +1,31 @@
package com.volmit.example.chunk;
import com.volmit.FeatureAttachment;
import com.volmit.IrisFeature;
import com.volmit.config.Desc;
import com.volmit.config.Feature;
@Desc("Plans a horizontal ore vein from the centre of each chunk, which may run into the next chunk")
@Feature(id = "example:veins", name = "Veins", targets = {FeatureAttachment.Target.DIMENSION, FeatureAttachment.Target.BIOME})
public final class VeinSettings implements IrisFeature {
@Desc("Blocks along X, starting at the chunk's centre column")
private int length = 12;
@Desc("World height of the vein")
private int y = -60;
public VeinSettings() {}
public VeinSettings(int length, int y) {
this.length = length;
this.y = y;
}
public int length() {
return length;
}
public int y() {
return y;
}
}
@@ -0,0 +1,13 @@
package com.volmit.example.hosttypes;
import java.util.List;
/** Stands for a host class such as Iris's material palette: the host describes it in studio itself. */
public final class Palette {
private List<String> blocks = List.of("minecraft:stone");
private Object anything;
public List<String> blocks() {
return blocks;
}
}
@@ -3,6 +3,7 @@ package com.volmit.example.placement;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.placement.PlacedBlock;
import com.volmit.stage.PlacementStage;
import com.volmit.stage.PlacementStageContext;
@@ -10,6 +11,7 @@ import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import com.volmit.stage.StageRef;
import com.volmit.world.BlockPosition;
import java.util.List;
import java.util.Optional;
/** Third-party module: turns the ground under the anchor into moss. It requires the core
@@ -33,7 +35,8 @@ public final class MossModule implements IrisModule {
.run(MossModule::spread);
}
private static void spread(PlacementStageContext context, MossSettings moss) {
private static void spread(PlacementStageContext context, List<Entry<MossSettings>> settings) {
var moss = settings.getFirst().value(); // a placement declares its settings once
var anchor = context.output(AnchorModule.HEIGHT).anchor();
var access = context.access();
int groundY = anchor.y() - 1;
@@ -3,6 +3,7 @@ package com.volmit.example.placement;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.block.BlockState;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.placement.PlacedBlock;
import com.volmit.stage.BlockStage;
import com.volmit.stage.PlacementStage;
@@ -11,6 +12,7 @@ import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import com.volmit.stage.StageRef;
import com.volmit.world.BlockPosition;
import java.util.List;
import java.util.Optional;
/** Port of PlacementModuleSnow: collect written tops, then cover them after the finishing work
@@ -40,7 +42,8 @@ public final class SnowModule implements IrisModule {
.run(SnowModule::cover);
}
private static void cover(PlacementStageContext context, SnowSettings snow) {
private static void cover(PlacementStageContext context, List<Entry<SnowSettings>> settings) {
var snow = settings.getFirst().value(); // a placement declares its settings once
var layer = SNOW.withProperty("layers", Integer.toString(snow.layers()));
var access = context.access();
for (var top : context.output(COLLECT).tops()) {
@@ -2,6 +2,7 @@ package com.volmit.example.placement;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.object.ObjectBlock;
import com.volmit.placement.PlacedBlock;
import com.volmit.stage.BlockStage;
@@ -10,6 +11,7 @@ import com.volmit.stage.PlacementStageContext;
import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
import com.volmit.world.BlockPosition;
import java.util.List;
import java.util.Optional;
/** Port of PlacementModuleStilt: collect the lowest layer per block, then fill down after writes. */
@@ -34,7 +36,8 @@ public final class StiltModule implements IrisModule {
.run(StiltModule::fill);
}
private static void fill(PlacementStageContext context, StiltSettings stilt) {
private static void fill(PlacementStageContext context, List<Entry<StiltSettings>> settings) {
var stilt = settings.getFirst().value(); // a placement declares its settings once
var anchor = context.output(AnchorModule.HEIGHT);
for (var block : context.output(BOUNDS).lowestLayer()) {
var bottom = anchor.worldPosition(context.request(), block.position());
@@ -0,0 +1,22 @@
package com.volmit.example.terrain;
import com.volmit.FeatureRegistry;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.stage.StageGraph;
/** A built-in module whose settings use host types; it only registers them. */
public final class LayersModule implements IrisModule {
@Override
public ModuleInfo info() {
return ModuleInfo.of("iris:layers", "Layers", "test");
}
@Override
public void install(StageGraph graph) {}
@Override
public void register(FeatureRegistry registry) {
registry.registerSettings(LayersSettings.class);
}
}
@@ -0,0 +1,19 @@
package com.volmit.example.terrain;
import com.volmit.FeatureAttachment;
import com.volmit.IrisFeature;
import com.volmit.config.Desc;
import com.volmit.config.Feature;
import com.volmit.example.hosttypes.Palette;
import java.util.List;
/** Built-in settings holding host types, written in pack JSON as a bare list: {@code "layers": [...]}. */
@Feature(id = "iris:layers", name = "Layers", alias = "layers", targets = FeatureAttachment.Target.BIOME)
public final class LayersSettings implements IrisFeature {
@Desc("Rock layers from the surface down")
private List<Palette> layers = List.of();
public List<Palette> layers() {
return layers;
}
}
@@ -0,0 +1,43 @@
package com.volmit.host;
import com.volmit.FeatureAttachment.Target;
import com.volmit.IrisFeature;
import com.volmit.config.Feature;
import com.volmit.config.FeatureSettings.Entry;
import com.volmit.resource.ResourceId;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ConfiguredSettingsTest {
private static final ResourceId DIMENSION = ResourceId.parse("pack:dimension/overworld");
private static final ResourceId BIOME = ResourceId.parse("pack:biome/plains");
private static final ResourceId OBJECT = ResourceId.parse("pack:object/tree");
@Feature(id = "test:levels", name = "Levels", targets = {Target.DIMENSION, Target.BIOME, Target.OBJECT_PLACEMENT})
static final class Levels implements IrisFeature {}
@Feature(id = "test:other", name = "Other", targets = Target.BIOME)
static final class Other implements IrisFeature {}
@Test
void entriesAreGroupedByTypeInLevelOrderAndNeverMerged() {
var biome = new Entry<>(Target.BIOME, BIOME, new Levels());
var dimension = new Entry<>(Target.DIMENSION, DIMENSION, new Levels());
var settings = ConfiguredSettings.of(List.of(biome, dimension));
assertEquals(List.of(dimension, biome), settings.find(Levels.class));
assertEquals(List.of(), settings.find(Other.class));
assertEquals(List.of(), ConfiguredSettings.none().find(Levels.class));
}
@Test
void aPlacementDeclaresEachTypeOnce() {
var levels = new Levels();
assertEquals(List.of(new Entry<>(Target.OBJECT_PLACEMENT, OBJECT, levels)),
ConfiguredSettings.placement(OBJECT, levels).find(Levels.class));
var twice = assertThrows(IllegalArgumentException.class, () -> ConfiguredSettings.placement(OBJECT, new Levels(), new Levels()));
assertEquals("Settings configured twice: " + Levels.class.getName(), twice.getMessage());
}
}
@@ -30,7 +30,7 @@ class DiscoveredModuleTest {
var module = new ShiftingIdentity();
var discovered = DiscoveredModule.builtIn(module);
var loaded = LoadedModules.load(List.of(discovered), Map.of(), Set.of());
var loaded = LoadedModules.load(List.of(discovered), Map.of(), Set.of(), Set.of());
assertEquals("first:core", discovered.info().id().toString());
assertEquals(List.of("first:core/mark"),
@@ -128,14 +128,14 @@ class ModuleRegistryTest {
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); }
};
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of());
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of(), Set.of());
assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
assertEquals(1, loaded.catalog().objects().size());
assertEquals(1, loaded.placement().stages().size());
assertTrue(loaded.chunk().stages().isEmpty());
var reserved = Map.of(FeatureAttachment.Target.BIOME, Set.of("shared"));
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved, Set.of()));
assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), reserved, Set.of(), Set.of()));
}
@Test
@@ -180,11 +180,11 @@ class ModuleRegistryTest {
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
};
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(owner), DiscoveredModule.builtIn(user)), Map.of(), Set.of());
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(owner), DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of());
assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
var unowned = assertThrows(IllegalArgumentException.class,
() -> LoadedModules.load(List.of(DiscoveredModule.builtIn(user)), Map.of(), Set.of()));
() -> LoadedModules.load(List.of(DiscoveredModule.builtIn(user)), Map.of(), Set.of(), Set.of()));
assertTrue(unowned.getMessage().contains("which none of the modules naming it in when(...) owns"), unowned.getMessage());
}
@@ -44,7 +44,7 @@ class PackModulesTest {
private static LoadedModules loadWithBuiltIns(PackModules packModules) {
var modules = new ArrayList<>(PlacementModules.builtIns());
modules.addAll(packModules.modules());
return LoadedModules.load(modules, Map.of(), Set.of());
return LoadedModules.load(modules, Map.of(), Set.of(), Set.of());
}
/** Versioned entries of a multi-release jar are read like any class, so they must be real class files. */
@@ -343,7 +343,7 @@ class PackModulesTest {
try (var packModules = PackModules.load(pack)) {
assertEquals(List.of(new MissingClasses(origin("user.jar"), List.of(OPTIONAL_CLASS))), packModules.missingClasses());
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
}
}
@@ -477,7 +477,7 @@ class PackModulesTest {
.writeInto(pack, "beta.jar");
try (var packModules = PackModules.load(pack)) {
var loaded = LoadedModules.load(packModules.modules(), Map.of(), Set.of());
var loaded = LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of());
assertTrue(loaded.placement().stages().stream().anyMatch(stage -> stage.stage().id().toString().equals("beta:core/size")));
var beta = packModules.modules().getLast().module().getClass();
assertEquals(0, beta.getDeclaredField("SHARED").getAnnotations().length,
@@ -494,7 +494,7 @@ class PackModulesTest {
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
modules.addAll(packModules.modules());
var order = LoadedModules.load(modules, Map.of(), Set.of()).placement().stages().stream()
var order = LoadedModules.load(modules, Map.of(), Set.of(), Set.of()).placement().stages().stream()
.map(stage -> stage.stage().id().toString()).toList();
assertEquals(List.of("iris:carve/clear", "user:core/carved"), order);
}
@@ -572,7 +572,7 @@ class PackModulesTest {
.writeInto(pack, "once.jar");
try (var packModules = PackModules.load(pack)) {
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
assertEquals("once:core", packModules.modules().getFirst().info().id().toString());
assertEquals(1, loaded.placement().stages().size());
}
@@ -591,7 +591,7 @@ class PackModulesTest {
.writeInto(pack, "beta.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
"Module beta:core from " + origin("beta.jar") + " failed to link or initialize a class while installing",
"java.lang.NoSuchMethodError", "pack.lib.Lib.added()");
}
@@ -603,7 +603,7 @@ class PackModulesTest {
module("pack.two.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "two.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
"Duplicate module dup:core from " + origin("two.jar") + "; already loaded from " + origin("one.jar"));
}
}
@@ -625,7 +625,7 @@ class PackModulesTest {
.writeInto(pack, "foreign.jar");
try (var packModules = PackModules.load(pack)) {
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of())),
assertMentions(failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of())),
"Stage mine:other/work must start with mine:core/, the id of its defining module mine:core from " + origin("foreign.jar"));
}
}
@@ -640,8 +640,8 @@ class PackModulesTest {
assertEquals("Module iris:stilt from " + origin("stilt.jar") + " uses reserved namespace 'iris', which only "
+ "modules that ship with the host may use; give the module a namespace of its own, which its stages, "
+ "settings ids and mantle keys then use too",
failure(() -> LoadedModules.load(packModules.modules(), Map.of(), RESERVED_NAMESPACES)));
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of()));
failure(() -> LoadedModules.load(packModules.modules(), Map.of(), RESERVED_NAMESPACES, Set.of())));
assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()));
}
}
@@ -654,13 +654,13 @@ class PackModulesTest {
try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
modules.addAll(packModules.modules());
assertDoesNotThrow(() -> LoadedModules.load(modules, Map.of(), RESERVED_NAMESPACES));
assertDoesNotThrow(() -> LoadedModules.load(modules, Map.of(), RESERVED_NAMESPACES, Set.of()));
}
}
@Test
void invalidReservedNamespaceFails() {
assertEquals("Reserved namespace 'iris:core' is not a namespace; name one such as iris, without ':' or a path",
failure(() -> LoadedModules.load(List.of(), Map.of(), Set.of("iris:core"))));
failure(() -> LoadedModules.load(List.of(), Map.of(), Set.of("iris:core"), Set.of())));
}
}
@@ -4,7 +4,9 @@ import com.volmit.FeatureAttachment.Target;
import com.volmit.config.Reference;
import com.volmit.example.chunk.GrassModule;
import com.volmit.example.chunk.TerrainModule;
import com.volmit.example.hosttypes.Palette;
import com.volmit.example.placement.PlacementModules;
import com.volmit.example.terrain.LayersModule;
import com.volmit.host.schema.FeatureRole;
import com.volmit.host.schema.FeatureSchema;
import com.volmit.host.schema.ObjectType;
@@ -15,6 +17,7 @@ import com.volmit.host.schema.ValueKind.BooleanKind;
import com.volmit.host.schema.ValueKind.DecimalKind;
import com.volmit.host.schema.ValueKind.EnumConstant;
import com.volmit.host.schema.ValueKind.EnumKind;
import com.volmit.host.schema.ValueKind.HostKind;
import com.volmit.host.schema.ValueKind.IntegerKind;
import com.volmit.host.schema.ValueKind.ListKind;
import com.volmit.host.schema.ValueKind.MapKind;
@@ -40,6 +43,7 @@ import static org.junit.jupiter.api.Assertions.*;
class PackSchemaTest {
private static final ModuleHost NO_BUILT_INS = ModuleHost.of(List.of());
private static final String FLORA = "example:flora";
private static final String HOST_TYPES = "com.volmit.example.hosttypes";
private static final String FLORA_SOURCE = "settings example:flora (module example:flora from modules/flora.jar)";
private static final String NO_STAGES = "";
@@ -338,10 +342,49 @@ class PackSchemaTest {
false, OptionalDouble.empty(), OptionalDouble.empty(), Optional.of("minecraft:grass_block")), grass.settings().options().getFirst());
}
@Test
void builtInSettingsMayHoldHostTypesWhichTheHostDescribesItself() {
var builtIns = List.<com.volmit.IrisModule>of(new LayersModule());
var schema = schemaOf(pack("layers"), ModuleHost.of(builtIns).describingHostTypesIn(HOST_TYPES));
var layers = schema.fields(Target.BIOME).getFirst();
var option = layers.settings().options().getFirst();
assertEquals(new ListKind(new HostKind(Palette.class.getName()), false), option.kind());
assertEquals(Optional.of(List.of()), option.defaultValue());
assertTrue(schema.types().stream().noneMatch(type -> type.name().equals(Palette.class.getName())),
"the host describes its own types, so their fields are never read");
assertEquals(Optional.of(option), layers.shorthand(), "one option: packs may write the bare list");
var undeclared = assertThrows(IllegalArgumentException.class, () -> schemaOf(pack("layers"), ModuleHost.of(builtIns)));
assertTrue(undeclared.getMessage().startsWith("Field " + Palette.class.getName() + ".anything of settings iris:layers"),
undeclared.getMessage());
assertThrows(IllegalArgumentException.class, () -> NO_BUILT_INS.describingHostTypesIn("not a package"));
}
@Test
void packModulesCannotUseHostTypes() {
var host = NO_BUILT_INS.exposing(HOST_TYPES).describingHostTypesIn(HOST_TYPES);
assertEquals("Field pack.flora.FloraSettings.palette of " + FLORA_SOURCE + " uses " + Palette.class.getName()
+ ", a class the host describes itself, which only settings of modules that ship with the host may use; "
+ ValueKinds.SUPPORTED_TYPES,
loadFailure(host, "private " + Palette.class.getName() + " palette;").getMessage());
}
@Test
void onlySettingsWithOneOptionHaveAShorthand() {
var flora = floraSchema().fields(Target.BIOME).getFirst();
assertEquals(Optional.empty(), flora.shorthand());
}
/** A pack whose module example:flora registers pack.flora.FloraSettings with {@code body} as its class body. */
private IllegalArgumentException loadFailure(String body, String... otherSources) {
return loadFailure(NO_BUILT_INS, body, otherSources);
}
private IllegalArgumentException loadFailure(ModuleHost host, String body, String... otherSources) {
var jar = module("pack.flora.FloraModule", FLORA,
"public void register(com.volmit.FeatureRegistry registry) { registry.registerSettings(FloraSettings.class); }", NO_STAGES)
.compileAgainstHostOf(Palette.class)
.source("pack.flora.FloraSettings", """
package pack.flora;
@@ -362,7 +405,7 @@ class PackSchemaTest {
}
var pack = pack("broken-" + ++brokenPacks);
jar.writeInto(pack, "flora.jar");
return assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, NO_BUILT_INS));
return assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, host));
}
private void assertLoadFails(String expected, String body, String... otherSources) {
@@ -374,7 +374,7 @@ class StageGraphTest {
place(graph, flatEdit(), ConfiguredSettings.none());
assertEquals(List.of("unrelated"), events);
events.clear();
place(graph, flatEdit(), ConfiguredSettings.of(new Toggle()));
place(graph, flatEdit(), ConfiguredSettings.placement(ResourceId.parse("test:placement"), new Toggle()));
assertEquals(List.of("toggled", "dependent:1", "unrelated"), events);
}
@@ -452,6 +452,8 @@ class StageGraphTest {
var empty = resolve(Pipeline.CHUNK);
assertThrows(IllegalArgumentException.class, () -> new PlacementRunner(empty));
assertThrows(IllegalArgumentException.class, () -> new ChunkRunner(resolve(Pipeline.PLACEMENT)));
assertThrows(IllegalArgumentException.class, () -> new MantleRunner(empty));
assertThrows(IllegalArgumentException.class, () -> new ChunkRunner(resolve(Pipeline.MANTLE)));
assertTrue(empty.describe().contains("CHUNK pipeline: 0 stages, 0 block passes"));
}
@@ -463,9 +465,9 @@ class StageGraphTest {
void reachIsDeclaredPerStageAndCollectedPerPipelineAndLayer() {
var depth = new MantleKey<>("test:reach/depth", Integer.class);
var marks = new MantleKey<>("test:reach/marks", String.class);
var carve = Stage.chunk("test:reach/carve");
var mark = Stage.chunk("test:reach/mark");
var plain = Stage.chunk("test:reach/plain");
var carve = Stage.mantle("test:reach/carve");
var mark = Stage.mantle("test:reach/mark");
var plain = Stage.mantle("test:reach/plain");
var paint = Stage.placementBlock("test:reach/paint");
var reach = module("test:reach", stages -> {
stages.define(carve).reach(24).writesMantle(depth).run(context -> {});
@@ -473,42 +475,60 @@ class StageGraphTest {
stages.define(plain).run(context -> {});
stages.define(paint).reach(2).run(context -> {});
});
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(reach, depth, marks))), Map.of(), Set.of());
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(reach, depth, marks))), Map.of(), Set.of(), Set.of());
assertEquals(new StageReach(24, List.of(depth)), loaded.chunk().stages().getFirst().reach());
assertEquals(StageReach.NONE, loaded.chunk().stages().get(2).reach(), "an undeclared reach is 0 with no layers");
assertEquals(new PipelineReach(24, Map.of("test:reach/depth", 24, "test:reach/marks", 8)), loaded.chunk().reach());
assertEquals(new StageReach(24, List.of(depth)), loaded.mantle().stages().getFirst().reach());
assertEquals(StageReach.NONE, loaded.mantle().stages().get(2).reach(), "an undeclared reach is 0 with no layers");
assertEquals(new PipelineReach(24, Map.of("test:reach/depth", 24, "test:reach/marks", 8)), loaded.mantle().reach());
assertEquals(new PipelineReach(2, Map.of()), loaded.placement().reach());
assertTrue(loaded.chunk().describe().contains(" reach 24 blocks, writes mantle test:reach/depth\n"), loaded.chunk().describe());
assertTrue(loaded.chunk().describe().contains(" reach 8 blocks, writes mantle test:reach/depth, test:reach/marks\n"));
assertEquals(new PipelineReach(0, Map.of()), loaded.chunk().reach());
assertTrue(loaded.mantle().describe().contains(" reach 24 blocks, writes mantle test:reach/depth\n"), loaded.mantle().describe());
assertTrue(loaded.mantle().describe().contains(" reach 8 blocks, writes mantle test:reach/depth, test:reach/marks\n"));
}
@Test
void reachMustBeValidAndNameRegisteredLayers() {
var depth = new MantleKey<>("test:reach/depth", Integer.class);
var carve = Stage.chunk("test:reach/carve");
var carve = Stage.mantle("test:reach/carve");
var negative = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.CHUNK,
var negative = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.MANTLE,
module("test:reach", stages -> stages.define(carve).reach(-1).run(context -> {}))));
assertEquals("Stage test:reach/carve (module test:reach) declares a negative reach of -1 blocks", negative.getMessage());
var twice = assertThrows(IllegalStateException.class, () -> resolve(Pipeline.CHUNK,
var twice = assertThrows(IllegalStateException.class, () -> resolve(Pipeline.MANTLE,
module("test:reach", stages -> stages.define(carve).reach(1).reach(2).run(context -> {}))));
assertEquals("Stage test:reach/carve (module test:reach) already declares a reach of 1 blocks", twice.getMessage());
assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.CHUNK,
assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.MANTLE,
module("test:reach", stages -> stages.define(carve).writesMantle(depth, depth).run(context -> {}))));
var unregistered = assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(
module("test:reach", stages -> stages.define(carve).writesMantle(depth).run(context -> {})))), Map.of(), Set.of()));
module("test:reach", stages -> stages.define(carve).writesMantle(depth).run(context -> {})))), Map.of(), Set.of(), Set.of()));
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth, which no module registers;"
+ " its owner registers it with FeatureRegistry.registerMantle", unregistered.getMessage());
var asText = new MantleKey<>("test:reach/depth", String.class);
var mismatch = assertThrows(IllegalArgumentException.class, () -> LoadedModules.load(List.of(DiscoveredModule.builtIn(withMantle(
module("test:reach", stages -> stages.define(carve).writesMantle(asText).run(context -> {})), depth))), Map.of(), Set.of()));
module("test:reach", stages -> stages.define(carve).writesMantle(asText).run(context -> {})), depth))), Map.of(), Set.of(), Set.of()));
assertEquals("Stage test:reach/carve of module test:reach writes mantle layer test:reach/depth as java.lang.String,"
+ " but it is registered as java.lang.Integer", mismatch.getMessage());
}
@Test
void chunkStagesNeitherReachBeyondTheirChunkNorWriteTheMantle() {
var depth = new MantleKey<>("test:reach/depth", Integer.class);
var carve = Stage.chunk("test:reach/carve");
var reach = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.CHUNK,
module("test:reach", stages -> stages.define(carve).reach(8).run(context -> {}))));
assertEquals("Stage test:reach/carve (module test:reach) is a chunk stage and declares a reach of 8 blocks, but chunk"
+ " stages write only their own chunk and read the mantle; define work that writes the mantle or reaches beyond"
+ " its chunk as a mantle stage (Stage.mantle), which runs before the chunks it reaches", reach.getMessage());
var writes = assertThrows(IllegalArgumentException.class, () -> resolve(Pipeline.CHUNK,
module("test:reach", stages -> stages.define(carve).writesMantle(depth).run(context -> {}))));
assertTrue(writes.getMessage().startsWith("Stage test:reach/carve (module test:reach) is a chunk stage and declares mantle writes"),
writes.getMessage());
assertDoesNotThrow(() -> resolve(Pipeline.CHUNK, module("test:reach", stages -> stages.define(carve).reach(0).run(context -> {}))));
}
/** {@code module}, also registering {@code layers}. */
private static IrisModule withMantle(IrisModule module, MantleKey<?>... layers) {
return new IrisModule() {
@@ -131,7 +131,7 @@ class StageStreamsTest {
var graph = resolve(Pipeline.CHUNK, module("a:mod", stages -> stages.define(Stage.chunk("a:mod/ores"))
.run(context -> drawn.add((long) context.random().nextInt(BOUND)))));
new ChunkRunner(graph).generate(TestContexts.generation(Map.of()), TestContexts.SEED, ConfiguredSettings.none());
new ChunkRunner(graph).generate(TestContexts.generation(Map.of()), TestContexts.SEED);
assertEquals(firstDraws(TestContexts.SEED, "a:mod/ores", 1), drawn);
}
@@ -0,0 +1,32 @@
package com.volmit.support;
import com.volmit.IrisFeature;
import com.volmit.config.FeatureSettings;
import java.util.List;
import java.util.function.BiFunction;
/** Settings declared per world column by a function, for chunk and mantle tests. */
public final class ColumnSettings implements FeatureSettings {
public static final ColumnSettings NONE = new ColumnSettings((x, z) -> List.of());
private final BiFunction<Integer, Integer, List<Entry<?>>> columns;
public ColumnSettings(BiFunction<Integer, Integer, List<Entry<?>>> columns) {
this.columns = columns;
}
/** The same entries at every column. */
public static ColumnSettings everywhere(Entry<?>... entries) {
var all = List.of(entries);
return new ColumnSettings((x, z) -> all);
}
@Override
@SuppressWarnings("unchecked") // Filtered by the requested type.
public <F extends IrisFeature> List<Entry<F>> at(Class<F> featureType, int worldX, int worldZ) {
return columns.apply(worldX, worldZ).stream()
.filter(entry -> featureType.isInstance(entry.value()))
.map(entry -> (Entry<F>) entry)
.toList();
}
}
@@ -0,0 +1,81 @@
package com.volmit.support;
import com.volmit.mantle.BlockBox;
import com.volmit.mantle.Mantle;
import com.volmit.mantle.MantleEdit;
import com.volmit.mantle.MantleKey;
import com.volmit.mantle.MantleLayer;
import com.volmit.mantle.MantleVisitor;
import com.volmit.world.BlockPosition;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/** An in-memory mantle over fixed bounds; an invocation sees it through {@link #within(BlockBox)}. */
public final class MemoryMantle {
private static final Comparator<BlockPosition> VISIT_ORDER = Comparator.comparingInt(BlockPosition::x)
.thenComparingInt(BlockPosition::y).thenComparingInt(BlockPosition::z);
private final Map<String, TreeMap<BlockPosition, Object>> layers = new HashMap<>();
/** A view that accepts reads and writes inside {@code bounds} only. */
public Mantle within(BlockBox bounds) {
return new Mantle() {
public BlockBox bounds() { return bounds; }
public <T> MantleLayer<T> layer(MantleKey<T> key) { return new Layer<>(key, bounds); }
public MantleEdit edit() { throw new UnsupportedOperationException("Edits are not used in these tests"); }
};
}
public <T> T get(MantleKey<T> key, int x, int y, int z) {
return key.type().cast(values(key).get(new BlockPosition(x, y, z)));
}
private TreeMap<BlockPosition, Object> values(MantleKey<?> key) {
return layers.computeIfAbsent(key.id(), ignored -> new TreeMap<>(VISIT_ORDER));
}
private final class Layer<T> implements MantleLayer<T> {
private final MantleKey<T> key;
private final BlockBox bounds;
Layer(MantleKey<T> key, BlockBox bounds) {
this.key = key;
this.bounds = bounds;
}
public T get(int x, int y, int z) {
requireInside(x, y, z);
return MemoryMantle.this.get(key, x, y, z);
}
public void set(int x, int y, int z, T value) {
requireInside(x, y, z);
values(key).put(new BlockPosition(x, y, z), key.type().cast(value));
}
public void remove(int x, int y, int z) {
requireInside(x, y, z);
values(key).remove(new BlockPosition(x, y, z));
}
public void visit(BlockBox area, MantleVisitor<T> visitor) {
if (!bounds.contains(area)) throw new IndexOutOfBoundsException("Outside the mantle view: " + area);
values(key).forEach((position, value) -> {
if (area.contains(position.x(), position.y(), position.z())) {
visitor.accept(position.x(), position.y(), position.z(), key.type().cast(value));
}
});
}
public void clear(BlockBox area) {
if (!bounds.contains(area)) throw new IndexOutOfBoundsException("Outside the mantle view: " + area);
values(key).keySet().removeIf(position -> area.contains(position.x(), position.y(), position.z()));
}
private void requireInside(int x, int y, int z) {
if (!bounds.contains(x, y, z)) throw new IndexOutOfBoundsException("Outside the mantle view: " + x + "," + y + "," + z);
}
}
}
@@ -2,6 +2,7 @@ package com.volmit.support;
import com.volmit.config.FeatureSettings;
import com.volmit.generation.GenerationContext;
import com.volmit.generation.MantleGenerationContext;
import com.volmit.hunk.Hunk;
import com.volmit.mantle.Mantle;
import com.volmit.object.ObjectTransformer;
@@ -18,6 +19,11 @@ public final class TestContexts {
private TestContexts() {}
public static GenerationContext generation(Map<Class<?>, Hunk<?>> hunks) {
return generation(hunks, null, null);
}
/** The chunk at the origin; a null {@code settings} or {@code mantle} fails when used. */
public static GenerationContext generation(Map<Class<?>, Hunk<?>> hunks, FeatureSettings settings, Mantle mantle) {
return new GenerationContext() {
public int originX() { return 0; }
public int originY() { return -64; }
@@ -25,8 +31,8 @@ public final class TestContexts {
public long worldSeed() { return SEED; }
public PackResources resources() { throw new AssertionError("Unused resources"); }
public TerrainSampler terrain() { throw new AssertionError("Unused terrain"); }
public FeatureSettings settings() { throw new AssertionError("Unused settings"); }
public Mantle mantle() { throw new AssertionError("Unused mantle"); }
public FeatureSettings settings() { return used(settings, "settings"); }
public Mantle mantle() { return used(mantle, "mantle"); }
@SuppressWarnings("unchecked")
public <T> Hunk<T> hunk(Class<T> type) {
var hunk = hunks.get(type);
@@ -36,6 +42,19 @@ public final class TestContexts {
};
}
/** The mantle invocation of the chunk whose first block is ({@code originX}, {@code originZ}). */
public static MantleGenerationContext mantle(int originX, int originZ, FeatureSettings settings, Mantle mantle) {
return new MantleGenerationContext() {
public int originX() { return originX; }
public int originZ() { return originZ; }
public long worldSeed() { return SEED; }
public PackResources resources() { throw new AssertionError("Unused resources"); }
public TerrainSampler terrain() { throw new AssertionError("Unused terrain"); }
public FeatureSettings settings() { return settings; }
public Mantle mantle() { return mantle; }
};
}
public static PlacementContext placement() {
return placement(null);
}
@@ -53,4 +72,9 @@ public final class TestContexts {
}
};
}
private static <T> T used(T value, String name) {
if (value == null) throw new AssertionError("Unused " + name);
return value;
}
}