Add value types for settings (BlockSpec, NoiseStyle, StyledRange, Range, BlockPosition, Object as any JSON), host-built noise through PackResources.noise(), and VanillaStructureHook for the platform's own structures

This commit is contained in:
piexel committed 2026-10-09 14:20:10 +02:00
1 parent ef4b640867
commit 84969c74bd
28 files changed
+1012 -39

No files matched your search

@@ -0,0 +1,116 @@
package com.volmit;
import com.volmit.block.BlockSpec;
import com.volmit.config.Range;
import com.volmit.noise.NoiseFactory;
import com.volmit.noise.NoiseSampler;
import com.volmit.noise.NoiseStyle;
import com.volmit.noise.StyledRange;
import com.volmit.random.WorldRandom;
import com.volmit.vanilla.VanillaStructureRule;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.volmit.vanilla.VanillaStructureRule.*;
import static org.junit.jupiter.api.Assertions.*;
/** The value types settings hold and the rules of vanilla structure hooks. */
class SettingsValuesTest {
@Test
void blockSpecsAreValuesWithTheDefaultsPacksGetWhenTheyLeaveOptionsOut() {
var spec = BlockSpec.of("iron_ore").withProperty("waterlogged", "true").withWeight(3)
.withBackup(BlockSpec.of("stone")).withTileData(Map.of("Delay", 20.0));
assertEquals("iron_ore", spec.block());
assertEquals(Map.of("waterlogged", "true"), spec.data());
assertEquals(3, spec.weight());
assertEquals(Optional.of(BlockSpec.of("stone")), spec.backup());
assertEquals(Map.of("Delay", 20.0), spec.tileData());
assertEquals(spec, BlockSpec.of("iron_ore").withProperty("waterlogged", "true").withWeight(3)
.withBackup(BlockSpec.of("stone")).withTileData(Map.of("Delay", 20.0)));
assertNotEquals(spec, spec.withWeight(1));
assertEquals(1, BlockSpec.of("stone").weight());
assertEquals(Optional.empty(), BlockSpec.of("stone").backup());
assertThrows(UnsupportedOperationException.class, () -> spec.data().clear());
assertThrows(UnsupportedOperationException.class, () -> spec.tileData().clear());
}
@Test
void noiseStylesNameTheHostsStyleAndAreFlatOnlyWithoutExpressionOrImageMap() {
var simplex = NoiseStyle.of("SIMPLEX").withZoom(0.08);
assertEquals("SIMPLEX", simplex.style());
assertEquals(0.08, simplex.zoom());
assertEquals(1, NoiseStyle.of("SIMPLEX").zoom());
assertEquals(simplex, NoiseStyle.of("SIMPLEX").withZoom(0.08));
assertEquals(simplex.hashCode(), NoiseStyle.of("SIMPLEX").withZoom(0.08).hashCode());
assertNotEquals(simplex, NoiseStyle.of("SIMPLEX"));
assertTrue(NoiseStyle.of(NoiseStyle.FLAT).isFlat());
assertFalse(simplex.isFlat());
assertEquals(Optional.empty(), simplex.expression());
assertEquals(Optional.empty(), simplex.fracture());
}
@Test
void styledRangesPickBetweenTheirBoundsWithTheStylesNoise() {
var asked = new ArrayList<String>();
NoiseFactory noise = (style, seed) -> {
asked.add(style.style() + "@" + seed);
return fixed(0.25);
};
var styled = StyledRange.of(10, 30, NoiseStyle.of("SIMPLEX"));
assertEquals(15, styled.at(noise, 7, 100, 200));
assertEquals(List.of("SIMPLEX@7"), asked);
assertEquals(20, StyledRange.of(10, 30, NoiseStyle.of(NoiseStyle.FLAT)).at(noise, 7, 0, 0), "flat: halfway");
assertEquals(10, StyledRange.of(10, 10, NoiseStyle.of("SIMPLEX")).at(noise, 7, 0, 0), "equal bounds");
assertEquals(1, asked.size(), "neither flat nor equal bounds build noise");
assertTrue(StyledRange.of(10, 10, NoiseStyle.of("SIMPLEX")).isFlat());
assertFalse(styled.isFlat());
}
@Test
void rangesIncludeBothBoundsAndPickWithOneDraw() {
var range = Range.of(2, 6);
assertTrue(range.contains(2));
assertTrue(range.contains(6));
assertFalse(range.contains(6.5));
var picks = new WorldRandom(42);
var expected = new WorldRandom(42);
assertEquals(2 + expected.nextDouble() * 4, range.pick(picks));
assertEquals(expected.nextLong(), picks.nextLong(), "one draw per pick");
assertEquals(3, Range.of(3, 3).pick(picks));
assertEquals(expected.nextLong(), picks.nextLong(), "no draw when the bounds are equal");
}
@Test
void vanillaStructureRulesCombineBlockingAndPlacingWhateverTheOrder() {
var expected = Map.of(
List.of(GENERATE, GENERATE), GENERATE,
List.of(GENERATE, ALSO_PLACE), ALSO_PLACE,
List.of(GENERATE, BLOCK), BLOCK,
List.of(ALSO_PLACE, BLOCK), REPLACE,
List.of(GENERATE, REPLACE), REPLACE,
List.of(BLOCK, BLOCK), BLOCK);
expected.forEach((rules, combined) -> {
assertEquals(combined, rules.get(0).and(rules.get(1)), rules.toString());
assertEquals(combined, rules.get(1).and(rules.get(0)), rules.toString());
});
for (VanillaStructureRule rule : VanillaStructureRule.values()) {
assertEquals(rule, rule.and(GENERATE), "GENERATE changes nothing");
}
assertTrue(REPLACE.blocksPlatform() && REPLACE.placedByModule());
assertTrue(BLOCK.blocksPlatform() && !BLOCK.placedByModule());
assertTrue(!ALSO_PLACE.blocksPlatform() && ALSO_PLACE.placedByModule());
}
private static NoiseSampler fixed(double value) {
return new NoiseSampler() {
public double sample(double worldX, double worldZ) { return value; }
public double sample(double worldX, double y, double worldZ) { return value; }
};
}
}
@@ -5,7 +5,7 @@ import java.util.List;
/** Stands for a host class such as Iris's material palette: the host describes it in studio itself. */
public final class Palette {
private List<String> blocks = List.of("minecraft:stone");
private Object anything;
private java.util.UUID anything;
public List<String> blocks() {
return blocks;
@@ -5,6 +5,8 @@ import com.volmit.config.Feature;
import com.volmit.mantle.*;
import com.volmit.resource.ResourceKind;
import com.volmit.validation.Validator;
import com.volmit.vanilla.VanillaStructureHook;
import com.volmit.vanilla.VanillaStructureRule;
import com.volmit.stage.PlacementStage;
import com.volmit.stage.Stage;
import com.volmit.stage.StageGraph;
@@ -248,6 +250,25 @@ class ModuleRegistryTest {
assertThrows(IllegalStateException.class, () -> registry.registerValidator(pack -> {}), "registration has ended");
}
@Test
void vanillaStructureHooksAreCataloguedByModuleIdInTheOrderEachModuleRegisteredThem() {
var registry = new ModuleRegistry();
VanillaStructureHook first = (structure, world) -> VanillaStructureRule.REPLACE;
VanillaStructureHook second = (structure, world) -> VanillaStructureRule.GENERATE;
VanillaStructureHook other = (structure, world) -> VanillaStructureRule.BLOCK;
registerAs(registry, "test:zeta", hooks -> hooks.registerVanillaStructureHook(other));
registerAs(registry, "test:alpha", hooks -> {
hooks.registerVanillaStructureHook(first);
hooks.registerVanillaStructureHook(second);
});
assertThrows(IllegalStateException.class, () -> registry.registerVanillaStructureHook(first), "outside register");
var hooks = registry.freeze().vanillaStructureHooks();
assertEquals(List.of(first, second, other), hooks.stream().map(VanillaStructureHookRegistration::hook).toList());
assertEquals("test:alpha", hooks.getFirst().module().info().id().toString());
assertThrows(IllegalStateException.class, () -> registry.registerVanillaStructureHook(first), "registration has ended");
}
private static MantleCodec<String> codec(int version) {
return new MantleCodec<>() {
public int version() { return version; }
@@ -14,6 +14,7 @@ import com.volmit.host.schema.PackSchema;
import com.volmit.host.schema.ResourceKindSchema;
import com.volmit.host.schema.SchemaOption;
import com.volmit.host.schema.TextFormat;
import com.volmit.host.schema.ValueKind.AnyKind;
import com.volmit.host.schema.ValueKind.BooleanKind;
import com.volmit.host.schema.ValueKind.DecimalKind;
import com.volmit.host.schema.ValueKind.EnumConstant;
@@ -416,6 +417,66 @@ class PackSchemaTest {
assertEquals(expected, loadFailure(body, otherSources).getMessage());
}
@Test
void settingsMayHoldTheApisValueTypesWhichAreDescribedLikeSettings() {
var pack = pack("values");
module("pack.flora.FloraModule", FLORA,
"public void register(com.volmit.FeatureRegistry registry) { registry.registerSettings(FloraSettings.class); }", NO_STAGES)
.source("pack.flora.FloraSettings", """
package pack.flora;
import com.volmit.FeatureAttachment.Target;
import com.volmit.IrisFeature;
import com.volmit.block.BlockSpec;
import com.volmit.config.Feature;
import com.volmit.config.Range;
import com.volmit.noise.NoiseStyle;
import com.volmit.noise.StyledRange;
import com.volmit.world.BlockPosition;
import java.util.*;
@Feature(id = "example:flora", name = "Flora", targets = Target.BIOME)
public final class FloraSettings implements IrisFeature {
private List<BlockSpec> palette = List.of(BlockSpec.of("poppy"));
private NoiseStyle style = NoiseStyle.of("SIMPLEX").withZoom(4);
private StyledRange height = StyledRange.of(2, 5, NoiseStyle.of("SIMPLEX"));
private Range light = Range.of(0, 15);
private BlockPosition offset = new BlockPosition(1, 2, 3);
private Object extra;
}
""")
.writeInto(pack, "flora.jar");
var schema = schemaOf(pack, NO_BUILT_INS);
var settings = schema.type("pack.flora.FloraSettings");
assertEquals(new ListKind(new ObjectKind("com.volmit.block.BlockSpec"), false), option(settings, "palette").kind());
assertEquals(Optional.of(List.of(Map.of("block", "poppy", "data", Map.of(), "tileData", Map.of(), "weight", 1L))),
option(settings, "palette").defaultValue());
assertEquals(new ObjectKind("com.volmit.noise.NoiseStyle"), option(settings, "style").kind());
assertEquals(new ObjectKind("com.volmit.noise.StyledRange"), option(settings, "height").kind());
assertEquals(new ObjectKind("com.volmit.config.Range"), option(settings, "light").kind());
assertEquals(Optional.of(Map.of("max", 15.0, "min", 0.0)), option(settings, "light").defaultValue());
assertEquals(new ObjectKind("com.volmit.world.BlockPosition"), option(settings, "offset").kind());
assertEquals(Optional.of(Map.of("x", 1L, "y", 2L, "z", 3L)), option(settings, "offset").defaultValue());
assertEquals(new AnyKind(), option(settings, "extra").kind(), "Object holds any JSON value");
var block = schema.type("com.volmit.block.BlockSpec");
assertEquals(List.of("block", "data", "weight", "backup", "tileData"), block.options().stream().map(SchemaOption::name).toList());
assertEquals(new ReferenceKind(Reference.BLOCK, TextFormat.PLAIN), option(block, "block").kind());
assertEquals(new MapKind(new TextKind(TextFormat.PLAIN)), option(block, "data").kind());
assertEquals(new MapKind(new AnyKind()), option(block, "tileData").kind());
assertEquals(new ObjectKind("com.volmit.block.BlockSpec"), option(block, "backup").kind());
assertEquals(Optional.of("air"), option(block, "block").defaultValue());
var style = schema.type("com.volmit.noise.NoiseStyle");
assertEquals(Optional.of("style"), style.snippet(), "API classes keep the snippet folders packs already use");
assertEquals(new ReferenceKind(Reference.NOISE_STYLE, TextFormat.PLAIN), option(style, "style").kind());
assertEquals(new ReferenceKind(Reference.PACK_EXPRESSION, TextFormat.PLAIN), option(style, "expression").kind());
assertEquals(new ReferenceKind(Reference.PACK_IMAGE_MAP, TextFormat.PLAIN), option(style, "imageMap").kind());
assertEquals(Optional.of("FLAT"), option(style, "style").defaultValue());
assertEquals(List.of("range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
}
@Test
void defaultsLeaveOutWhatJsonCannotHold() {
var pack = pack("defaults");