From 2c7b0cd1f7f4d0981eb5580ea7e18a8e7e348f68 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 08:20:59 +0200 Subject: [PATCH] Pin loot draws, loot reference resolution and entity resolution before the loot and spawn classes move --- .../iris/engine/loot/LootDrawPinTest.java | 83 +++++++++++ .../engine/loot/LootEnvironmentPinTest.java | 140 ++++++++++++++++++ .../engine/spawn/EntityResolutionPinTest.java | 73 +++++++++ 3 files changed, 296 insertions(+) create mode 100644 core/src/test/java/art/arcane/iris/engine/loot/LootDrawPinTest.java create mode 100644 core/src/test/java/art/arcane/iris/engine/loot/LootEnvironmentPinTest.java create mode 100644 core/src/test/java/art/arcane/iris/engine/spawn/EntityResolutionPinTest.java diff --git a/core/src/test/java/art/arcane/iris/engine/loot/LootDrawPinTest.java b/core/src/test/java/art/arcane/iris/engine/loot/LootDrawPinTest.java new file mode 100644 index 000000000..80acfa1f1 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/loot/LootDrawPinTest.java @@ -0,0 +1,83 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.engine.object.loot.IrisLootTable; +import com.google.gson.Gson; +import com.volmit.item.ItemDrop; +import com.volmit.item.ItemRegistry; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.OptionalInt; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * What a loot table gives at fixed spots of a fixed world, as the whole drawn items, hashed. The numbers were taken + * before the loot classes became settings of the loot module; a change in them changes the loot of existing worlds. + */ +public class LootDrawPinTest { + private static final long LOOT_SEED = 8123456789L; + private static final String TABLE = """ + {"name": "Pin table", "rarity": 1, "minPicked": 2, "maxPicked": 6, "maxTries": 20, "loot": [ + {"type": "diamond_sword", "rarity": 2, "minAmount": 1, "maxAmount": 3, "minDurability": 0.25, "maxDurability": 0.75, + "displayName": "Pin blade", "lore": ["one", "two"], "unbreakable": true, "itemFlags": ["HIDE_ENCHANTS"], + "enchantments": [{"enchantment": "sharpness", "minLevel": 1, "maxLevel": 5, "chance": 0.6}, + {"enchantment": "unbreaking", "minLevel": 2, "maxLevel": 3}], + "attributes": [{"attribute": "max_health", "name": "bonus", "minAmount": 2, "maxAmount": 6, "chance": 0.7}]}, + {"type": "diamond", "minAmount": 1, "maxAmount": 5}, + {"type": "bread", "rarity": 2, "minAmount": 2, "maxAmount": 8, "customModel": 4, "dyeColor": "red", "leatherColor": "#ff0000"}, + {"type": "stick", "slotTypes": "FUEL"}, + {"type": "unknown_item"} + ]} + """; + private static final int[][] SPOTS = {{0, 64, 0}, {10, 64, -30}, {-512, 12, 777}, {4096, 200, -4096}, {7, -50, 7}}; + + private ItemRegistry registry; + + @Before + public void knowTheItems() { + registry = mock(ItemRegistry.class); + when(registry.maxDurability(anyString())).thenReturn(OptionalInt.empty()); + when(registry.maxDurability("diamond_sword")).thenReturn(OptionalInt.of(1561)); + when(registry.maxDurability("diamond")).thenReturn(OptionalInt.of(0)); + when(registry.maxDurability("bread")).thenReturn(OptionalInt.of(0)); + when(registry.maxDurability("stick")).thenReturn(OptionalInt.of(0)); + } + + @Test + public void aTableGivesThePinnedItemsAtEachSpot() { + IrisLootTable table = new Gson().fromJson(TABLE, IrisLootTable.class); + LootItems items = new LootItems(registry, false); + + List hashes = new ArrayList<>(); + for (int[] spot : SPOTS) { + List drops = items.draw(table, LOOT_SEED, spot[0], spot[1], spot[2]); + hashes.add(drops.toString().hashCode()); + } + + assertEquals(List.of(-492887340, -92432338, -1096436977, -1436497629, -1714085578), hashes); + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/loot/LootEnvironmentPinTest.java b/core/src/test/java/art/arcane/iris/engine/loot/LootEnvironmentPinTest.java new file mode 100644 index 000000000..c88a3b7f7 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/loot/LootEnvironmentPinTest.java @@ -0,0 +1,140 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.core.loader.IrisData; +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.object.loot.IrisLootReference; +import art.arcane.iris.engine.object.loot.IrisLootTable; +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.math.RNG; +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Which loot tables a spot draws from, in which order and how often, for the loot references of its dimension, region and + * biomes, as written in pack JSON. Taken before the references became settings of the loot module. + */ +public class LootEnvironmentPinTest { + private static final int X = 10; + private static final int Z = 20; + private static final int SURFACE_Y = 70; + private static final int CAVE_Y = 40; + + private Engine engine; + private IrisDimension dimension; + private IrisRegion region; + private IrisBiome surface; + private IrisBiome cave; + + @Before + public void buildTheWorld() { + engine = mock(Engine.class, RETURNS_DEEP_STUBS); + IrisData data = mock(IrisData.class); + ModuleResourceFixture fixture = ModuleResourceFixture.serve(data); + for (String key : List.of("a", "b", "c", "d", "e")) { + fixture.resource(LootResources.TABLES, key, table(key)); + } + IrisComplex complex = engine.getComplex(); + when(complex.getData()).thenReturn(data); + dimension = mock(IrisDimension.class); + region = mock(IrisRegion.class); + 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(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); + } + + @Test + public void everyLevelAddsItsTablesInOrderAndTheMultipliersScaleTheCount() { + references("{\"tables\": [\"a\"]}", "{\"tables\": [\"b\", \"c\"], \"multiplier\": 2}", "{\"tables\": [\"d\"], \"multiplier\": 1.5}", "{}"); + + assertEquals(List.of("a", "b", "c", "d", "c", "d", "a", "b", "d", "c", "a", "b"), identities(false, SURFACE_Y)); + } + + @Test + public void fallbackReferencesStepAsideForTablesThatAnObjectDefined() { + references("{\"mode\": \"FALLBACK\", \"tables\": [\"a\"]}", "{\"tables\": [\"b\"]}", "{\"mode\": \"FALLBACK\", \"tables\": [\"c\"]}", "{}"); + + assertEquals(List.of("b"), identities(true, SURFACE_Y)); + assertEquals(List.of("a", "b", "c"), identities(false, SURFACE_Y)); + } + + @Test + public void replaceAndClearDropWhatCameBefore() { + references("{\"tables\": [\"a\"]}", "{\"mode\": \"REPLACE\", \"tables\": [\"b\"]}", "{\"tables\": [\"d\"]}", "{}"); + assertEquals(List.of("b", "d"), identities(false, SURFACE_Y)); + + references("{\"tables\": [\"a\"]}", "{\"tables\": [\"b\"]}", "{\"mode\": \"CLEAR\", \"tables\": [\"d\"]}", "{}"); + assertEquals(List.of(), identities(false, SURFACE_Y)); + } + + @Test + public void belowTheSurfaceTheCaveBiomeAddsItsTablesToo() { + references("{\"tables\": [\"a\"]}", "{}", "{\"tables\": [\"b\"]}", "{\"tables\": [\"e\"], \"multiplier\": 3}"); + + assertEquals(List.of("a", "b", "e", "e", "b", "a", "e", "a", "b"), identities(false, CAVE_Y)); + } + + private List identities(boolean objectLootDefined, int y) { + List sources = new ArrayList<>(); + LootTables.resolveEnvironmentSources(sources, engine, new RNG(19L), X, y, Z, objectLootDefined, LootTables::identity); + return sources; + } + + private void references(String dimensionJson, String regionJson, String surfaceJson, String caveJson) { + when(dimension.getLoot()).thenReturn(reference(dimensionJson)); + when(region.getLoot()).thenReturn(reference(regionJson)); + when(surface.getLoot()).thenReturn(reference(surfaceJson)); + when(cave.getLoot()).thenReturn(reference(caveJson)); + } + + private static IrisLootReference reference(String json) { + return new Gson().fromJson(json, IrisLootReference.class); + } + + private static IrisBiome biome(String key) { + IrisBiome biome = mock(IrisBiome.class); + when(biome.getLoadKey()).thenReturn(key); + return biome; + } + + private static IrisLootTable table(String key) { + IrisLootTable table = new IrisLootTable().setName(key); + table.setLoadKey(key); + return table; + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/spawn/EntityResolutionPinTest.java b/core/src/test/java/art/arcane/iris/engine/spawn/EntityResolutionPinTest.java new file mode 100644 index 000000000..fe872f7d2 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/spawn/EntityResolutionPinTest.java @@ -0,0 +1,73 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.spawn; + +import art.arcane.iris.engine.loot.LootItems; +import art.arcane.iris.engine.object.entity.IrisEntity; +import art.arcane.iris.spi.PlatformWorld; +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.OptionalInt; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * What an entity of the pack becomes when it is made from a fixed stream, hashed. The number was taken before the entity + * classes became settings of the spawn module; a change in it changes the equipment, attributes and passengers worlds spawn. + */ +public class EntityResolutionPinTest { + private static final long STREAM_SEED = 4321L; + private static final String ENTITY = """ + {"type": "minecraft:zombie", "customName": "&cPin", "aware": false, "glowing": true, "baby": true, "removable": true, + "helmet": {"type": "iron_helmet", "rarity": 2, "minDurability": 0.2, "maxDurability": 0.9, + "enchantments": [{"enchantment": "protection", "minLevel": 1, "maxLevel": 4}]}, + "mainHand": {"type": "diamond_sword", "minAmount": 1, "maxAmount": 1, + "attributes": [{"attribute": "attack_damage", "name": "pin", "minAmount": 1, "maxAmount": 9}]}, + "offHand": {"type": "bread", "minAmount": 1, "maxAmount": 6}, + "attributes": [{"attribute": "max_health", "name": "tough", "minAmount": 10, "maxAmount": 40, "chance": 0.8}, + {"attribute": "movement_speed", "name": "quick", "operation": "ADD_SCALAR", "minAmount": 0.1, "maxAmount": 0.3}], + "loot": {"tables": ["zombie/drops", "zombie/rare"]}, + "passengers": [{"type": "minecraft:chicken", "baby": true, "helmet": {"type": "bread"}}], + "leashHolder": {"type": "minecraft:bat", "attributes": [{"attribute": "max_health", "name": "tiny", "minAmount": 1, "maxAmount": 3}]}, + "spawnEffect": {"sound": "entity.zombie.ambient", "particleEffect": "SMOKE", "particleCount": 3, "interval": 0, "chance": 1}, + "spawnEffectRiseOutOfGround": true, + "rawCommands": [{"commands": ["say hello {x}"], "delay": 5, "repeat": true, "repeatDelay": 40}]} + """; + + @Test + public void anEntityResolvesToThePinnedSpec() { + ItemRegistry registry = mock(ItemRegistry.class); + 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); + IrisEntity entity = new Gson().fromJson(ENTITY, IrisEntity.class); + + EntitySpec spec = new EntityResolution(new LootItems(registry, false), world).resolve(entity, new RNG(STREAM_SEED)); + + assertEquals(1726532922, spec.toString().hashCode()); + } +}