mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
iris:loot and iris:spawn leave the compiled-in list for the built-ins jar (JAR_MODULE_IDS, BuiltInPack.jarModules); pack-reading tests put the jar into their pack; the entity key completion reads the pack's entity ids; the missing-jar warning names loot and spawns; RarityWeightingTest replaces IRareWeightingTest
This commit is contained in:
1 parent
2b49ffb759
commit
ae73115c5a
18 files changed
+460
-189
No files matched your search
@@ -21,9 +21,7 @@ package art.arcane.iris.core.modules;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.engine.actuator.DecorantModule;
|
||||
import art.arcane.iris.engine.actuator.TerrainModule;
|
||||
import art.arcane.iris.engine.loot.LootModule;
|
||||
import art.arcane.iris.engine.mantle.components.ObjectModule;
|
||||
import art.arcane.iris.engine.spawn.SpawnModule;
|
||||
import art.arcane.iris.engine.mantle.components.StructureModule;
|
||||
import art.arcane.iris.engine.modifier.CarveModule;
|
||||
import art.arcane.iris.engine.modifier.DepositModule;
|
||||
@@ -63,7 +61,7 @@ public final class IrisModuleHost {
|
||||
* {@code IrisModule/builtin-modules.txt}) instead of being compiled into Iris. A pack loads them from its {@code modules}
|
||||
* folder, and {@link MissingBuiltInModules} warns when it loads none; a test keeps this equal to the jar's list.
|
||||
*/
|
||||
public static final List<String> JAR_MODULE_IDS = List.of("iris:post", "iris:perfection", "iris:matter");
|
||||
public static final List<String> JAR_MODULE_IDS = List.of("iris:post", "iris:perfection", "iris:matter", "iris:loot", "iris:spawn");
|
||||
/** Iris's pack object model: built-in settings may hold these classes, which Iris reads and describes itself. */
|
||||
public static final String HOST_TYPE_PACKAGE = "art.arcane.iris.engine.object";
|
||||
private static final String[] JAR_COPY_FOLDER = {"cache", "module-jars"};
|
||||
@@ -88,7 +86,7 @@ public final class IrisModuleHost {
|
||||
|
||||
private static List<IrisModule> generationModules() {
|
||||
return List.of(new TerrainModule(), new CarveModule(), new ObjectModule(), new StructureModule(),
|
||||
new FloatingModule(), new DepositModule(), new DecorantModule(), new LootModule(), new SpawnModule());
|
||||
new FloatingModule(), new DepositModule(), new DecorantModule());
|
||||
}
|
||||
|
||||
/** Iris's own log, labelled {@code [iris]}, for host code that calls module classes outside any module's stage, service or hook. */
|
||||
|
||||
@@ -43,7 +43,8 @@ final class MissingBuiltInModules {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of("Pack " + packFolder.getName() + " loads without the built-in modules (" + JAR_NAME + "): its chunks get"
|
||||
+ " no objects or structures, no surface smoothing and no clean-up of floating decorations. Copy " + JAR_NAME
|
||||
+ " no objects or structures, no surface smoothing and no clean-up of floating decorations, and its world has no loot,"
|
||||
+ " block drops, entity spawns or ambient effects. Copy " + JAR_NAME
|
||||
+ " into " + new File(packFolder, PackModuleSet.MODULES_FOLDER).getPath() + " to fix it.");
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,7 +21,7 @@ package art.arcane.iris.util.common.director.specialhandlers;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.PackModulesException;
|
||||
import art.arcane.iris.core.pack.PackDirectoryResolver;
|
||||
import art.arcane.iris.engine.spawn.SpawnResources;
|
||||
import art.arcane.iris.builtin.spawn.EntityDefinition;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.util.common.director.DirectorParameterHandler;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
@@ -70,7 +70,7 @@ public final class EntityKeyHandler implements DirectorParameterHandler<String>
|
||||
/** A pack whose modules fail to load has no entities to offer; its failure was reported when it loaded. */
|
||||
private static void addEntityKeys(Set<String> keys, IrisData data) {
|
||||
try {
|
||||
keys.addAll(SpawnResources.entityKeys(data));
|
||||
data.getPackResources().ids(EntityDefinition.class).forEach(id -> keys.add(id.path()));
|
||||
} catch (PackModulesException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.stream.Stream;
|
||||
* every class {@code builtin-modules.txt} lists ({@code module} lines with their nested classes, {@code include} patterns)
|
||||
* and the entry points in order. Tests load it from a pack's {@code modules} folder without building IrisModule.
|
||||
*/
|
||||
final class BuiltInModulesJar {
|
||||
public final class BuiltInModulesJar {
|
||||
private static final String LIST_PROPERTY = "iris.builtinModulesList";
|
||||
private static final String SERVICE_ENTRY = "META-INF/services/com.volmit.IrisModule";
|
||||
private static final String JAR_NAME = "iris-builtins.jar";
|
||||
@@ -45,7 +45,7 @@ final class BuiltInModulesJar {
|
||||
}
|
||||
|
||||
/** Writes {@code <pack>/modules/iris-builtins.jar} and returns its entry classes. */
|
||||
static List<String> writeInto(Path pack) throws IOException {
|
||||
public static List<String> writeInto(Path pack) throws IOException {
|
||||
List<String> modules = new ArrayList<>();
|
||||
List<PathMatcher> classes = new ArrayList<>();
|
||||
for (String line : Files.readAllLines(Path.of(System.getProperty(LIST_PROPERTY)))) {
|
||||
|
||||
@@ -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.builtin.loot.BlockDropSettings;
|
||||
import art.arcane.iris.builtin.loot.LootSettings;
|
||||
import art.arcane.iris.builtin.loot.LootTable;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.IrisRegistrant;
|
||||
import art.arcane.iris.engine.IrisComplex;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.engine.framework.SeedManager;
|
||||
import art.arcane.iris.engine.loot.BlockDropping;
|
||||
import art.arcane.iris.engine.loot.LootResources;
|
||||
import art.arcane.iris.engine.modifier.ApiChunk;
|
||||
import art.arcane.iris.engine.object.TestSettings;
|
||||
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.block.BlockRegistry;
|
||||
import com.volmit.block.BlockSpec;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.host.LoadedPack;
|
||||
import com.volmit.host.ModuleHost;
|
||||
import com.volmit.service.BlockDrops;
|
||||
import com.volmit.service.Loot;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import com.volmit.world.PackWorld;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** The services of the loot and spawn modules, loaded from the built-ins jar, answer about worlds Iris runs through the live view of the world. */
|
||||
public class BuiltInModulesJarServicesTest {
|
||||
private static final int X = 10;
|
||||
private static final int Y = 70;
|
||||
private static final int Z = -20;
|
||||
private static final BlockPosition AT = new BlockPosition(X, Y, Z);
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
private Engine engine;
|
||||
private IrisBiome biome;
|
||||
private IrisDimension dimension;
|
||||
private PackWorld world;
|
||||
private ModuleResourceFixture tables;
|
||||
|
||||
@Before
|
||||
public void buildWorld() {
|
||||
engine = mock(Engine.class);
|
||||
IrisData data = mock(IrisData.class);
|
||||
tables = ModuleResourceFixture.serve(data);
|
||||
BlockRegistry blocks = mock(BlockRegistry.class);
|
||||
when(blocks.resolve(any(BlockState.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(blocks.resolve(any(BlockSpec.class))).thenAnswer(invocation -> BlockState.parse("minecraft:" + invocation.<BlockSpec>getArgument(0).block()));
|
||||
when(data.getPackResources().blocks()).thenReturn(blocks);
|
||||
when(data.getPackResources().items()).thenReturn(item -> OptionalInt.of(0));
|
||||
when(engine.getData()).thenReturn(data);
|
||||
when(engine.getMinHeight()).thenReturn(-64);
|
||||
IrisComplex complex = mock(IrisComplex.class, RETURNS_DEEP_STUBS);
|
||||
when(complex.getHeightStream().get(X, Z)).thenReturn(64D);
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
SeedManager seeds = mock(SeedManager.class);
|
||||
when(seeds.getLoot()).thenReturn(5L);
|
||||
when(engine.getSeedManager()).thenReturn(seeds);
|
||||
biome = bare(IrisBiome.class);
|
||||
dimension = bare(IrisDimension.class);
|
||||
when(engine.getBiome(X, Y, Z)).thenReturn(biome);
|
||||
when(engine.getSurfaceBiome(X, Z)).thenReturn(biome);
|
||||
IrisRegion region = bare(IrisRegion.class);
|
||||
when(engine.getRegion(X, Z)).thenReturn(region);
|
||||
when(engine.getDimension()).thenReturn(dimension);
|
||||
world = new EngineGeneration(engine).world();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blockDropsOfTheBiomeAtABrokenBlockComeFromTheJarsModule() throws IOException {
|
||||
when(biome.getModuleSettings()).thenReturn(BuiltInPack.settings(TestSettings.of(BlockDropSettings.class, """
|
||||
{"drops": [{"blocks": [{"block": "stone"}], "replaceVanillaDrops": true,
|
||||
"drops": [{"type": "gravel", "minAmount": 2, "maxAmount": 2}]}]}""")));
|
||||
|
||||
try (LoadedPack loaded = load()) {
|
||||
BlockDrops drops = loaded.catalog().services(BlockDrops.class).getFirst();
|
||||
|
||||
assertEquals(BlockDropping.class.getName(), drops.getClass().getName());
|
||||
assertNotSame(BlockDropping.class, drops.getClass());
|
||||
BlockDrops.Result result = drops.drops(world, new BlockDrops.Broken(AT, new BlockState("minecraft:stone")));
|
||||
assertEquals(1, result.drops().size());
|
||||
assertEquals("gravel", result.drops().getFirst().item());
|
||||
assertEquals(2, result.drops().getFirst().amount());
|
||||
assertTrue(result.replacesVanillaDrops());
|
||||
assertEquals(BlockDrops.Result.NONE, drops.drops(world, new BlockDrops.Broken(AT, new BlockState("minecraft:dirt"))));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aContainersTablesAreThoseOfTheDimensionAtItsColumn() throws IOException {
|
||||
when(dimension.getModuleSettings()).thenReturn(BuiltInPack.settings(TestSettings.of(LootSettings.class, "{\"tables\": [\"dungeon/chest\"]}")));
|
||||
tables.resource(LootResources.TABLES, "dungeon/chest", TestSettings.of(LootTable.class, "{}"));
|
||||
|
||||
try (LoadedPack loaded = load()) {
|
||||
Loot loot = loaded.catalog().services(Loot.class).getFirst();
|
||||
|
||||
List<Loot.Source> tables = loot.tables(world, new Loot.Site(AT, null, false, false));
|
||||
|
||||
assertEquals(List.of(new Loot.Table("dungeon/chest")), tables);
|
||||
}
|
||||
}
|
||||
|
||||
private LoadedPack load() throws IOException {
|
||||
Path pack = BuiltInPack.newPackFolder(folder);
|
||||
BuiltInModulesJar.writeInto(pack);
|
||||
return LoadedPack.load(pack, ModuleHost.of(ApiChunk.standIns())
|
||||
.exposing(IrisModuleHost.EXPOSED_PACKAGES.toArray(String[]::new))
|
||||
.copyingJarsTo(folder.newFolder().toPath()));
|
||||
}
|
||||
|
||||
private static <T extends IrisRegistrant> T bare(Class<T> type) {
|
||||
T owner = mock(type);
|
||||
when(owner.getModuleSettings()).thenReturn(ModuleSettings.NONE);
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,11 @@ 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.loot.LootModule;
|
||||
import art.arcane.iris.engine.modifier.MatterModule;
|
||||
import art.arcane.iris.engine.modifier.PerfectionModule;
|
||||
import art.arcane.iris.engine.modifier.PostModule;
|
||||
import art.arcane.iris.engine.spawn.SpawnModule;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.host.LoadedPack;
|
||||
@@ -51,7 +53,7 @@ public final class BuiltInPack {
|
||||
* with Iris; {@code BuiltInModulesJarTest} loads the same modules from the jar instead.
|
||||
*/
|
||||
public static List<IrisModule> jarModules() {
|
||||
return List.of(new PostModule(), new PerfectionModule(), new MatterModule());
|
||||
return List.of(new PostModule(), new PerfectionModule(), new MatterModule(), new LootModule(), new SpawnModule());
|
||||
}
|
||||
|
||||
/** Every built-in module: Iris's own and the compiled-in copies of the jar's ({@link #jarModules()}). */
|
||||
|
||||
@@ -37,7 +37,7 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
/** The services and hooks Iris's built-in modules provide, which Iris core asks instead of their features. */
|
||||
public class BuiltInServicesTest {
|
||||
private static final FeatureCatalog BUILT_INS = ModuleResourceFixture.catalogOf(IrisModuleHost.builtInModules().toArray(IrisModule[]::new));
|
||||
private static final FeatureCatalog BUILT_INS = ModuleResourceFixture.catalogOf(BuiltInPack.allBuiltIns().toArray(IrisModule[]::new));
|
||||
|
||||
@Test
|
||||
public void objectsTellWhichObjectsAChunkGets() {
|
||||
|
||||
@@ -23,6 +23,7 @@ import art.arcane.iris.builtin.spawn.EntityDefinition;
|
||||
import art.arcane.iris.builtin.spawn.SpawnMarker;
|
||||
import art.arcane.iris.builtin.spawn.Spawner;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.BuiltInModulesJar;
|
||||
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;
|
||||
@@ -123,6 +124,7 @@ public class ModuleResourceClosureTest {
|
||||
write(pack, "entities/beta/sentinel.json", "{\"type\":\"minecraft:skeleton\"}");
|
||||
write(pack, "entities/cow.json", "{\"type\":\"minecraft:cow\"}");
|
||||
write(pack, "markers/camp.json", "{\"spawners\":[\"marker/spawner\"]}");
|
||||
BuiltInModulesJar.writeInto(pack.toPath());
|
||||
return pack.toPath();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import art.arcane.iris.core.modules.BuiltInPack;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.engine.IrisComplex;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.builtin.loot.LootSettings;
|
||||
import art.arcane.iris.builtin.loot.LootTable;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
@@ -72,8 +73,8 @@ public class LootEnvironmentPinTest {
|
||||
surface = biome("surface");
|
||||
cave = biome("cave");
|
||||
when(engine.getDimension()).thenReturn(dimension);
|
||||
when(complex.getRegionStream().get(X, Z)).thenReturn(region);
|
||||
when(complex.getTrueBiomeStream().get(X, Z)).thenReturn(surface);
|
||||
when(engine.getRegion(X, Z)).thenReturn(region);
|
||||
when(engine.getSurfaceBiome(X, Z)).thenReturn(surface);
|
||||
when(complex.getHeightStream().get(X, Z)).thenReturn(64D);
|
||||
when(engine.getCaveBiome(X, CAVE_Y, Z)).thenReturn(cave);
|
||||
when(engine.getCaveBiome(X, SURFACE_Y, Z)).thenReturn(surface);
|
||||
@@ -112,7 +113,7 @@ public class LootEnvironmentPinTest {
|
||||
|
||||
private List<String> identities(boolean objectLootDefined, int y) {
|
||||
List<String> sources = new ArrayList<>();
|
||||
LootTables.resolveEnvironmentSources(sources, engine, new RNG(19L), X, y, Z, objectLootDefined, key -> key);
|
||||
LootTables.resolveEnvironmentSources(sources, new EngineGeneration(engine).world(), new RNG(19L), X, y, Z, objectLootDefined, key -> key);
|
||||
return sources;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package art.arcane.iris.engine.loot;
|
||||
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.builtin.loot.LootResolver;
|
||||
|
||||
import art.arcane.iris.engine.IrisComplex;
|
||||
@@ -18,6 +19,7 @@ import com.volmit.config.Desc;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.item.ItemRegistry;
|
||||
import com.volmit.world.PackWorld;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -107,7 +109,7 @@ public class LootResolverTest {
|
||||
|
||||
LootTables.resolveEnvironmentSources(
|
||||
sources,
|
||||
fixture.engine,
|
||||
fixture.world,
|
||||
new RNG(19L),
|
||||
10,
|
||||
70,
|
||||
@@ -126,7 +128,7 @@ public class LootResolverTest {
|
||||
|
||||
LootTables.resolveEnvironmentSources(
|
||||
sources,
|
||||
fixture.engine,
|
||||
fixture.world,
|
||||
new RNG(19L),
|
||||
10,
|
||||
70,
|
||||
@@ -264,6 +266,7 @@ public class LootResolverTest {
|
||||
|
||||
private static final class ResolutionFixture {
|
||||
private final Engine engine = mock(Engine.class, RETURNS_DEEP_STUBS);
|
||||
private final PackWorld world;
|
||||
|
||||
private ResolutionFixture() {
|
||||
IrisDimension dimension = mock(IrisDimension.class);
|
||||
@@ -282,10 +285,11 @@ public class LootResolverTest {
|
||||
when(region.getModuleSettings()).thenReturn(fallbackTo("region"));
|
||||
when(surface.getModuleSettings()).thenReturn(fallbackTo("surface"));
|
||||
when(surface.getLoadKey()).thenReturn("surface");
|
||||
when(complex.getRegionStream().get(10, 20)).thenReturn(region);
|
||||
when(complex.getTrueBiomeStream().get(10, 20)).thenReturn(surface);
|
||||
when(engine.getRegion(10, 20)).thenReturn(region);
|
||||
when(engine.getSurfaceBiome(10, 20)).thenReturn(surface);
|
||||
when(complex.getHeightStream().get(10, 20)).thenReturn(64D);
|
||||
when(engine.getCaveBiome(10, 70, 20)).thenReturn(surface);
|
||||
world = new EngineGeneration(engine).world();
|
||||
}
|
||||
|
||||
private static ModuleSettings fallbackTo(String table) {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package art.arcane.iris.engine.loot;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.BuiltInModulesJar;
|
||||
import art.arcane.iris.core.project.ModuleResourceSchemas;
|
||||
import art.arcane.iris.core.project.SchemaTestPlatform;
|
||||
import art.arcane.iris.builtin.loot.LootTable;
|
||||
@@ -52,7 +53,7 @@ public class LootResourcesTest {
|
||||
|
||||
assertEquals(List.of("iris:loot/table"), catalog.resourceKinds().stream().map(registration -> registration.kind().id().toString())
|
||||
.filter(id -> id.startsWith("iris:loot/")).toList());
|
||||
assertTrue(catalog.validators().stream().map(ValidatorRegistration::validator).anyMatch(LootValidator.class::isInstance));
|
||||
assertTrue(catalog.validators().stream().map(ValidatorRegistration::validator).anyMatch(validator -> validator.getClass().getName().equals(LootValidator.class.getName())));
|
||||
assertFalse(data.getLoaders().containsKey(LootTable.class));
|
||||
} finally {
|
||||
data.close();
|
||||
@@ -64,18 +65,18 @@ public class LootResourcesTest {
|
||||
Path pack = lootPack("tables");
|
||||
IrisData data = IrisData.openValidation(pack.toFile());
|
||||
try {
|
||||
LootTable table = LootResources.table(data, "dungeon/chest");
|
||||
LootTable table = LootResources.table(data.getPackResources(), "dungeon/chest");
|
||||
|
||||
assertEquals("Chest", table.name());
|
||||
assertEquals(1, table.loot().size());
|
||||
assertEquals("stick", table.loot().getFirst().type());
|
||||
assertEquals(List.of("dungeon/chest"), LootResources.tableKeys(data));
|
||||
assertNull(LootResources.table(data, "missing"));
|
||||
assertNull(LootResources.table(data, " "));
|
||||
assertEquals(List.of("dungeon/chest"), LootResources.tableKeys(data.getPackResources()));
|
||||
assertNull(LootResources.table(data.getPackResources(), "missing"));
|
||||
assertNull(LootResources.table(data.getPackResources(), " "));
|
||||
|
||||
write(pack, "loot/dungeon/chest.json", "{\"name\":\"Renamed\"}");
|
||||
data.dump();
|
||||
assertEquals("Renamed", LootResources.table(data, "dungeon/chest").name());
|
||||
assertEquals("Renamed", LootResources.table(data.getPackResources(), "dungeon/chest").name());
|
||||
} finally {
|
||||
data.close();
|
||||
}
|
||||
@@ -101,6 +102,7 @@ public class LootResourcesTest {
|
||||
write(pack.toPath(), "regions/region.json", "{\"landBiomes\":[\"biome\"]}");
|
||||
write(pack.toPath(), "biomes/biome.json", "{\"name\":\"Biome\"}");
|
||||
write(pack.toPath(), "loot/dungeon/chest.json", "{\"name\":\"Chest\",\"loot\":[{\"type\":\"stick\"}]}");
|
||||
BuiltInModulesJar.writeInto(pack.toPath());
|
||||
return pack.toPath();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,55 +1,68 @@
|
||||
package art.arcane.iris.engine.loot;
|
||||
|
||||
import art.arcane.iris.builtin.loot.LootEntry;
|
||||
import art.arcane.iris.builtin.loot.LootSettings;
|
||||
import art.arcane.iris.builtin.loot.LootTable;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.engine.object.TestSettings;
|
||||
import com.volmit.FeatureAttachment.Target;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.FeatureSettings.Entry;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.validation.Problem;
|
||||
import com.volmit.validation.Severity;
|
||||
import org.junit.Rule;
|
||||
import com.volmit.validation.ValidationContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class LootValidatorTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
private ModuleResourceFixture tables;
|
||||
private PackResources resources;
|
||||
private final List<Entry<LootSettings>> references = new ArrayList<>();
|
||||
|
||||
@Before
|
||||
public void servePack() {
|
||||
IrisData data = mock(IrisData.class);
|
||||
tables = ModuleResourceFixture.serve(data);
|
||||
resources = data.getPackResources();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsValidLootTableAndReference() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "loot/global/clutter.json", "{\"name\":\"Clutter\",\"minPicked\":1,\"maxPicked\":2,"
|
||||
public void acceptsValidLootTableAndReference() {
|
||||
table("global/clutter", "{\"name\":\"Clutter\",\"minPicked\":1,\"maxPicked\":2,"
|
||||
+ "\"loot\":[{\"type\":\"stick\",\"rarity\":2,\"minAmount\":1,\"maxAmount\":3}]}");
|
||||
write(pack, "dimensions/main.json", "{\"loot\":{\"mode\":\"FALLBACK\",\"multiplier\":0.5,"
|
||||
+ "\"tables\":[\"global/clutter\"]}}");
|
||||
reference(Target.DIMENSION, "main", "{\"mode\":\"FALLBACK\",\"multiplier\":0.5,\"tables\":[\"global/clutter\"]}");
|
||||
|
||||
assertTrue(errors(pack).isEmpty());
|
||||
assertTrue(errors().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsMissingLootReference() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "dimensions/main.json", "{\"loot\":{\"tables\":[\"missing\"]}}");
|
||||
public void rejectsMissingLootReference() {
|
||||
reference(Target.DIMENSION, "main", "{\"tables\":[\"missing\"]}");
|
||||
|
||||
assertEquals(List.of("Dimension 'main'.loot.tables[0] references missing loot table 'missing'."),
|
||||
errors(pack));
|
||||
assertEquals(List.of("Dimension 'main'.loot.tables[0] references missing loot table 'missing'."), errors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsInvalidLootBoundsAndEmptyEntries() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "loot/broken.json", "{\"rarity\":0,\"minPicked\":4,\"maxPicked\":2,\"maxTries\":0,"
|
||||
public void rejectsInvalidLootBoundsAndEmptyEntries() {
|
||||
table("broken", "{\"rarity\":0,\"minPicked\":4,\"maxPicked\":2,\"maxTries\":0,"
|
||||
+ "\"loot\":[{\"type\":\"\",\"rarity\":0,\"minAmount\":3,\"maxAmount\":1,"
|
||||
+ "\"enchantments\":[{\"enchantment\":\"\",\"minLevel\":4,\"maxLevel\":2,\"chance\":2}]}]}");
|
||||
|
||||
List<String> errors = errors(pack);
|
||||
List<String> errors = errors();
|
||||
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains(".rarity must be at least 1")));
|
||||
assertTrue(errors.stream().anyMatch(error -> error.contains(".minPicked must not exceed")));
|
||||
@@ -61,45 +74,42 @@ public class LootValidatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsInvalidReferenceModeAndMultiplier() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "dimensions/main.json", "{\"loot\":{\"mode\":\"MERGE\",\"multiplier\":-1,\"tables\":[]}}");
|
||||
public void aTableWithoutEntriesIsRejected() {
|
||||
table("empty", "{\"name\":\"Empty\"}");
|
||||
|
||||
List<String> errors = errors(pack);
|
||||
|
||||
assertEquals(2, errors.size());
|
||||
assertTrue(errors.get(0).contains(".mode must be"));
|
||||
assertTrue(errors.get(1).contains(".multiplier must be"));
|
||||
assertEquals(List.of("Loot table 'empty'.loot must be a non-empty array."), errors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enforcesPublishedLootMultiplierCap() throws Exception {
|
||||
File acceptedPack = temporaryFolder.newFolder("accepted-pack");
|
||||
write(acceptedPack, "dimensions/main.json", "{\"loot\":{\"multiplier\":16,\"tables\":[]}}");
|
||||
assertTrue(errors(acceptedPack).isEmpty());
|
||||
public void rejectsInvalidReferenceMultiplier() {
|
||||
reference(Target.DIMENSION, "main", "{\"multiplier\":-1,\"tables\":[]}");
|
||||
|
||||
File rejectedPack = temporaryFolder.newFolder("rejected-pack");
|
||||
write(rejectedPack, "dimensions/main.json", "{\"loot\":{\"multiplier\":16.01,\"tables\":[]}}");
|
||||
assertEquals(List.of("Dimension 'main'.loot.multiplier must be a finite number from 0 to 16."),
|
||||
errors(rejectedPack));
|
||||
assertEquals(List.of("Dimension 'main'.loot.multiplier must be a finite number from 0 to 16."), errors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsPublishedLootWorkBounds() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "loot/boundary.json", "{\"minPicked\":64,\"maxPicked\":64,\"maxTries\":256,"
|
||||
public void enforcesPublishedLootMultiplierCap() {
|
||||
reference(Target.DIMENSION, "accepted", "{\"multiplier\":16,\"tables\":[]}");
|
||||
assertTrue(errors().isEmpty());
|
||||
|
||||
reference(Target.DIMENSION, "rejected", "{\"multiplier\":16.01,\"tables\":[]}");
|
||||
assertEquals(List.of("Dimension 'rejected'.loot.multiplier must be a finite number from 0 to 16."), errors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsPublishedLootWorkBounds() {
|
||||
table("boundary", "{\"minPicked\":64,\"maxPicked\":64,\"maxTries\":256,"
|
||||
+ "\"loot\":[{\"type\":\"stone\",\"minAmount\":64,\"maxAmount\":64}]}");
|
||||
|
||||
assertTrue(errors(pack).isEmpty());
|
||||
assertTrue(errors().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsLootWorkBoundsAbovePublishedCaps() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "loot/excessive.json", "{\"minPicked\":65,\"maxPicked\":65,\"maxTries\":257,"
|
||||
public void rejectsLootWorkBoundsAbovePublishedCaps() {
|
||||
table("excessive", "{\"minPicked\":65,\"maxPicked\":65,\"maxTries\":257,"
|
||||
+ "\"loot\":[{\"type\":\"stone\",\"minAmount\":65,\"maxAmount\":65}]}");
|
||||
|
||||
List<String> errors = errors(pack);
|
||||
List<String> errors = errors();
|
||||
|
||||
assertTrue(errors.contains("Loot table 'excessive'.minPicked must be at most 64."));
|
||||
assertTrue(errors.contains("Loot table 'excessive'.maxPicked must be at most 64."));
|
||||
@@ -124,43 +134,89 @@ public class LootValidatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearModeWithTablesWarnsWithoutBlocking() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("clear-warn");
|
||||
write(pack, "loot/t.json", "{\"loot\":[{\"type\":\"minecraft:stone\"}]}");
|
||||
write(pack, "biomes/x.json", "{\"loot\":{\"mode\":\"CLEAR\",\"tables\":[\"t\"]}}");
|
||||
public void clearModeWithTablesWarnsWithoutBlocking() {
|
||||
table("t", "{\"loot\":[{\"type\":\"minecraft:stone\"}]}");
|
||||
reference(Target.BIOME, "x", "{\"mode\":\"CLEAR\",\"tables\":[\"t\"]}");
|
||||
|
||||
|
||||
assertTrue(errors(pack).isEmpty());
|
||||
assertTrue(errors().isEmpty());
|
||||
assertEquals(List.of("Biome 'x'.loot uses mode CLEAR and lists 1 table(s); CLEAR clears parent tables"
|
||||
+ " and contributes no tables of its own, so these entries are dead. Use REPLACE to substitute them."),
|
||||
warnings(pack));
|
||||
warnings());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearModeWithMissingTableStillBlocksOnTheDanglingKey() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("clear-dangling");
|
||||
write(pack, "biomes/x.json", "{\"loot\":{\"mode\":\"CLEAR\",\"tables\":[\"missing\"]}}");
|
||||
public void clearModeWithMissingTableStillBlocksOnTheDanglingKey() {
|
||||
reference(Target.BIOME, "x", "{\"mode\":\"CLEAR\",\"tables\":[\"missing\"]}");
|
||||
|
||||
|
||||
assertTrue(errors(pack).contains(
|
||||
"Biome 'x'.loot.tables[0] references missing loot table 'missing'."));
|
||||
assertTrue(errors().contains("Biome 'x'.loot.tables[0] references missing loot table 'missing'."));
|
||||
}
|
||||
|
||||
private static List<String> errors(File pack) {
|
||||
return messages(pack, Severity.ERROR);
|
||||
@Test
|
||||
public void aTableThatFailsToLoadIsReportedWithoutStoppingTheCheck() {
|
||||
table("fine", "{\"loot\":[{\"type\":\"stone\"}]}");
|
||||
table("broken", "{\"loot\":[{\"type\":\"stone\"}]}");
|
||||
when(resources.find(LootResources.TABLES.key(new ResourceId("test", "broken")))).thenThrow(new IllegalStateException("unreadable"));
|
||||
|
||||
assertEquals(List.of("Loot table 'broken' cannot be read; the log says what is wrong with the file."), errors());
|
||||
}
|
||||
|
||||
private static List<String> warnings(File pack) {
|
||||
return messages(pack, Severity.WARNING);
|
||||
@Test
|
||||
public void referencesAreFoundByTheIdThePackGivesTheirKey() {
|
||||
table("Global/Clutter", "{\"loot\":[{\"type\":\"stick\"}]}");
|
||||
reference(Target.REGION, "plains", "{\"tables\":[\"Global/Clutter\"]}");
|
||||
|
||||
assertTrue(errors().isEmpty());
|
||||
}
|
||||
|
||||
private static List<String> messages(File pack, Severity severity) {
|
||||
return LootValidator.check(pack).stream().filter(problem -> problem.severity() == severity).map(Problem::message).toList();
|
||||
private void table(String key, String json) {
|
||||
tables.resource(LootResources.TABLES, key, TestSettings.of(LootTable.class, json));
|
||||
}
|
||||
|
||||
private void write(File root, String relative, String content) throws Exception {
|
||||
File file = new File(root, relative);
|
||||
Files.createDirectories(file.toPath().getParent());
|
||||
Files.writeString(file.toPath(), content, StandardCharsets.UTF_8);
|
||||
private void reference(Target level, String key, String json) {
|
||||
String folder = switch (level) {
|
||||
case DIMENSION -> "dimension";
|
||||
case REGION -> "region";
|
||||
default -> "biome";
|
||||
};
|
||||
references.add(new Entry<>(level, new ResourceId("test", folder + "/" + key), TestSettings.of(LootSettings.class, json)));
|
||||
}
|
||||
|
||||
private List<String> errors() {
|
||||
return messages(Severity.ERROR);
|
||||
}
|
||||
|
||||
private List<String> warnings() {
|
||||
return messages(Severity.WARNING);
|
||||
}
|
||||
|
||||
private List<String> messages(Severity severity) {
|
||||
List<Problem> problems = new ArrayList<>();
|
||||
new LootValidator().validate(context(problems));
|
||||
return problems.stream().filter(problem -> problem.severity() == severity).map(Problem::message).toList();
|
||||
}
|
||||
|
||||
private ValidationContext context(List<Problem> problems) {
|
||||
return new ValidationContext() {
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackSettings settings() {
|
||||
return new PackSettings() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <F extends IrisFeature> List<Entry<F>> declared(Class<F> featureType) {
|
||||
return featureType == LootSettings.class ? (List<Entry<F>>) (List<?>) List.copyOf(references) : List.of();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void report(Problem problem) {
|
||||
problems.add(problem);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -21,24 +21,27 @@ package art.arcane.iris.engine.loot;
|
||||
import art.arcane.iris.builtin.loot.LootTable;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.object.TestSettings;
|
||||
import art.arcane.iris.engine.object.loot.IObjectLoot;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.iris.builtin.loot.ObjectLoot;
|
||||
import art.arcane.iris.builtin.loot.ObjectLootEntry;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.host.ModuleLogs;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.block.BlockSpec;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.service.Loot;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -53,16 +56,6 @@ public class PlacementLootTest {
|
||||
|
||||
private PlacementLoot placement;
|
||||
|
||||
@BeforeClass
|
||||
public static void bindPlatform() {
|
||||
UsableBlockPlatform.bind();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void unbindPlatform() {
|
||||
UsableBlockPlatform.unbind();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void servePackTables() {
|
||||
IrisData data = mock(IrisData.class);
|
||||
@@ -70,12 +63,17 @@ public class PlacementLootTest {
|
||||
for (String key : List.of("everywhere", "chests", "this-chest", "other")) {
|
||||
fixture.resource(LootResources.TABLES, key, TestSettings.of(LootTable.class, "{\"name\": \"" + key + "\"}"));
|
||||
}
|
||||
placement = new PlacementLoot(data, () -> Set.of("minecraft:chests/simple_dungeon"), LOOT_SEED, ModuleLogs.discarding());
|
||||
PackResources resources = data.getPackResources();
|
||||
BlockRegistry blocks = mock(BlockRegistry.class);
|
||||
when(resources.blocks()).thenReturn(blocks);
|
||||
when(blocks.resolve(any(BlockState.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(blocks.resolve(any(BlockSpec.class))).thenAnswer(invocation -> BlockState.parse(invocation.<BlockSpec>getArgument(0).block()));
|
||||
placement = new PlacementLoot(resources, () -> Set.of("minecraft:chests/simple_dungeon"), LOOT_SEED, ModuleLogs.discarding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTableThatNamesTheExactStateBeatsOneThatNamesTheKindOfBlock() {
|
||||
List<IObjectLoot> pack = List.of(entry("everywhere", 1, false), entry("chests", 1, false, CHEST), entry("this-chest", 1, true, CHEST));
|
||||
List<ObjectLootEntry> pack = List.of(entry("everywhere", 1, false), entry("chests", 1, false, CHEST), entry("this-chest", 1, true, CHEST));
|
||||
|
||||
assertEquals(new Loot.Table("this-chest"), pick(List.of(), pack, CHEST));
|
||||
assertEquals(new Loot.Table("chests"), pick(List.of(), pack, EAST_CHEST));
|
||||
@@ -83,26 +81,26 @@ public class PlacementLootTest {
|
||||
|
||||
@Test
|
||||
public void aTableThatNamesNoBlockServesChestsNothingElseNames() {
|
||||
List<IObjectLoot> pack = List.of(entry("everywhere", 1, false), entry("other", 1, false, "minecraft:barrel"));
|
||||
List<ObjectLootEntry> pack = List.of(entry("everywhere", 1, false), entry("other", 1, false, "minecraft:barrel"));
|
||||
|
||||
assertEquals(new Loot.Table("everywhere"), pick(List.of(), pack, CHEST));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exactEntriesDoNotApplyToOtherStatesAndNothingMatchingGivesNothing() {
|
||||
List<IObjectLoot> pack = List.of(entry("this-chest", 1, true, CHEST), entry("other", 1, false, "minecraft:barrel"));
|
||||
List<ObjectLootEntry> pack = List.of(entry("this-chest", 1, true, CHEST), entry("other", 1, false, "minecraft:barrel"));
|
||||
|
||||
assertNull(pick(List.of(), pack, EAST_CHEST));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weightsPickAmongEqualsAndZeroWeightsAreIgnored() {
|
||||
List<IObjectLoot> pack = List.of(entry("everywhere", 3, false), entry("chests", 1, false), entry("other", 0, false));
|
||||
List<ObjectLootEntry> pack = List.of(entry("everywhere", 3, false), entry("chests", 1, false), entry("other", 0, false));
|
||||
int everywhere = 0;
|
||||
RNG rng = new RNG(5L);
|
||||
|
||||
for (int i = 0; i < 4000; i++) {
|
||||
Loot.Source picked = placement.pick(List.of(), pack, block(CHEST), rng, 0, 64, 0);
|
||||
Loot.Source picked = placement.pick(List.of(), pack, BlockState.parse(CHEST), rng, 0, 64, 0);
|
||||
if (picked.equals(new Loot.Table("everywhere"))) {
|
||||
everywhere++;
|
||||
} else {
|
||||
@@ -115,7 +113,7 @@ public class PlacementLootTest {
|
||||
|
||||
@Test
|
||||
public void aPlatformTableWithAnythingThePlatformHasIsASourceWithTheSeedOfItsIdentity() {
|
||||
List<IObjectLoot> platform = List.of(entry("chests/simple_dungeon", 1, false), entry("chests/missing", 1, false));
|
||||
List<ObjectLootEntry> platform = List.of(entry("chests/simple_dungeon", 1, false), entry("chests/missing", 1, false));
|
||||
|
||||
Loot.Source picked = pick(platform, List.of(), CHEST);
|
||||
|
||||
@@ -128,24 +126,13 @@ public class PlacementLootTest {
|
||||
assertNull(pick(List.of(entry("chests/missing", 1, false)), List.of(entry("not-a-table", 1, false)), CHEST));
|
||||
}
|
||||
|
||||
private Loot.Source pick(List<IObjectLoot> platform, List<IObjectLoot> pack, String container) {
|
||||
return placement.pick(platform, pack, block(container), new RNG(1L), 0, 64, 0);
|
||||
private Loot.Source pick(List<ObjectLootEntry> platform, List<ObjectLootEntry> pack, String container) {
|
||||
return placement.pick(platform, pack, BlockState.parse(container), new RNG(1L), 0, 64, 0);
|
||||
}
|
||||
|
||||
private static PlatformBlockState block(String key) {
|
||||
return UsableBlockPlatform.block(key);
|
||||
}
|
||||
|
||||
private static IObjectLoot entry(String name, int weight, boolean exact, String... filter) {
|
||||
IObjectLoot loot = mock(IObjectLoot.class);
|
||||
when(loot.getName()).thenReturn(name);
|
||||
when(loot.getWeight()).thenReturn(weight);
|
||||
when(loot.isExact()).thenReturn(exact);
|
||||
KList<PlatformBlockState> filterStates = new KList<>();
|
||||
for (String key : filter) {
|
||||
filterStates.add(block(key));
|
||||
}
|
||||
when(loot.getFilter(org.mockito.ArgumentMatchers.any())).thenReturn(filterStates);
|
||||
return loot;
|
||||
private static ObjectLootEntry entry(String name, int weight, boolean exact, String... filter) {
|
||||
String blocks = Arrays.stream(filter).map(key -> "{\"block\": \"" + key + "\"}").collect(Collectors.joining(", "));
|
||||
return TestSettings.of(ObjectLoot.class, "{\"name\": \"" + name + "\", \"weight\": " + weight + ", \"exact\": " + exact
|
||||
+ ", \"filter\": [" + blocks + "]}");
|
||||
}
|
||||
}
|
||||
@@ -20,13 +20,14 @@ package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.engine.loot.LootItems;
|
||||
import art.arcane.iris.builtin.spawn.EntityDefinition;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import art.arcane.iris.builtin.LiveWorld.Sky;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.google.gson.Gson;
|
||||
import com.volmit.entity.EntitySpec;
|
||||
import com.volmit.item.ItemRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -63,10 +64,10 @@ public class EntityResolutionPinTest {
|
||||
when(registry.maxDurability(anyString())).thenReturn(OptionalInt.of(0));
|
||||
when(registry.maxDurability("iron_helmet")).thenReturn(OptionalInt.of(165));
|
||||
when(registry.maxDurability("diamond_sword")).thenReturn(OptionalInt.of(1561));
|
||||
PlatformWorld world = mock(PlatformWorld.class);
|
||||
Optional<Sky> sky = Optional.of(new Sky(0L, false, false));
|
||||
EntityDefinition entity = new Gson().fromJson(ENTITY, EntityDefinition.class);
|
||||
|
||||
EntitySpec spec = new EntityResolution(new LootItems(registry, false), world, new EffectTimers()).resolve(entity, new RNG(STREAM_SEED));
|
||||
EntitySpec spec = new EntityResolution(new LootItems(registry, false), sky, new EffectTimers()).resolve(entity, new RNG(STREAM_SEED));
|
||||
|
||||
assertEquals(-819231818, spec.toString().hashCode());
|
||||
}
|
||||
|
||||
@@ -18,20 +18,20 @@
|
||||
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.builtin.LiveWorld;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.engine.framework.SeedManager;
|
||||
import art.arcane.iris.engine.mantle.EngineMantle;
|
||||
import art.arcane.iris.builtin.spawn.EntityDefinition;
|
||||
import art.arcane.iris.builtin.spawn.SpawnMarker;
|
||||
import art.arcane.iris.builtin.spawn.Spawner;
|
||||
import art.arcane.iris.engine.object.TestSettings;
|
||||
import art.arcane.iris.engine.object.runtime.IrisEngineData;
|
||||
import art.arcane.iris.engine.object.world.IrisWorld;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import com.volmit.entity.EntitySpec;
|
||||
import com.volmit.item.ItemRegistry;
|
||||
import com.volmit.service.EntitySpawns;
|
||||
@@ -80,6 +80,7 @@ public class EntitySpawningTest {
|
||||
IrisWorld engineWorld = mock(IrisWorld.class);
|
||||
when(engineWorld.platformWorld()).thenReturn(mock(PlatformWorld.class));
|
||||
when(engine.getWorld()).thenReturn(engineWorld);
|
||||
when(engine.getEngineData()).thenReturn(new IrisEngineData());
|
||||
SeedManager seeds = mock(SeedManager.class);
|
||||
when(seeds.getEntity()).thenReturn(11L);
|
||||
when(engine.getSeedManager()).thenReturn(seeds);
|
||||
@@ -88,11 +89,10 @@ public class EntitySpawningTest {
|
||||
|
||||
@Test
|
||||
public void aCaveWithoutFloorMarkersHasNowhereToSpawn() {
|
||||
EngineMantle mantle = mock(EngineMantle.class);
|
||||
when(engine.getMantle()).thenReturn(mantle);
|
||||
when(mantle.findMarkers(2, 3, art.arcane.volmlib.util.matter.slices.MarkerMatter.CAVE_FLOOR)).thenReturn(new KList<>());
|
||||
LiveWorld live = mock(LiveWorld.class);
|
||||
when(live.caveFloors(2, 3)).thenReturn(List.of());
|
||||
|
||||
assertNull(EntitySpawning.caveSpawnPosition(engine, 2, 3));
|
||||
assertNull(EntitySpawning.caveSpawnPosition(live, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.engine.spawn;
|
||||
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/** Rarity weights a pool exactly once: a spawner's spawns are picked from the expanded pool. */
|
||||
public class RarityWeightingTest {
|
||||
private record Spawn(int rarity) {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rarityIsAppliedOnceAsPoolWeightOnly() {
|
||||
Spawn common = new Spawn(1);
|
||||
Spawn rare = new Spawn(4);
|
||||
|
||||
KList<Spawn> expanded = RarityWeighting.expand(List.of(common, rare), Spawn::rarity);
|
||||
|
||||
// totalRarity 5 -> common appears 5/1 = 5 times, rare 5/4 = 1 time.
|
||||
assertEquals(6, expanded.size());
|
||||
assertEquals(5, expanded.stream().filter(entry -> entry == common).count());
|
||||
assertEquals(1, expanded.stream().filter(entry -> entry == rare).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rarityZeroAndNegativeCountAsOne() {
|
||||
Spawn zero = new Spawn(0);
|
||||
Spawn negative = new Spawn(-5);
|
||||
|
||||
KList<Spawn> expanded = RarityWeighting.expand(List.of(zero, negative, new Spawn(3)), Spawn::rarity);
|
||||
|
||||
// totalRarity 1 + 1 + 3 = 5 -> 5, 5 and 1 entries.
|
||||
assertEquals(11, expanded.size());
|
||||
assertEquals(5, expanded.stream().filter(entry -> entry == zero).count());
|
||||
assertEquals(5, expanded.stream().filter(entry -> entry == negative).count());
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.BuiltInModulesJar;
|
||||
import art.arcane.iris.core.project.ModuleResourceSchemas;
|
||||
import art.arcane.iris.core.project.SchemaTestPlatform;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
@@ -68,21 +69,21 @@ public class SpawnResourcesTest {
|
||||
Path pack = spawnPack("spawns");
|
||||
IrisData data = IrisData.openValidation(pack.toFile());
|
||||
try {
|
||||
Spawner spawner = SpawnResources.spawner(data, "camp/zombies");
|
||||
Spawner spawner = SpawnResources.spawner(data.getPackResources(), "camp/zombies");
|
||||
|
||||
assertEquals("zombie", spawner.spawns().getFirst().entity());
|
||||
assertEquals("minecraft:zombie", SpawnResources.entity(data, "zombie").type());
|
||||
assertEquals(List.of("camp/zombies"), SpawnResources.marker(data, "camp").spawners());
|
||||
assertEquals(List.of(new SpawnResources.KeyedSpawner("camp/zombies", spawner)), SpawnResources.spawners(data, List.of("camp/zombies", "missing")));
|
||||
assertEquals(List.of("zombie"), SpawnResources.entityKeys(data));
|
||||
assertEquals(List.of("camp"), SpawnResources.markerKeys(data));
|
||||
assertEquals(List.of("camp/zombies"), SpawnResources.spawnerKeys(data));
|
||||
assertNull(SpawnResources.spawner(data, "missing"));
|
||||
assertNull(SpawnResources.entity(data, " "));
|
||||
assertEquals("minecraft:zombie", SpawnResources.entity(data.getPackResources(), "zombie").type());
|
||||
assertEquals(List.of("camp/zombies"), SpawnResources.marker(data.getPackResources(), "camp").spawners());
|
||||
assertEquals(List.of(new SpawnResources.KeyedSpawner("camp/zombies", spawner)), SpawnResources.spawners(data.getPackResources(), List.of("camp/zombies", "missing")));
|
||||
assertEquals(List.of("zombie"), SpawnResources.entityKeys(data.getPackResources()));
|
||||
assertEquals(List.of("camp"), SpawnResources.markerKeys(data.getPackResources()));
|
||||
assertEquals(List.of("camp/zombies"), SpawnResources.spawnerKeys(data.getPackResources()));
|
||||
assertNull(SpawnResources.spawner(data.getPackResources(), "missing"));
|
||||
assertNull(SpawnResources.entity(data.getPackResources(), " "));
|
||||
|
||||
write(pack, "entities/zombie.json", "{\"type\":\"minecraft:husk\"}");
|
||||
data.dump();
|
||||
assertEquals("minecraft:husk", SpawnResources.entity(data, "zombie").type());
|
||||
assertEquals("minecraft:husk", SpawnResources.entity(data.getPackResources(), "zombie").type());
|
||||
} finally {
|
||||
data.close();
|
||||
}
|
||||
@@ -115,6 +116,7 @@ public class SpawnResourcesTest {
|
||||
write(pack.toPath(), "entities/zombie.json", "{\"type\":\"minecraft:zombie\"}");
|
||||
write(pack.toPath(), "spawners/camp/zombies.json", "{\"spawns\":[{\"entity\":\"zombie\"}]}");
|
||||
write(pack.toPath(), "markers/camp.json", "{\"spawners\":[\"camp/zombies\"]}");
|
||||
BuiltInModulesJar.writeInto(pack.toPath());
|
||||
return pack.toPath();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,13 +18,16 @@
|
||||
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.builtin.LiveWorld;
|
||||
import art.arcane.iris.builtin.LiveWorld.BiomeKind;
|
||||
import art.arcane.iris.builtin.LiveWorld.Sky;
|
||||
import art.arcane.iris.builtin.spawn.Spawner;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.engine.object.TestSettings;
|
||||
import art.arcane.iris.engine.object.runtime.IrisEngineData;
|
||||
import art.arcane.iris.engine.object.world.IrisWorld;
|
||||
import art.arcane.iris.engine.object.world.biome.InferredType;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -36,17 +39,19 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
/** Whether a spawner may fire: the biome kind its group wants, the world's time and weather, and its rates in the engine's data. */
|
||||
public class SpawnerGateTest {
|
||||
private Engine engine;
|
||||
private PlatformWorld world;
|
||||
private LiveWorld live;
|
||||
|
||||
@Before
|
||||
public void buildEngine() {
|
||||
engine = mock(Engine.class);
|
||||
Engine engine = mock(Engine.class);
|
||||
IrisWorld engineWorld = mock(IrisWorld.class);
|
||||
world = mock(PlatformWorld.class);
|
||||
when(engineWorld.platformWorld()).thenReturn(world);
|
||||
PlatformWorld platformWorld = mock(PlatformWorld.class);
|
||||
when(platformWorld.getTime()).thenReturn(6000L);
|
||||
when(engineWorld.platformWorld()).thenReturn(platformWorld);
|
||||
when(engine.getWorld()).thenReturn(engineWorld);
|
||||
when(engine.getData()).thenReturn(mock(IrisData.class));
|
||||
when(engine.getEngineData()).thenReturn(new IrisEngineData());
|
||||
live = LiveWorld.of(new EngineGeneration(engine).world());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,58 +61,54 @@ public class SpawnerGateTest {
|
||||
Spawner beach = spawner("{\"group\": \"BEACH\"}");
|
||||
Spawner cave = spawner("{\"group\": \"CAVE\"}");
|
||||
|
||||
assertTrue(SpawnerGate.suits(normal, biome(InferredType.LAND)));
|
||||
assertFalse(SpawnerGate.suits(normal, biome(InferredType.SEA)));
|
||||
assertTrue(SpawnerGate.suits(underwater, biome(InferredType.SEA)));
|
||||
assertFalse(SpawnerGate.suits(underwater, biome(InferredType.LAND)));
|
||||
assertTrue(SpawnerGate.suits(beach, biome(InferredType.SHORE)));
|
||||
assertFalse(SpawnerGate.suits(beach, biome(InferredType.CAVE)));
|
||||
assertTrue(SpawnerGate.suits(cave, biome(InferredType.SEA)));
|
||||
assertTrue(SpawnerGate.suits(normal, BiomeKind.LAND));
|
||||
assertFalse(SpawnerGate.suits(normal, BiomeKind.SEA));
|
||||
assertTrue(SpawnerGate.suits(underwater, BiomeKind.SEA));
|
||||
assertFalse(SpawnerGate.suits(underwater, BiomeKind.LAND));
|
||||
assertTrue(SpawnerGate.suits(beach, BiomeKind.SHORE));
|
||||
assertFalse(SpawnerGate.suits(beach, BiomeKind.CAVE));
|
||||
assertTrue(SpawnerGate.suits(cave, BiomeKind.SEA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeAndWeatherMustMatch() {
|
||||
Spawner daytimeRain = spawner("{\"timeBlock\": {\"startHour\": 8, \"endHour\": 18}, \"weather\": \"DOWNFALL\"}");
|
||||
when(world.getTime()).thenReturn(6000L);
|
||||
when(world.isStorming()).thenReturn(true);
|
||||
assertTrue(SpawnerGate.suits(daytimeRain, world));
|
||||
|
||||
when(world.isStorming()).thenReturn(false);
|
||||
assertFalse(SpawnerGate.suits(daytimeRain, world));
|
||||
|
||||
when(world.isStorming()).thenReturn(true);
|
||||
when(world.getTime()).thenReturn(20000L);
|
||||
assertFalse(SpawnerGate.suits(daytimeRain, world));
|
||||
assertTrue(SpawnerGate.suits(daytimeRain, new Sky(6000L, true, false)));
|
||||
assertFalse(SpawnerGate.suits(daytimeRain, new Sky(6000L, false, false)));
|
||||
assertFalse(SpawnerGate.suits(daytimeRain, new Sky(20000L, true, false)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aSpawnerWithoutRatesAlwaysMayFire() {
|
||||
Spawner free = spawner("{}");
|
||||
|
||||
SpawnerGate.spawned("free", free, engine, 3, 4);
|
||||
SpawnerGate.spawned("free", free, live, 3, 4);
|
||||
|
||||
assertTrue(SpawnerGate.canSpawn("free", free, engine, 3, 4));
|
||||
assertTrue(SpawnerGate.canSpawn("free", free, live, 3, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aRateHoldsTheSpawnerBackForItsIntervalPerWorldAndPerChunk() {
|
||||
Spawner limited = spawner("{\"maximumRate\": {\"amount\": 1, \"per\": {\"minutes\": 10}}, \"maximumRatePerChunk\": {\"amount\": 1, \"per\": {\"minutes\": 10}}}");
|
||||
|
||||
assertTrue(SpawnerGate.canSpawn("limited", limited, engine, 3, 4));
|
||||
SpawnerGate.spawned("limited", limited, engine, 3, 4);
|
||||
assertTrue(SpawnerGate.canSpawn("limited", limited, live, 3, 4));
|
||||
SpawnerGate.spawned("limited", limited, live, 3, 4);
|
||||
|
||||
assertFalse(SpawnerGate.canSpawn("limited", limited, engine, 3, 4));
|
||||
assertFalse(SpawnerGate.canSpawn("limited", limited, engine, 9, 9));
|
||||
assertTrue(SpawnerGate.canSpawn("other", limited, engine, 9, 9));
|
||||
assertFalse(SpawnerGate.canSpawn("limited", limited, live, 3, 4));
|
||||
assertFalse(SpawnerGate.canSpawn("limited", limited, live, 9, 9));
|
||||
assertTrue(SpawnerGate.canSpawn("other", limited, live, 9, 9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aWorldWithoutAPlatformWorldLetsNothingFire() {
|
||||
Engine engine = mock(Engine.class);
|
||||
when(engine.getData()).thenReturn(mock(IrisData.class));
|
||||
when(engine.getWorld()).thenReturn(mock(IrisWorld.class));
|
||||
|
||||
assertFalse(SpawnerGate.canSpawn("any", spawner("{}"), LiveWorld.of(new EngineGeneration(engine).world()), 0, 0));
|
||||
}
|
||||
|
||||
private static Spawner spawner(String json) {
|
||||
return TestSettings.of(Spawner.class, json);
|
||||
}
|
||||
|
||||
private static IrisBiome biome(InferredType type) {
|
||||
IrisBiome biome = mock(IrisBiome.class);
|
||||
when(biome.getInferredType()).thenReturn(type);
|
||||
return biome;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user