diff --git a/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java b/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java new file mode 100644 index 000000000..729c80f8c --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java @@ -0,0 +1,88 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.spi.PlatformBlockState; +import com.volmit.block.BlockState; +import com.volmit.hunk.Hunk; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +/** + * Iris's chunk block hunk seen as IrisApi block states. Iris leaves unset blocks null, which IrisApi + * forbids, so they read as air. States are converted through their keys and remembered for the + * invocation, since a chunk repeats few states many times. + */ +final class BlockStateHunk implements Hunk { + private static final BlockState AIR = BlockState.parse("minecraft:air"); + + private final art.arcane.iris.util.project.hunk.Hunk blocks; + private final Function platformStates; + private final Map read = new HashMap<>(); + private final Map written = new HashMap<>(); + + /** {@code platformStates} turns a block state key into the platform's state, such as {@code B::getState}. */ + BlockStateHunk(art.arcane.iris.util.project.hunk.Hunk blocks, Function platformStates) { + this.blocks = blocks; + this.platformStates = platformStates; + } + + @Override + public int getWidth() { + return blocks.getWidth(); + } + + @Override + public int getHeight() { + return blocks.getHeight(); + } + + @Override + public int getDepth() { + return blocks.getDepth(); + } + + @Override + public BlockState get(int x, int y, int z) { + requireInside(x, y, z); + PlatformBlockState state = blocks.get(x, y, z); + if (state == null) { + return AIR; + } + return read.computeIfAbsent(state, platform -> BlockState.parse(platform.key())); + } + + @Override + public void set(int x, int y, int z, BlockState value) { + requireInside(x, y, z); + if (value == null) { + throw new NullPointerException("Block hunks hold no null states; write minecraft:air for empty space"); + } + blocks.set(x, y, z, written.computeIfAbsent(value, state -> platformStates.apply(state.key()))); + } + + private void requireInside(int x, int y, int z) { + if (x < 0 || y < 0 || z < 0 || x >= getWidth() || y >= getHeight() || z >= getDepth()) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the " + + getWidth() + "x" + getHeight() + "x" + getDepth() + " chunk hunk"); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ChunkModuleSettings.java b/core/src/main/java/art/arcane/iris/core/modules/ChunkModuleSettings.java new file mode 100644 index 000000000..893af89ca --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ChunkModuleSettings.java @@ -0,0 +1,81 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.core.loader.IrisRegistrant; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import com.volmit.FeatureAttachment.Target; +import com.volmit.IrisFeature; +import com.volmit.config.FeatureSettings; +import com.volmit.host.ConfiguredSettings; +import com.volmit.resource.ResourceId; + +import java.util.List; + +/** + * Module settings at world positions, for one engine. + *

+ * The settings that activate a chunk's stages come from its dimension, then the region and then the + * biome at the chunk's center column, each type taking the most specific declaration: a biome's value + * replaces its region's, a region's the dimension's. Iris resolves single-valued settings, such as a + * cave profile, the same way. {@link #at} keeps the targets apart, as {@link FeatureSettings} promises. + */ +final class ChunkModuleSettings implements FeatureSettings { + private final Engine engine; + private final PackResourceIds ids; + + ChunkModuleSettings(Engine engine, PackResourceIds ids) { + this.engine = engine; + this.ids = ids; + } + + static ConfiguredSettings configured(IrisDimension dimension, IrisRegion region, IrisBiome biome) { + return settingsOf(dimension) + .overriddenBy(settingsOf(region)) + .overriddenBy(settingsOf(biome)) + .configured(); + } + + @Override + public List> at(Class featureType, Target target, int worldX, int worldY, int worldZ) { + return switch (target) { + case DIMENSION -> entries(featureType, "dimension", engine.getDimension()); + case REGION -> entries(featureType, "region", engine.getRegion(worldX, worldZ)); + case BIOME -> entries(featureType, "biome", engine.getSurfaceBiome(worldX, worldZ)); + case OBJECT_PLACEMENT -> List.of(); + }; + } + + private List> entries(Class featureType, String kind, IrisRegistrant owner) { + if (owner == null) { + return List.of(); + } + ResourceId source = ids.of(kind, owner.getLoadKey()); + return owner.getModuleSettings().find(featureType) + .map(value -> List.of(new Entry<>(source, source, value))) + .orElse(List.of()); + } + + private static ModuleSettings settingsOf(IrisRegistrant owner) { + return owner == null ? ModuleSettings.NONE : owner.getModuleSettings(); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java b/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java new file mode 100644 index 000000000..b75987152 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java @@ -0,0 +1,103 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import com.volmit.block.BlockState; +import com.volmit.config.FeatureSettings; +import com.volmit.generation.GenerationContext; +import com.volmit.mantle.Mantle; +import com.volmit.resource.PackResources; +import com.volmit.world.TerrainSampler; + +/** + * One chunk of an Iris engine as an IrisApi chunk invocation: the chunk's block hunk, Iris's terrain + * streams and the pack's module settings. Mantle access and pack resources are not bridged yet; a stage + * that asks for them fails the chunk with an error saying so, rather than seeing an empty mantle or + * pack. + */ +final class IrisGenerationContext implements GenerationContext { + private static final String NOT_BRIDGED = " is not bridged to Iris yet; modules cannot use it until a built-in port needs it"; + + private final Engine engine; + private final int originX; + private final int originZ; + private final BlockStateHunk blocks; + private final IrisTerrainSampler terrain; + private final ChunkModuleSettings settings; + + IrisGenerationContext(Engine engine, int originX, int originZ, BlockStateHunk blocks, int reachBlocks) { + PackResourceIds ids = new PackResourceIds(engine.getData().getDataFolder()); + this.engine = engine; + this.originX = originX; + this.originZ = originZ; + this.blocks = blocks; + this.terrain = new IrisTerrainSampler(engine, ids, originX, originZ, reachBlocks); + this.settings = new ChunkModuleSettings(engine, ids); + } + + @Override + public int originX() { + return originX; + } + + @Override + public int originY() { + return engine.getMinHeight(); + } + + @Override + public int originZ() { + return originZ; + } + + @Override + public TerrainSampler terrain() { + return terrain; + } + + @Override + public FeatureSettings settings() { + return settings; + } + + @Override + public Mantle mantle() { + throw new UnsupportedOperationException("Mantle access from module stages" + NOT_BRIDGED); + } + + @Override + @SuppressWarnings("unchecked") // The only hunk type is checked against the requested class. + public com.volmit.hunk.Hunk hunk(Class type) { + if (type != BlockState.class) { + throw new IllegalArgumentException("Iris provides chunk hunks of " + BlockState.class.getName() + " only, not " + type.getName()); + } + return (com.volmit.hunk.Hunk) blocks; + } + + @Override + public long worldSeed() { + return engine.getSeedManager().getSeed(); + } + + @Override + public PackResources resources() { + throw new UnsupportedOperationException("Pack resources for module stages" + NOT_BRIDGED); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java new file mode 100644 index 000000000..0a649bf2a --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java @@ -0,0 +1,81 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import com.volmit.mantle.BlockBox; +import com.volmit.world.BiomeSample; +import com.volmit.world.TerrainSample; +import com.volmit.world.TerrainSampler; + +import java.util.Optional; +import java.util.OptionalInt; + +/** + * Iris's terrain streams for one chunk invocation. The streams are computed, not prepared, so the + * bounds are only the area the pipeline's reach may touch: the chunk widened by it, at full height. + */ +final class IrisTerrainSampler implements TerrainSampler { + private static final int CHUNK_WIDTH_BLOCKS = 16; + + private final Engine engine; + private final PackResourceIds ids; + private final BlockBox bounds; + + IrisTerrainSampler(Engine engine, PackResourceIds ids, int originX, int originZ, int reachBlocks) { + this.engine = engine; + this.ids = ids; + this.bounds = new BlockBox( + originX - reachBlocks, engine.getMinHeight(), originZ - reachBlocks, + originX + CHUNK_WIDTH_BLOCKS + reachBlocks, engine.getMinHeight() + engine.getHeight(), originZ + CHUNK_WIDTH_BLOCKS + reachBlocks); + } + + @Override + public BlockBox bounds() { + return bounds; + } + + @Override + public TerrainSample column(int worldX, int worldZ) { + requireInside(worldX, bounds.minY(), worldZ); + double surfaceY = engine.getComplex().getHeightStream().get(worldX, worldZ); + double slope = Math.abs(engine.getComplex().getSlopeStream().get(worldX, worldZ)); + int fluidHeight = engine.getDimension().getFluidHeight(); + OptionalInt fluidY = surfaceY < fluidHeight ? OptionalInt.of(fluidHeight) : OptionalInt.empty(); + return new TerrainSample(surfaceY, fluidY, slope); + } + + @Override + public BiomeSample biome(int worldX, int worldY, int worldZ) { + requireInside(worldX, worldY, worldZ); + IrisBiome surface = engine.getSurfaceBiome(worldX, worldZ); + Optional cave = Optional.ofNullable(engine.getCaveBiome(worldX, worldY, worldZ)); + return new BiomeSample( + ids.of(engine.getRegion(worldX, worldZ).getLoadKey()), + ids.of(surface.getLoadKey()), + cave.map(biome -> ids.of(biome.getLoadKey()))); + } + + private void requireInside(int worldX, int worldY, int worldZ) { + if (!bounds.contains(worldX, worldY, worldZ)) { + throw new IndexOutOfBoundsException("Block " + worldX + ", " + worldY + ", " + worldZ + " is outside the sampled area " + bounds); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java new file mode 100644 index 000000000..00b367c50 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java @@ -0,0 +1,68 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.util.common.data.B; +import art.arcane.iris.util.project.context.ChunkContext; +import art.arcane.iris.util.project.hunk.Hunk; +import com.volmit.host.ChunkRunner; +import com.volmit.host.ConfiguredSettings; +import com.volmit.host.LoadedPack; +import com.volmit.host.ResolvedGraph; + +import java.util.function.Function; + +/** + * Runs the pack's resolved chunk graph on a chunk Iris has generated. Its stages are active where the + * chunk's dimension, region or biome configures their settings (see {@link ChunkModuleSettings}). A + * failing stage fails the chunk. + *

+ * Stages write the chunk's own block hunk, so a block written beyond the chunk fails instead of being + * clipped. The pipeline's reach widens the terrain they may sample; mantle access is not bridged yet. + */ +public final class ModuleChunkGeneration { + private static final int CHUNK_CENTER = 7; + private static final int CHUNK_SHIFT = 4; + + private ModuleChunkGeneration() { + } + + /** {@code x} and {@code z} are the chunk's first block, as engine stages receive them. */ + public static void generate(Engine engine, int x, int z, Hunk blocks, ChunkContext context) { + generate(engine.getData().getModules().pack(), engine, x, z, blocks, + context.getRegion().get(CHUNK_CENTER, CHUNK_CENTER), context.getBiome().get(CHUNK_CENTER, CHUNK_CENTER), B::getState); + } + + static void generate(LoadedPack pack, Engine engine, int x, int z, Hunk blocks, + IrisRegion centerRegion, IrisBiome centerBiome, Function platformStates) { + ResolvedGraph chunk = pack.chunk(); + if (chunk.stages().isEmpty()) { + return; + } + ConfiguredSettings settings = ChunkModuleSettings.configured(engine.getDimension(), centerRegion, centerBiome); + IrisGenerationContext inputs = new IrisGenerationContext(engine, x, z, + new BlockStateHunk(blocks, platformStates), chunk.reach().blocks()); + long seed = ModuleSeeds.chunk(engine.getSeedManager().getSeed(), x >> CHUNK_SHIFT, z >> CHUNK_SHIFT); + new ChunkRunner(chunk).generate(inputs, seed, settings); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleSeeds.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleSeeds.java new file mode 100644 index 000000000..5393e5ef6 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleSeeds.java @@ -0,0 +1,35 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +/** + * The seed of one module chunk invocation, from the world seed and the chunk position. IrisApi derives + * each stage's stream from it and the stage id. + */ +final class ModuleSeeds { + private static final long CHUNK_X_MULTIPLIER = 0x9E3779B97F4A7C15L; + private static final long CHUNK_Z_MULTIPLIER = 0xC2B2AE3D27D4EB4FL; + + private ModuleSeeds() { + } + + static long chunk(long worldSeed, int chunkX, int chunkZ) { + return worldSeed ^ (chunkX * CHUNK_X_MULTIPLIER) ^ (chunkZ * CHUNK_Z_MULTIPLIER); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/PackResourceIds.java b/core/src/main/java/art/arcane/iris/core/modules/PackResourceIds.java new file mode 100644 index 000000000..b8bcd686e --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/PackResourceIds.java @@ -0,0 +1,55 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import com.volmit.resource.ResourceId; + +import java.io.File; +import java.util.Locale; +import java.util.regex.Pattern; + +/** + * Names a pack's dimensions, regions and biomes as {@link ResourceId}s where IrisApi needs one. Pack keys + * such as {@code desert/dunes} have no namespace, so the namespace is the pack folder's name, made a + * valid namespace: lower case, and every other character outside {@code [a-z0-9_.-]} replaced by + * {@code _}. A biome of pack {@code overworld} is {@code overworld:desert/dunes}. + */ +final class PackResourceIds { + private static final Pattern NOT_NAMESPACE = Pattern.compile("[^a-z0-9_.-]"); + private static final Pattern NOT_PATH = Pattern.compile("[^a-z0-9_./-]"); + private static final String REPLACEMENT = "_"; + private static final String UNNAMED_PACK = "pack"; + + private final String namespace; + + PackResourceIds(File packFolder) { + String name = NOT_NAMESPACE.matcher(packFolder.getName().toLowerCase(Locale.ROOT)).replaceAll(REPLACEMENT); + this.namespace = name.isEmpty() ? UNNAMED_PACK : name; + } + + /** {@code key} of a resource whose type its use makes clear, such as a biome in a biome sample. */ + ResourceId of(String key) { + return new ResourceId(namespace, NOT_PATH.matcher(key.toLowerCase(Locale.ROOT)).replaceAll(REPLACEMENT)); + } + + /** {@code key} of a resource of {@code type}, such as {@code overworld:biome/desert/dunes}. */ + ResourceId of(String type, String key) { + return of(type + "/" + key); + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/framework/EngineMode.java b/core/src/main/java/art/arcane/iris/engine/framework/EngineMode.java index d152e8ed2..e1d49a817 100644 --- a/core/src/main/java/art/arcane/iris/engine/framework/EngineMode.java +++ b/core/src/main/java/art/arcane/iris/engine/framework/EngineMode.java @@ -20,6 +20,7 @@ package art.arcane.iris.engine.framework; import art.arcane.iris.engine.IrisComplex; import art.arcane.iris.engine.mantle.EngineMantle; +import art.arcane.iris.core.modules.ModuleChunkGeneration; import art.arcane.iris.util.project.context.ChunkContext; import art.arcane.iris.util.project.context.IrisContext; import art.arcane.volmlib.util.documentation.BlockCoordinates; @@ -107,6 +108,7 @@ public interface EngineMode extends Staged { for (EngineStage i : stages) { i.generate(x, z, blocks, biomes, multicore, ctx); } + ModuleChunkGeneration.generate(getEngine(), x, z, blocks, ctx); } } diff --git a/core/src/test/java/art/arcane/iris/core/modules/ModuleChunkGenerationTest.java b/core/src/test/java/art/arcane/iris/core/modules/ModuleChunkGenerationTest.java new file mode 100644 index 000000000..65aaa1070 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/ModuleChunkGenerationTest.java @@ -0,0 +1,162 @@ +/* + * Iris is a World Generator for Minecraft 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.SeedManager; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.util.project.hunk.Hunk; +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.host.LoadedPack; +import com.volmit.host.ModuleHost; +import com.volmit.stage.Stage; +import com.volmit.stage.StageGraph; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ModuleChunkGenerationTest { + private static final int CHUNK_WIDTH = 16; + private static final int HEIGHT = 4; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private LoadedPack pack; + private Engine engine; + private Hunk blocks; + + @Feature(id = "iris:pillar", name = "Pillar", alias = "pillar", targets = {Target.BIOME, Target.REGION, Target.DIMENSION}) + public static final class PillarSettings implements IrisFeature { + private BlockState block = BlockState.parse("minecraft:stone"); + private int x; + } + + /** Copies the block below the chunk's corner up one, and writes the configured block at {@code x}. */ + public static final class PillarModule implements IrisModule { + @Override + public ModuleInfo info() { + return ModuleInfo.of("iris:pillar", "Pillar", "1"); + } + + @Override + public void install(StageGraph graph) { + graph.define(Stage.chunk("iris:pillar/raise")).when(PillarSettings.class).run((context, pillar) -> { + com.volmit.hunk.Hunk chunk = context.hunk(BlockState.class); + chunk.set(0, 1, 0, chunk.get(0, 0, 0)); + chunk.set(pillar.x, 2, 0, pillar.block); + }); + } + } + + @Before + public void loadPack() throws IOException { + pack = LoadedPack.load(folder.newFolder("pack").toPath(), + ModuleHost.of(List.of(new PillarModule())).copyingJarsTo(folder.newFolder("copies").toPath())); + engine = mock(Engine.class); + IrisData data = mock(IrisData.class); + SeedManager seeds = mock(SeedManager.class); + when(engine.getData()).thenReturn(data); + when(data.getDataFolder()).thenReturn(folder.getRoot()); + when(engine.getSeedManager()).thenReturn(seeds); + when(seeds.getSeed()).thenReturn(1L); + when(engine.getDimension()).thenReturn(new IrisDimension()); + blocks = Hunk.newArrayHunk(CHUNK_WIDTH, HEIGHT, CHUNK_WIDTH); + } + + @After + public void closePack() { + pack.close(); + } + + @Test + public void stagesRunWhereTheChunksBiomeConfiguresThemAndWriteIrisBlocks() { + blocks.set(0, 0, 0, platformState("minecraft:dirt")); + + generate(configured(new IrisBiome(), pillar(3))); + + assertEquals("minecraft:dirt", blocks.get(0, 1, 0).key()); + assertEquals("minecraft:oak_log[axis=y]", blocks.get(3, 2, 0).key()); + } + + @Test + public void unsetIrisBlocksReadAsAir() { + generate(configured(new IrisBiome(), pillar(3))); + + assertEquals("minecraft:air", blocks.get(0, 1, 0).key()); + } + + @Test + public void stagesStayInactiveWhereNothingConfiguresThem() { + generate(new IrisBiome()); + + assertNull(blocks.get(3, 2, 0)); + } + + @Test + public void aBlockOutsideTheChunkFailsInsteadOfBeingClipped() { + IndexOutOfBoundsException failure = assertThrows(IndexOutOfBoundsException.class, + () -> generate(configured(new IrisBiome(), pillar(CHUNK_WIDTH)))); + + assertEquals("Block 16, 2, 0 is outside the 16x4x16 chunk hunk", failure.getMessage()); + } + + private void generate(IrisBiome biome) { + ModuleChunkGeneration.generate(pack, engine, 0, 0, blocks, new IrisRegion(), biome, ModuleChunkGenerationTest::platformState); + } + + private static PillarSettings pillar(int x) { + PillarSettings pillar = new PillarSettings(); + pillar.block = BlockState.parse("minecraft:oak_log[axis=y]"); + pillar.x = x; + return pillar; + } + + private static T configured(T owner, IrisFeature settings) { + owner.setModuleSettings(ModuleSettings.of(Map.of(settings.getClass(), settings))); + return owner; + } + + private static PlatformBlockState platformState(String key) { + PlatformBlockState state = mock(PlatformBlockState.class); + when(state.key()).thenReturn(key); + return state; + } +}