Modded loot, drops, spawns, mobs and effects ask the modules through host-typed content services

This commit is contained in:
piexel committed 2026-10-10 13:00:03 +02:00
1 parent f404dec7d7
commit fbbc9ce4ac
24 files changed
+1178 -944

No files matched your search

@@ -0,0 +1,134 @@
/*
* 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.pack;
import art.arcane.iris.builtin.loot.LootTable;
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.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.volmlib.util.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/** An export carries the loot tables, spawners, entities and markers that the pack's settings and placements reference, and what those reference. */
public class ModuleResourceClosureTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void followsEveryLevelsSettingsAndThePlacementsThroughSpawnersEntitiesAndMarkers() throws Exception {
IrisData data = IrisData.openValidation(pack().toFile());
try {
ModuleResourceClosure closure = new ModuleResourceClosure(data);
IrisDimension dimension = data.getDimensionLoader().load("main");
IrisRegion region = data.getRegionLoader().load("region");
IrisBiome biome = data.getBiomeLoader().load("biome");
closure.followSettingsOf(dimension);
closure.followSettingsOf(region);
closure.followSettingsOf(biome);
closure.follow(biome.getObjects());
assertEquals(List.of("drops", "global", "regional"), closure.keysOf(LootTable.class));
assertEquals(List.of("camp/zombies", "cave", "marker/spawner"), closure.keysOf(Spawner.class));
assertEquals(List.of("beta/sentinel", "cow", "zombie"), closure.keysOf(EntityDefinition.class));
assertEquals(List.of("camp"), closure.keysOf(SpawnMarker.class));
} finally {
data.close();
}
}
@Test
public void whatIsNotReferencedIsNotExportedAndMissingKeysAreSkipped() throws Exception {
IrisData data = IrisData.openValidation(pack().toFile());
try {
ModuleResourceClosure closure = new ModuleResourceClosure(data);
closure.followSettingsOf(data.getRegionLoader().load("region"));
assertEquals(List.of("regional"), closure.keysOf(LootTable.class));
assertEquals(List.of(), closure.keysOf(Spawner.class));
assertFalse(closure.keysOf(LootTable.class).contains("unused"));
assertTrue(new ModuleResourceClosure(data).isEmpty());
} finally {
data.close();
}
}
@Test
public void writesEachResourceUnderTheFolderOfItsKind() throws Exception {
IrisData data = IrisData.openValidation(pack().toFile());
try {
ModuleResourceClosure closure = new ModuleResourceClosure(data);
closure.followSettingsOf(data.getDimensionLoader().load("main"));
File target = temporaryFolder.newFolder("export");
String hashes = closure.writeTo(target, true);
assertFalse(hashes.isEmpty());
assertEquals("Global", new JSONObject(Files.readString(target.toPath().resolve("loot/global.json"))).getString("name"));
assertTrue(Files.isRegularFile(target.toPath().resolve("spawners/camp/zombies.json")));
assertTrue(Files.isRegularFile(target.toPath().resolve("entities/zombie.json")));
assertFalse(Files.exists(target.toPath().resolve("loot/unused.json")));
} finally {
data.close();
}
}
private Path pack() throws Exception {
File pack = temporaryFolder.newFolder("pack");
write(pack, "dimensions/main.json", "{\"regions\":[\"region\"],\"loot\":{\"tables\":[\"global\"]},\"entitySpawners\":[\"camp/zombies\"]}");
write(pack, "regions/region.json", "{\"landBiomes\":[\"biome\"],\"loot\":{\"tables\":[\"regional\"]}}");
write(pack, "biomes/biome.json", "{\"name\":\"Biome\",\"entitySpawners\":[\"cave\"],\"blockDrops\":[{\"blocks\":[{\"block\":\"stone\"}],\"drops\":[{\"type\":\"gravel\"}]}],"
+ "\"objects\":[{\"place\":[\"hut\"],\"loot\":[{\"name\":\"drops\"}],\"markers\":[{\"marker\":\"camp\",\"mark\":[{\"block\":\"stone\"}]}]}]}");
write(pack, "loot/global.json", "{\"name\":\"Global\",\"loot\":[{\"type\":\"stick\"}]}");
write(pack, "loot/regional.json", "{\"name\":\"Regional\",\"loot\":[{\"type\":\"stick\"}]}");
write(pack, "loot/drops.json", "{\"name\":\"Drops\",\"loot\":[{\"type\":\"stick\"}]}");
write(pack, "loot/unused.json", "{\"name\":\"Unused\",\"loot\":[{\"type\":\"stick\"}]}");
write(pack, "spawners/camp/zombies.json", "{\"spawns\":[{\"entity\":\"zombie\"}],\"initialSpawns\":[{\"entity\":\"beta/sentinel\"}]}");
write(pack, "spawners/cave.json", "{\"spawns\":[{\"entity\":\"cow\"}]}");
write(pack, "spawners/marker/spawner.json", "{\"spawns\":[{\"entity\":\"cow\"}]}");
write(pack, "entities/zombie.json", "{\"type\":\"minecraft:zombie\",\"loot\":{\"tables\":[\"drops\"]}}");
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\"]}");
return pack.toPath();
}
private static void write(File root, String relative, String content) throws Exception {
Path path = root.toPath().resolve(relative);
Files.createDirectories(path.getParent());
Files.writeString(path, content, StandardCharsets.UTF_8);
}
}
@@ -59,9 +59,9 @@ public class PackExportClosureTest {
String modded = Files.readString(Path.of(
"../adapters/modded-common/src/main/java/art/arcane/iris/modded/command/ModdedStudioCommands.java")).replace("\r\n", "\n");
assertTrue("modded packager must write spawners/", modded.contains("\"spawners\""));
assertTrue("modded packager must write markers/", modded.contains("\"markers\""));
assertTrue("modded packager must include initial spawns", modded.contains("getInitialSpawns"));
assertTrue("modded packager must write the spawn and loot resources the settings reference",
modded.contains("moduleResources.writeTo(folder, true)"));
assertTrue("modded packager must export the placements' references", modded.contains("moduleResources.follow(region.getObjects())"));
assertTrue("modded packager must export region objects", modded.contains("region.getObjects()"));
int moddedValidation = modded.indexOf("PackValidator.validateForPackaging(packFolder)");
assertTrue("modded packager must validate before mutating package state",
@@ -0,0 +1,150 @@
/*
* 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.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.volmlib.util.math.RNG;
import com.volmit.service.Loot;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* The loot table an object placement gives a chest: a table naming the chest's exact state beats one naming its kind of
* block, which beats one naming no block; the weights pick among equals; platform tables count when the platform has them.
*/
public class PlacementLootTest {
private static final String CHEST = "minecraft:chest[facing=north]";
private static final String EAST_CHEST = "minecraft:chest[facing=east]";
private static final long LOOT_SEED = 99L;
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);
ModuleResourceFixture fixture = ModuleResourceFixture.serve(data);
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);
}
@Test
public void aTableThatNamesTheExactStateBeatsOneThatNamesTheKindOfBlock() {
List<IObjectLoot> 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));
}
@Test
public void aTableThatNamesNoBlockServesChestsNothingElseNames() {
List<IObjectLoot> 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"));
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));
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);
if (picked.equals(new Loot.Table("everywhere"))) {
everywhere++;
} else {
assertEquals(new Loot.Table("chests"), picked);
}
}
assertEquals(3000, everywhere, 150);
}
@Test
public void aPlatformTableWithAnythingThePlatformHasIsASourceWithTheSeedOfItsIdentity() {
List<IObjectLoot> platform = List.of(entry("chests/simple_dungeon", 1, false), entry("chests/missing", 1, false));
Loot.Source picked = pick(platform, List.of(), CHEST);
assertEquals(new Loot.PlatformTable("minecraft:chests/simple_dungeon",
LootTables.seed(LOOT_SEED, "Vanilla minecraft:chests/simple_dungeon", 0, 64, 0)), picked);
}
@Test
public void tablesTheWorldLacksAreSkipped() {
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 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;
}
}
@@ -0,0 +1,72 @@
/*
* 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.loot;
import art.arcane.iris.builtin.loot.BlockDrop;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.object.TestSettings;
import com.volmit.block.BlockRegistry;
import com.volmit.block.BlockSpec;
import com.volmit.block.BlockState;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** Which broken blocks a block drop applies to: the exact state it names, or any state of that block. */
public class ResolvedBlockDropTest {
private static final String NORTH_STAIRS = "minecraft:oak_stairs[facing=north]";
private static final String EAST_STAIRS = "minecraft:oak_stairs[facing=east]";
private static final String COBBLESTONE = "minecraft:cobblestone";
@BeforeClass
public static void bindPlatform() {
UsableBlockPlatform.bind();
}
@AfterClass
public static void unbindPlatform() {
UsableBlockPlatform.unbind();
}
@Test
public void exactMatchingIncludesPropertiesWhileTypeMatchingDoesNot() {
ResolvedBlockDrop exact = resolved(true);
ResolvedBlockDrop byType = resolved(false);
assertTrue(exact.dropsFor(NORTH_STAIRS));
assertFalse(exact.dropsFor(EAST_STAIRS));
assertTrue(byType.dropsFor(NORTH_STAIRS));
assertTrue(byType.dropsFor(EAST_STAIRS));
assertFalse(byType.dropsFor(COBBLESTONE));
assertFalse(exact.dropsFor(COBBLESTONE));
}
private static ResolvedBlockDrop resolved(boolean exactBlocks) {
BlockDrop drop = TestSettings.of(BlockDrop.class, "{\"exactBlocks\": " + exactBlocks + ", \"blocks\": [{\"block\": \"oak_stairs\"}]}");
BlockRegistry blocks = mock(BlockRegistry.class);
when(blocks.resolve(any(BlockSpec.class))).thenReturn(BlockState.parse(NORTH_STAIRS));
return ResolvedBlockDrop.of(drop, blocks);
}
}
@@ -0,0 +1,56 @@
/*
* 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.object.value;
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: the entity spawners of both platforms pick a spawn from the expanded pool. */
public class IRareWeightingTest {
private record Spawn(int rarity) implements IRare {
@Override
public int getRarity() {
return rarity;
}
}
@Test
public void rarityIsAppliedOnceAsPoolWeightOnly() {
Spawn common = new Spawn(1);
Spawn rare = new Spawn(4);
KList<Spawn> expanded = IRare.expandWeighted(List.of(common, rare));
// 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 rarityZeroAndNegativeAreClampedToOne() {
assertEquals(1, IRare.get(new Spawn(0)));
assertEquals(1, IRare.get(new Spawn(-5)));
assertEquals(3, IRare.get(new Spawn(3)));
}
}