Golden procedural objects also on a game-like platform: properties, defaults, missing blocks

This commit is contained in:
piexel committed 2026-10-15 11:41:55 +02:00
1 parent 14b50729e7
commit ad85b7e729
3 files changed
+182 -12

No files matched your search

@@ -25,12 +25,17 @@ public final class UsableBlockPlatform {
}
public static void bind() {
IrisPlatforms.unbind();
forgetCachedRegistries();
PlatformRegistries registries = mock(PlatformRegistries.class);
Map<String, PlatformBlockState> states = new HashMap<>();
when(registries.block(anyString())).thenAnswer(invocation -> states.computeIfAbsent(
invocation.<String>getArgument(0).toLowerCase(Locale.ROOT), UsableBlockPlatform::block));
bind(registries);
}
/** Binds a platform with {@code registries}, for a test that needs blocks to behave more like the game's. */
public static void bind(PlatformRegistries registries) {
IrisPlatforms.unbind();
forgetCachedRegistries();
IrisPlatform platform = mock(IrisPlatform.class);
when(platform.registries()).thenReturn(registries);
IrisPlatforms.bind(platform);
@@ -0,0 +1,133 @@
/*
* 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.procedural;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.spi.PlatformRegistries;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
/**
* Registries whose blocks carry properties the way the game's do: a key gets Minecraft's namespace, logs and stems an
* axis, leaves their distance, persistence and waterlogging, properties are listed by name, and setting a property
* gives the state with it. A key naming {@code minecraft:missing_*} is a block the platform lacks.
*/
final class GameLikeBlocks {
private static final String NAMESPACE = "minecraft:";
private static final String MISSING = NAMESPACE + "missing_";
private final Map<String, PlatformBlockState> states = new ConcurrentHashMap<>();
private final PlatformRegistries registries = mock(PlatformRegistries.class, withSettings().stubOnly());
GameLikeBlocks() {
when(registries.block(anyString())).thenAnswer(invocation -> {
PlatformBlockState state = orNull(invocation.getArgument(0));
return state == null ? state("minecraft:air") : state;
});
when(registries.blockOrNull(anyString())).thenAnswer(invocation -> orNull(invocation.getArgument(0)));
when(registries.blockOrNull(anyString(), anyBoolean())).thenAnswer(invocation -> orNull(invocation.getArgument(0)));
when(registries.air()).thenAnswer(invocation -> state("minecraft:air"));
}
PlatformRegistries registries() {
return registries;
}
private PlatformBlockState orNull(String key) {
String canonical = canonical(key);
return canonical.startsWith(MISSING) ? null : state(canonical);
}
private PlatformBlockState state(String canonical) {
return states.computeIfAbsent(canonical, this::create);
}
private PlatformBlockState create(String key) {
PlatformBlockState state = mock(PlatformBlockState.class, withSettings().stubOnly());
String material = material(key);
when(state.key()).thenReturn(key);
when(state.materialKey()).thenReturn(material);
when(state.isAir()).thenReturn(material.endsWith("air"));
when(state.withProperty(anyString(), anyString())).thenAnswer(invocation -> {
Map<String, String> properties = properties(key);
properties.put(invocation.getArgument(0), invocation.getArgument(1));
return state(write(material, properties));
});
return state;
}
private static String canonical(String key) {
String lower = key.toLowerCase(java.util.Locale.ROOT);
String namespaced = lower.contains(":") ? lower : NAMESPACE + lower;
String material = material(namespaced);
Map<String, String> properties = defaults(material);
properties.putAll(properties(namespaced));
return write(material, properties);
}
private static Map<String, String> defaults(String material) {
Map<String, String> properties = new TreeMap<>();
if (material.endsWith("_leaves")) {
properties.put("distance", "7");
properties.put("persistent", "false");
properties.put("waterlogged", "false");
} else if (material.endsWith("_log") || material.endsWith("_wood") || material.endsWith("_stem")
|| material.endsWith("_hyphae")) {
properties.put("axis", "y");
}
return properties;
}
private static String material(String key) {
int bracket = key.indexOf('[');
return bracket < 0 ? key : key.substring(0, bracket);
}
private static Map<String, String> properties(String key) {
Map<String, String> properties = new TreeMap<>();
int bracket = key.indexOf('[');
if (bracket < 0 || !key.endsWith("]")) {
return properties;
}
for (String property : key.substring(bracket + 1, key.length() - 1).split(",")) {
int equals = property.indexOf('=');
if (equals > 0) {
properties.put(property.substring(0, equals), property.substring(equals + 1));
}
}
return properties;
}
private static String write(String material, Map<String, String> properties) {
if (properties.isEmpty()) {
return material;
}
return material + properties.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(",", "[", "]"));
}
}
@@ -40,7 +40,6 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -75,7 +74,8 @@ import static org.mockito.Mockito.when;
*/
public class ProceduralObjectsGoldenTest {
private static final String FIXTURE = "/procedural-golden";
private static final Map<String, String> EXPECTED = Map.ofEntries(
/** Digests on a platform whose blocks have no properties and which knows no block by {@code blockOrNull}. */
private static final Map<String, String> BLOCK_ONLY = Map.ofEntries(
Map.entry("biomes-carving-frost-shards", "954613b558bd0e8b"),
Map.entry("biomes-estranged-baobab-grove", "3232037f828a9107"),
Map.entry("biomes-estranged-birchglass-heath", "a55859aa6a343b2a"),
@@ -101,22 +101,55 @@ public class ProceduralObjectsGoldenTest {
Map.entry("biomes-temperate-sea-ocean", "ebd0960ae22f4fc6"),
Map.entry("regions-estranged", "a806532a68e8c232"),
Map.entry("regions-mushroom", "f08c6663543ac201"));
/** Digests on {@link GameLikeBlocks}. */
private static final Map<String, String> GAME_LIKE = Map.ofEntries(
Map.entry("biomes-carving-frost-shards", "b425550ade26b810"),
Map.entry("biomes-estranged-baobab-grove", "ba75886b7bc06be0"),
Map.entry("biomes-estranged-birchglass-heath", "cb04761bb5074cf2"),
Map.entry("biomes-estranged-bloomspire-orchard", "27dca6f700d2c683"),
Map.entry("biomes-estranged-bonsai", "9d9a3eb1125a25fa"),
Map.entry("biomes-estranged-candlewood-grove", "6c085c37f8993922"),
Map.entry("biomes-estranged-emberbark-woods", "a8636178fffd89f2"),
Map.entry("biomes-estranged-mistveil-swamp", "03fc65a53bf531d9"),
Map.entry("biomes-estranged-pallid-necropolis", "0aeafb5af5224fd8"),
Map.entry("biomes-estranged-skyreach-pines", "facf4e1de1615c66"),
Map.entry("biomes-estranged-slinky-spires", "a8d019e9c4da8b14"),
Map.entry("biomes-estranged-snarlwood-thicket", "e5ce78962c3853d0"),
Map.entry("biomes-estranged-spindlewood", "71cca12bfe23e18e"),
Map.entry("biomes-estranged-stiltroot-shallows", "21d67797a8e38e9d"),
Map.entry("biomes-estranged-taxodium", "2f8f7b47d78102c9"),
Map.entry("biomes-estranged-weeping-mire", "f871b1643435a2f7"),
Map.entry("biomes-frozen-fields-hilly-plains", "6ab2b00dd640de13"),
Map.entry("biomes-frozen-ice-spikes", "732b09dfd0799a23"),
Map.entry("biomes-frozen-sea-frozen-parent-river", "a2fbf4ff15ad75aa"),
Map.entry("biomes-magnetics-glass", "6dfa6bacedf24a5f"),
Map.entry("biomes-magnetics-glass-shard", "21f08d7a93fd78c2"),
Map.entry("biomes-ocean-warm", "3df57be11fa3d53c"),
Map.entry("biomes-temperate-sea-ocean", "4e2269f4f39cff87"),
Map.entry("regions-estranged", "61d9ed40fed38854"),
Map.entry("regions-mushroom", "ccf240de087fac38"));
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Before
public void bindPlatform() {
UsableBlockPlatform.bind();
}
@After
public void unbindPlatform() {
UsableBlockPlatform.unbind();
}
@Test
public void everyProceduralObjectOfTheOverworldPackBakesAsBefore() throws Exception {
public void onABlockOnlyPlatformEveryProceduralObjectOfTheOverworldPackBakesAsBefore() throws Exception {
UsableBlockPlatform.bind();
assertEquals(new TreeMap<>(BLOCK_ONLY), bakeAll());
}
@Test
public void onAGameLikePlatformEveryProceduralObjectOfTheOverworldPackBakesAsBefore() throws Exception {
UsableBlockPlatform.bind(new GameLikeBlocks().registries());
assertEquals(new TreeMap<>(GAME_LIKE), bakeAll());
}
private Map<String, String> bakeAll() throws Exception {
copyFixtureInto(folder.getRoot().toPath());
IrisData data = mock(IrisData.class, withSettings().stubOnly().defaultAnswer(CALLS_REAL_METHODS));
IrisPackResources resources = mock(IrisPackResources.class, withSettings().stubOnly());
@@ -131,8 +164,7 @@ public class ProceduralObjectsGoldenTest {
ProceduralObjectSettings settings = gson.fromJson(sets.get(name), ProceduralObjectSettings.class);
actual.put(name, bake(settings, data));
}
assertEquals(new TreeMap<>(EXPECTED), actual);
return actual;
}
private static String bake(ProceduralObjectSettings settings, IrisData data) throws NoSuchAlgorithmException {