mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Serve the pack snapshot's objects, biomes and regions to chunk, mantle and placement stages through PackResources
This commit is contained in:
1 parent
21efc385da
commit
d2b4904263
14 files changed
+665
-11
No files matched your search
@@ -31,6 +31,7 @@ import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.core.modules.IrisPackResources;
|
||||
import art.arcane.iris.core.modules.ObjectPlacementSettingsReader;
|
||||
import art.arcane.iris.core.modules.PackModuleSet;
|
||||
import art.arcane.iris.core.pack.PackDirectoryResolver;
|
||||
@@ -73,6 +74,8 @@ import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.iris.util.project.context.IrisContext;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -109,6 +112,9 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
private final boolean datapackCompiler;
|
||||
private volatile boolean closed = false;
|
||||
private final PackModuleSet modules;
|
||||
@EqualsAndHashCode.Exclude
|
||||
@ToString.Exclude
|
||||
private volatile IrisPackResources packResources;
|
||||
private ResourceLoader<IrisBiome> biomeLoader;
|
||||
private ResourceLoader<IrisLootTable> lootLoader;
|
||||
private ResourceLoader<IrisRegion> regionLoader;
|
||||
@@ -505,6 +511,7 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
|
||||
gson = builder.create();
|
||||
loaders = replacement;
|
||||
packResources = new IrisPackResources(this);
|
||||
|
||||
for (Engine engine : getEngines()) {
|
||||
engine.hotload();
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import com.volmit.block.BlockFace;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.block.BlockTrait;
|
||||
import com.volmit.resource.ResourceId;
|
||||
|
||||
/**
|
||||
* IrisApi's view of the bound platform's blocks. States convert through their keys, as {@link PlatformBlockStates}
|
||||
* does; the platform decides which blocks exist, so a key it does not know fails. Traits answer from what the
|
||||
* platform's block states can tell. Three traits ({@code GRAVITY}, {@code LOG}, {@code LEAVES}), tags and
|
||||
* attachment to a face are not offered by the platform layer yet, and asking for them throws
|
||||
* {@link UnsupportedOperationException} rather than guessing.
|
||||
*/
|
||||
final class IrisBlockRegistry implements BlockRegistry {
|
||||
@Override
|
||||
public BlockState resolve(String key) {
|
||||
return BlockState.parse(PlatformBlockStates.namespaced(platformState(key).key()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrait(BlockState state, BlockTrait trait) {
|
||||
PlatformBlockState platform = platformState(state.key());
|
||||
return switch (trait) {
|
||||
case AIR -> platform.isAir();
|
||||
case SOLID -> platform.isSolid();
|
||||
case OCCLUDING -> platform.isOccluding();
|
||||
case FLUID -> platform.isFluid();
|
||||
case WATER -> platform.isWater();
|
||||
case FOLIAGE -> platform.isFoliage();
|
||||
case DECORATION -> platform.isDecorant();
|
||||
case ORE -> platform.isOre();
|
||||
case INVENTORY -> platform.isStorage();
|
||||
case BLOCK_ENTITY -> platform.hasTileEntity();
|
||||
case CUSTOM -> platform.isCustom();
|
||||
case NEEDS_UPDATE -> platform.isUpdatable();
|
||||
case GRAVITY, LOG, LEAVES -> throw unsupported("The " + trait + " trait");
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inTag(BlockState state, ResourceId tag) {
|
||||
throw unsupported("Block tags");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(BlockState state, BlockState support, BlockFace face) {
|
||||
throw unsupported("Attachment to a face");
|
||||
}
|
||||
|
||||
private static PlatformBlockState platformState(String key) {
|
||||
PlatformBlockState state = IrisPlatforms.get().registries().blockOrNull(PlatformBlockStates.namespaced(key), false);
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("Unknown block: " + key);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
private static UnsupportedOperationException unsupported(String what) {
|
||||
return new UnsupportedOperationException(what + " cannot be answered yet: Iris's platform layer does not offer it");
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,9 @@ import java.util.Collection;
|
||||
/**
|
||||
* One chunk of an Iris engine as an IrisApi chunk invocation: the chunk's block hunk, Iris's terrain
|
||||
* streams, the pack's module settings and the chunk's mantle, which IrisApi gives chunk stages read-only.
|
||||
* 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 pack.
|
||||
* Pack resources are the pack snapshot's, shared by every invocation of it.
|
||||
*/
|
||||
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";
|
||||
/** Chunk stages write only their own chunk. */
|
||||
private static final int CHUNK_STAGE_REACH = 0;
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
@@ -110,6 +108,6 @@ final class IrisGenerationContext implements GenerationContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for module stages" + NOT_BRIDGED);
|
||||
return engine.getData().getPackResources();
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,8 @@ import java.util.Collection;
|
||||
|
||||
/**
|
||||
* One chunk of an Iris engine as an IrisApi mantle invocation: the mantle around the chunk as far as the
|
||||
* pipeline reaches, Iris's terrain streams over the same area and the pack's module settings. Pack resources
|
||||
* are not bridged yet; a stage that asks for them fails with an error saying so.
|
||||
* pipeline reaches, Iris's terrain streams over the same area, the pack's module settings and the pack
|
||||
* snapshot's resources.
|
||||
*/
|
||||
final class IrisMantleGenerationContext implements MantleGenerationContext {
|
||||
private static final int CHUNK_SHIFT = 4;
|
||||
@@ -90,7 +90,6 @@ final class IrisMantleGenerationContext implements MantleGenerationContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for module stages are not bridged to Iris yet;"
|
||||
+ " modules cannot use them until a built-in port needs them");
|
||||
return engine.getData().getPackResources();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.IrisRegistrant;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
import art.arcane.iris.engine.object.prefab.IrisObject;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.engine.object.world.region.IrisRegion;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKey;
|
||||
import com.volmit.world.PackBiome;
|
||||
import com.volmit.world.PackRegion;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The resources of one loaded snapshot of an Iris pack, which every chunk, mantle and placement stage of that
|
||||
* snapshot shares. Each load or hotload of the pack's data makes a new one, with a new {@link #snapshotId()}.
|
||||
* <p>
|
||||
* Three kinds of resource are served, by the type the key asks for: {@link GeneratedObject} (a pack object, as
|
||||
* its geometry), {@link PackBiome} and {@link PackRegion}. All use the id {@code <pack namespace>:<key>} that
|
||||
* {@link PackResourceIds} gives the pack's biomes in a biome sample, so one id can name a biome and a region.
|
||||
* An id the pack lacks, or of another namespace, is absent; any other type fails with
|
||||
* {@link IllegalArgumentException}. A resource that exists but cannot be loaded fails with
|
||||
* {@link IllegalStateException}. Each resource is built once and kept for the snapshot.
|
||||
*/
|
||||
public final class IrisPackResources implements PackResources {
|
||||
private static final String SERVED_TYPES = "GeneratedObject, PackBiome and PackRegion";
|
||||
|
||||
private final PackResourceIds ids;
|
||||
private final UUID snapshotId = UUID.randomUUID();
|
||||
private final BlockRegistry blocks = new IrisBlockRegistry();
|
||||
private final Map<Class<?>, Kind<?, ?>> kinds;
|
||||
|
||||
public IrisPackResources(IrisData data) {
|
||||
this.ids = new PackResourceIds(data.getDataFolder());
|
||||
this.kinds = Map.of(
|
||||
GeneratedObject.class, new Kind<IrisObject, GeneratedObject>(ids, data::getObjectLoader, (id, object) -> object.getGeometry()),
|
||||
PackBiome.class, new Kind<IrisBiome, PackBiome>(ids, data::getBiomeLoader, this::biomeOf),
|
||||
PackRegion.class, new Kind<IrisRegion, PackRegion>(ids, data::getRegionLoader, this::regionOf));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceId packId() {
|
||||
return ids.packId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID snapshotId() {
|
||||
return snapshotId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockRegistry blocks() {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked") // The kinds are keyed by the type they serve.
|
||||
public <T> Optional<T> find(ResourceKey<T> key) {
|
||||
Kind<?, T> kind = (Kind<?, T>) kinds.get(key.type());
|
||||
if (kind == null) {
|
||||
throw new IllegalArgumentException("Iris serves pack resources of " + SERVED_TYPES + ", not " + key.type().getName());
|
||||
}
|
||||
return kind.find(key.id());
|
||||
}
|
||||
|
||||
private PackBiome biomeOf(ResourceId id, IrisBiome biome) {
|
||||
return new PackBiome(id, biome.getName(), ResourceId.parse(biome.getDerivativeKey()), biome.getRarity(), idsOf(biome.getChildren()));
|
||||
}
|
||||
|
||||
private PackRegion regionOf(ResourceId id, IrisRegion region) {
|
||||
return new PackRegion(id, region.getName(), region.getRarity(), idsOf(region.getLandBiomes()), idsOf(region.getSeaBiomes()),
|
||||
idsOf(region.getShoreBiomes()), idsOf(region.getCaveBiomes()));
|
||||
}
|
||||
|
||||
private List<ResourceId> idsOf(Collection<String> keys) {
|
||||
return keys.stream().map(ids::of).toList();
|
||||
}
|
||||
|
||||
/** One kind of pack resource: the loader that serves its keys, an index from ids to keys and what was built so far. */
|
||||
private static final class Kind<R extends IrisRegistrant, T> {
|
||||
private final PackResourceIds ids;
|
||||
private final Supplier<ResourceLoader<R>> loader;
|
||||
private final BiFunction<ResourceId, R, T> conversion;
|
||||
private final Map<ResourceId, T> built = new ConcurrentHashMap<>();
|
||||
private volatile Map<ResourceId, String> keysById;
|
||||
|
||||
Kind(PackResourceIds ids, Supplier<ResourceLoader<R>> loader, BiFunction<ResourceId, R, T> conversion) {
|
||||
this.ids = ids;
|
||||
this.loader = loader;
|
||||
this.conversion = conversion;
|
||||
}
|
||||
|
||||
Optional<T> find(ResourceId id) {
|
||||
T cached = built.get(id);
|
||||
if (cached != null) {
|
||||
return Optional.of(cached);
|
||||
}
|
||||
String key = keysById().get(id);
|
||||
if (key == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
R resource = loader.get().load(key);
|
||||
if (resource == null) {
|
||||
throw new IllegalStateException("Pack resource " + id + " exists but failed to load");
|
||||
}
|
||||
T made = conversion.apply(id, resource);
|
||||
T raced = built.putIfAbsent(id, made);
|
||||
return Optional.of(raced != null ? raced : made);
|
||||
}
|
||||
|
||||
/** The pack's keys by the ids they get, built on first use; a key whose id another key already has is left out. */
|
||||
private Map<ResourceId, String> keysById() {
|
||||
Map<ResourceId, String> index = keysById;
|
||||
if (index != null) {
|
||||
return index;
|
||||
}
|
||||
Map<ResourceId, String> indexed = new HashMap<>();
|
||||
for (String key : loader.get().getPossibleKeys()) {
|
||||
indexed.putIfAbsent(ids.of(key), key);
|
||||
}
|
||||
keysById = Map.copyOf(indexed);
|
||||
return keysById;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,10 @@ final class IrisPlacementInputs implements PlacementContext {
|
||||
private final TerrainSampler terrain;
|
||||
private final FeatureSettings settings;
|
||||
private final BlockView blocks;
|
||||
private final PackResources resources;
|
||||
|
||||
IrisPlacementInputs(long worldSeed, TerrainSampler terrain, FeatureSettings settings, BlockView blocks) {
|
||||
IrisPlacementInputs(long worldSeed, TerrainSampler terrain, FeatureSettings settings, BlockView blocks, PackResources resources) {
|
||||
this.resources = resources;
|
||||
this.worldSeed = worldSeed;
|
||||
this.terrain = terrain;
|
||||
this.settings = settings;
|
||||
@@ -95,6 +97,6 @@ final class IrisPlacementInputs implements PlacementContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for placement stages" + NOT_BRIDGED);
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public final class ModuleObjectPlacement {
|
||||
geometry, new BlockPosition(x, requestedY, z));
|
||||
IrisPlacementInputs inputs = new IrisPlacementInputs(worldSeed, terrain(engine, ids, area),
|
||||
new PlacementModuleSettings(worldSettings(engine, ids), config.getModuleSettings(), source),
|
||||
new PlacerBlockView(placer, area, states));
|
||||
new PlacerBlockView(placer, area, states), data.getPackResources());
|
||||
long seed = ModuleSeeds.placement(worldSeed, objectKey, x, requestedY, z);
|
||||
PlacementOutcome outcome = placement.runBound(() -> new PlacementRunner(graph)
|
||||
.place(request, edit, inputs, seed, config.getModuleSettings().placement(source)));
|
||||
|
||||
@@ -35,6 +35,7 @@ final class PackResourceIds {
|
||||
private static final Pattern NOT_PATH = Pattern.compile("[^a-z0-9_./-]");
|
||||
private static final String REPLACEMENT = "_";
|
||||
private static final String UNNAMED_PACK = "pack";
|
||||
private static final String PACK_PATH = "pack";
|
||||
|
||||
private final String namespace;
|
||||
|
||||
@@ -43,6 +44,16 @@ final class PackResourceIds {
|
||||
this.namespace = name.isEmpty() ? UNNAMED_PACK : name;
|
||||
}
|
||||
|
||||
/** The namespace of every id this pack's resources have. */
|
||||
String namespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
/** The pack itself: its namespace with the path {@code pack}. */
|
||||
ResourceId packId() {
|
||||
return new ResourceId(namespace, PACK_PATH);
|
||||
}
|
||||
|
||||
/** {@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));
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
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 com.volmit.block.BlockFace;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.block.BlockTrait;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** IrisApi's blocks are the bound platform's, through their keys; what the platform cannot answer is said so. */
|
||||
public class IrisBlockRegistryTest {
|
||||
private final IrisBlockRegistry blocks = new IrisBlockRegistry();
|
||||
|
||||
@Before
|
||||
public void bindPlatform() {
|
||||
PlatformBlockState stone = mock(PlatformBlockState.class);
|
||||
when(stone.key()).thenReturn("stone");
|
||||
when(stone.isSolid()).thenReturn(true);
|
||||
when(stone.isOre()).thenReturn(false);
|
||||
PlatformRegistries registries = mock(PlatformRegistries.class);
|
||||
when(registries.blockOrNull(eq("minecraft:stone"), anyBoolean())).thenReturn(stone);
|
||||
when(registries.blockOrNull(eq("minecraft:nothing"), anyBoolean())).thenReturn(null);
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
when(platform.registries()).thenReturn(registries);
|
||||
IrisPlatforms.unbind();
|
||||
IrisPlatforms.bind(platform);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbindPlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aKeyWithoutANamespaceIsMinecrafts() {
|
||||
assertEquals(BlockState.parse("minecraft:stone"), blocks.resolve("stone"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aBlockThePlatformDoesNotKnowIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> blocks.resolve("minecraft:nothing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void traitsAnswerFromThePlatformsBlockState() {
|
||||
BlockState stone = BlockState.parse("minecraft:stone");
|
||||
|
||||
assertTrue(blocks.hasTrait(stone, BlockTrait.SOLID));
|
||||
assertFalse(blocks.hasTrait(stone, BlockTrait.ORE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whatThePlatformLayerDoesNotOfferFailsInsteadOfGuessing() {
|
||||
BlockState stone = BlockState.parse("minecraft:stone");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.hasTrait(stone, BlockTrait.GRAVITY));
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.inTag(stone, ResourceId.parse("minecraft:logs")));
|
||||
assertThrows(UnsupportedOperationException.class, () -> blocks.canAttach(stone, stone, BlockFace.UP));
|
||||
}
|
||||
}
|
||||
@@ -291,6 +291,37 @@ public class IrisModuleMantleTest {
|
||||
assertTrue(chunks.containsKey(1L << 32));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aMantleStageFindsTheSnapshotsPackResources() throws IOException {
|
||||
List<Object> found = new ArrayList<>();
|
||||
IrisModule finder = new IrisModule() {
|
||||
public ModuleInfo info() { return ModuleInfo.of("iris:finder", "Finder", "1"); }
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(Stage.mantle("iris:finder/oak")).reach(0).run(context ->
|
||||
found.add(context.resources().find(PackResourceFixture.oakKey(folder.getRoot())).orElseThrow()));
|
||||
}
|
||||
};
|
||||
Engine 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());
|
||||
PackResourceFixture.serve(data);
|
||||
when(engine.getSeedManager()).thenReturn(seeds);
|
||||
when(engine.getDimension()).thenReturn(new IrisDimension());
|
||||
when(engine.getRegion(anyInt(), anyInt())).thenReturn(new IrisRegion());
|
||||
when(engine.getSurfaceBiome(anyInt(), anyInt())).thenReturn(new IrisBiome());
|
||||
when(engine.getMinHeight()).thenReturn(MIN_Y);
|
||||
when(engine.getHeight()).thenReturn(SECTIONS * 16);
|
||||
|
||||
try (LoadedPack pack = LoadedPack.load(folder.newFolder("finder-pack").toPath(),
|
||||
ModuleHost.of(List.of(finder)).copyingJarsTo(folder.newFolder("finder-copies").toPath()))) {
|
||||
ModuleMantleComponent.forPack(engine, pack, Set.of()).getFirst().generate(0, 0, this::chunk);
|
||||
}
|
||||
|
||||
assertEquals(List.of(PackResourceFixture.OAK), found);
|
||||
}
|
||||
|
||||
/** Iris's in-memory mantle sections, without the engine's static state that would bind a platform for every later test. */
|
||||
private static final class Sections implements MantleDataAdapter<Matter> {
|
||||
public Matter createSection() { return new IrisMatter(16, 16, 16); }
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
import art.arcane.iris.engine.object.prefab.IrisObject;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.engine.object.world.region.IrisRegion;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKey;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import com.volmit.world.PackBiome;
|
||||
import com.volmit.world.PackRegion;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** The pack snapshot's resources: objects, biomes and regions by the ids a biome sample gives. */
|
||||
public class IrisPackResourcesTest {
|
||||
private static final String NAMESPACE = "example-pack";
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
private IrisData data;
|
||||
private ResourceLoader<IrisObject> objectLoader;
|
||||
private ResourceLoader<IrisBiome> biomeLoader;
|
||||
private ResourceLoader<IrisRegion> regionLoader;
|
||||
private IrisPackResources resources;
|
||||
private final GeneratedObject tree = new GeneratedObject(new BlockBox(0, 0, 0, 1, 1, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(0, 0, 0), BlockState.parse("minecraft:oak_log"))), List.of());
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void serveAPack() throws IOException {
|
||||
File packFolder = folder.newFolder(NAMESPACE);
|
||||
data = mock(IrisData.class);
|
||||
when(data.getDataFolder()).thenReturn(packFolder);
|
||||
objectLoader = mock(ResourceLoader.class);
|
||||
biomeLoader = mock(ResourceLoader.class);
|
||||
regionLoader = mock(ResourceLoader.class);
|
||||
when(data.getObjectLoader()).thenReturn(objectLoader);
|
||||
when(data.getBiomeLoader()).thenReturn(biomeLoader);
|
||||
when(data.getRegionLoader()).thenReturn(regionLoader);
|
||||
|
||||
IrisObject object = mock(IrisObject.class);
|
||||
when(object.getGeometry()).thenReturn(tree);
|
||||
when(objectLoader.getPossibleKeys()).thenReturn(new String[]{"trees/oak"});
|
||||
when(objectLoader.load("trees/oak")).thenReturn(object);
|
||||
|
||||
IrisBiome dunes = new IrisBiome();
|
||||
dunes.setName("Dunes");
|
||||
dunes.setRarity(3);
|
||||
dunes.setDerivative("minecraft:desert");
|
||||
dunes.setChildren(new KList<>(List.of("desert/oasis")));
|
||||
when(biomeLoader.getPossibleKeys()).thenReturn(new String[]{"desert/dunes", "desert"});
|
||||
when(biomeLoader.load("desert/dunes")).thenReturn(dunes);
|
||||
IrisBiome sharedKeyBiome = new IrisBiome();
|
||||
sharedKeyBiome.setName("Desert biome");
|
||||
sharedKeyBiome.setDerivative("minecraft:desert");
|
||||
when(biomeLoader.load("desert")).thenReturn(sharedKeyBiome);
|
||||
|
||||
IrisRegion desert = new IrisRegion();
|
||||
desert.setName("The Desert");
|
||||
desert.setRarity(2);
|
||||
desert.setLandBiomes(new KList<>(List.of("desert/dunes")));
|
||||
desert.setSeaBiomes(new KList<>(List.of("ocean")));
|
||||
desert.setShoreBiomes(new KList<>(List.of("beach")));
|
||||
desert.setCaveBiomes(new KList<>(List.of("caves/dry")));
|
||||
when(regionLoader.getPossibleKeys()).thenReturn(new String[]{"desert"});
|
||||
when(regionLoader.load("desert")).thenReturn(desert);
|
||||
|
||||
resources = new IrisPackResources(data);
|
||||
}
|
||||
|
||||
private ResourceId id(String path) {
|
||||
return new ResourceId(NAMESPACE, path);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void thePackIsNamedByItsNamespace() {
|
||||
assertEquals(NAMESPACE, resources.packId().namespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everySnapshotHasItsOwnId() {
|
||||
assertNotEquals(resources.snapshotId(), new IrisPackResources(data).snapshotId());
|
||||
assertEquals(resources.snapshotId(), resources.snapshotId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPackObjectIsItsGeometry() {
|
||||
Optional<GeneratedObject> found = resources.find(new ResourceKey<>(id("trees/oak"), GeneratedObject.class));
|
||||
|
||||
assertSame(tree, found.orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPackBiomeIsBuiltFromTheBiomeFile() {
|
||||
PackBiome biome = resources.require(new ResourceKey<>(id("desert/dunes"), PackBiome.class));
|
||||
|
||||
assertEquals(new PackBiome(id("desert/dunes"), "Dunes", ResourceId.parse("minecraft:desert"), 3, List.of(id("desert/oasis"))), biome);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPackRegionIsBuiltFromTheRegionFileWithItsFourBiomeListsAsPackIds() {
|
||||
PackRegion region = resources.require(new ResourceKey<>(id("desert"), PackRegion.class));
|
||||
|
||||
assertEquals(new PackRegion(id("desert"), "The Desert", 2, List.of(id("desert/dunes")), List.of(id("ocean")),
|
||||
List.of(id("beach")), List.of(id("caves/dry"))), region);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneIdCanBeABiomeAndARegion() {
|
||||
PackBiome biome = resources.require(new ResourceKey<>(id("desert"), PackBiome.class));
|
||||
PackRegion region = resources.require(new ResourceKey<>(id("desert"), PackRegion.class));
|
||||
|
||||
assertEquals("Desert biome", biome.name());
|
||||
assertEquals("The Desert", region.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anIdThePackLacksForThatKindIsAbsent() {
|
||||
assertEquals(Optional.empty(), resources.find(new ResourceKey<>(id("desert/dunes"), PackRegion.class)));
|
||||
assertEquals(Optional.empty(), resources.find(new ResourceKey<>(id("trees/birch"), GeneratedObject.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anIdOfAnotherNamespaceIsAbsent() {
|
||||
assertEquals(Optional.empty(), resources.find(new ResourceKey<>(new ResourceId("other", "trees/oak"), GeneratedObject.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTypeIrisDoesNotServeIsRejectedWhetherOrNotTheIdExists() {
|
||||
assertThrows(IllegalArgumentException.class, () -> resources.find(new ResourceKey<>(id("trees/oak"), String.class)));
|
||||
assertThrows(IllegalArgumentException.class, () -> resources.find(new ResourceKey<>(id("missing"), String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aResourceThatExistsButFailsToLoadIsAnErrorNotAbsent() {
|
||||
when(objectLoader.getPossibleKeys()).thenReturn(new String[]{"trees/broken"});
|
||||
IrisPackResources fresh = new IrisPackResources(data);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> fresh.find(new ResourceKey<>(id("trees/broken"), GeneratedObject.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eachResourceIsBuiltOncePerSnapshot() {
|
||||
ResourceKey<PackBiome> key = new ResourceKey<>(id("desert/dunes"), PackBiome.class);
|
||||
|
||||
PackBiome first = resources.require(key);
|
||||
PackBiome second = resources.require(key);
|
||||
|
||||
assertSame(first, second);
|
||||
verify(biomeLoader, times(1)).load("desert/dunes");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keysWithCapitalsAreFoundByTheLowerCaseIdTheyGet() {
|
||||
when(objectLoader.getPossibleKeys()).thenReturn(new String[]{"Trees/Oak"});
|
||||
IrisObject object = mock(IrisObject.class);
|
||||
when(object.getGeometry()).thenReturn(tree);
|
||||
when(objectLoader.load("Trees/Oak")).thenReturn(object);
|
||||
IrisPackResources fresh = new IrisPackResources(data);
|
||||
|
||||
assertSame(tree, fresh.require(new ResourceKey<>(id("trees/oak"), GeneratedObject.class)));
|
||||
}
|
||||
}
|
||||
@@ -195,4 +195,28 @@ public class ModuleChunkGenerationTest {
|
||||
when(state.key()).thenReturn(key);
|
||||
return state;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aChunkStageFindsTheSnapshotsPackResources() throws IOException {
|
||||
IrisData data = engine.getData();
|
||||
PackResourceFixture.serve(data);
|
||||
List<Object> found = new ArrayList<>();
|
||||
IrisModule finder = new IrisModule() {
|
||||
public ModuleInfo info() { return ModuleInfo.of("test:finder", "Finder", "1"); }
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(Stage.chunk("test:finder/oak")).run(context ->
|
||||
found.add(context.resources().find(PackResourceFixture.oakKey(folder.getRoot())).orElseThrow()));
|
||||
}
|
||||
};
|
||||
|
||||
try (LoadedPack finderPack = LoadedPack.load(folder.newFolder("finder-pack").toPath(),
|
||||
ModuleHost.of(List.of(finder)).copyingJarsTo(folder.newFolder("finder-copies").toPath()))) {
|
||||
when(engine.getRegion(anyInt(), anyInt())).thenReturn(new IrisRegion());
|
||||
when(engine.getSurfaceBiome(anyInt(), anyInt())).thenReturn(new IrisBiome());
|
||||
|
||||
ModuleChunkGeneration.generate(finderPack, engine, 0, 0, blocks, null, false, null, ModuleChunkGenerationTest::platformState);
|
||||
}
|
||||
|
||||
assertEquals(List.of(PackResourceFixture.OAK), found);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
import art.arcane.iris.engine.object.prefab.IrisObject;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.resource.ResourceKey;
|
||||
import com.volmit.world.BlockPosition;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** A pack with one object, {@code trees/oak}, served through the same {@link IrisPackResources} every stage of a snapshot shares. */
|
||||
public final class PackResourceFixture {
|
||||
public static final String OAK_KEY = "trees/oak";
|
||||
public static final GeneratedObject OAK = new GeneratedObject(new BlockBox(0, 0, 0, 1, 2, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(0, 0, 0), BlockState.parse("minecraft:oak_log")),
|
||||
new ObjectBlock(new BlockPosition(0, 1, 0), BlockState.parse("minecraft:oak_leaves"))), List.of());
|
||||
|
||||
private PackResourceFixture() {
|
||||
}
|
||||
|
||||
/** Makes {@code data} (a mock whose data folder is set) serve the pack's resources, and returns them. */
|
||||
@SuppressWarnings("unchecked")
|
||||
public static IrisPackResources serve(IrisData data) {
|
||||
ResourceLoader<IrisObject> objects = mock(ResourceLoader.class);
|
||||
IrisObject oak = mock(IrisObject.class);
|
||||
when(oak.getGeometry()).thenReturn(OAK);
|
||||
when(objects.getPossibleKeys()).thenReturn(new String[]{OAK_KEY});
|
||||
when(objects.load(OAK_KEY)).thenReturn(oak);
|
||||
when(data.getObjectLoader()).thenReturn(objects);
|
||||
IrisPackResources resources = new IrisPackResources(data);
|
||||
when(data.getPackResources()).thenReturn(resources);
|
||||
return resources;
|
||||
}
|
||||
|
||||
/** The key of the oak in the pack in {@code packFolder}. */
|
||||
public static ResourceKey<GeneratedObject> oakKey(java.io.File packFolder) {
|
||||
return new ResourceKey<>(new PackResourceIds(packFolder).of(OAK_KEY), GeneratedObject.class);
|
||||
}
|
||||
}
|
||||
+36
@@ -18,6 +18,7 @@ import art.arcane.iris.builtin.bore.BoreStages;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.BuiltInPack;
|
||||
import art.arcane.iris.core.modules.ModuleObjectPlacement;
|
||||
import art.arcane.iris.core.modules.PackResourceFixture;
|
||||
import art.arcane.iris.core.modules.ObjectPlacementResult;
|
||||
import art.arcane.iris.core.modules.PlatformBlockStates;
|
||||
import art.arcane.iris.engine.IrisComplex;
|
||||
@@ -407,6 +408,41 @@ public class IrisObjectPlacementRegressionTest {
|
||||
}
|
||||
}
|
||||
|
||||
/** Looks up the pack's oak through the placement's resources. */
|
||||
private static final class FindModule implements IrisModule {
|
||||
private static final PlacementStage<Void> FIND = Stage.placement("test:find/oak");
|
||||
private final java.io.File packFolder;
|
||||
private final List<Object> found;
|
||||
|
||||
private FindModule(java.io.File packFolder, List<Object> found) {
|
||||
this.packFolder = packFolder;
|
||||
this.found = found;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("test:find", "Find", "1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(FIND).requires(BlocksStages.COMPLETE).run(context ->
|
||||
found.add(context.resources().find(PackResourceFixture.oakKey(packFolder)).orElseThrow()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPlacementStageFindsTheSnapshotsPackResources() throws IOException {
|
||||
PackResourceFixture.serve(data);
|
||||
List<Object> found = new ArrayList<>();
|
||||
usePack(List.of(new FindModule(folder.getRoot(), found)));
|
||||
|
||||
ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null);
|
||||
|
||||
assertTrue(result.isPlaced());
|
||||
assertEquals(List.of(PackResourceFixture.OAK), found);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromBottomKeepsItsAnchorWithoutSamplingTerrain() {
|
||||
RecordingPlacer placer = new RecordingPlacer(null);
|
||||
|
||||
Reference in new issue
Block a user