mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +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
@@ -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