Require object feature ids, mantle keys and settings ids to be the owning module's id or below it

Object feature ids and mantle keys a module registers must now equal its
module id or start with it and '/', the rule stage ids already follow,
instead of only sharing its namespace. A settings type named in when(...)
needs a feature id that at least one module naming it owns in the same
sense, so another module may still condition on it, as moss can on
iris:snow. ModuleInfo.ownsId states the rule; stageIdPrefix becomes
idPrefix since it now prefixes more than stages. Errors name the module
and its origin and suggest an owned id.
This commit is contained in:
piexel committed 2026-10-05 22:15:59 +02:00
1 parent 77e4ab4710
commit d5aad57ad9
10 files changed
+113 -61

No files matched your search

+9 -2
View File
@@ -69,19 +69,26 @@ class ApiValuesTest {
}
@Test
void moduleIdsAreOneSegmentAndOwnTheStageIdsBelowThem() {
void moduleIdsAreOneSegmentAndOwnTheIdsBelowThem() {
var nested = assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("iris:stilt/extra", "Extra", "1"));
assertEquals("Module id iris:stilt/extra must not contain '/': stage ids are a module id, '/' and a name, "
+ "so iris:stilt/extra would name stages under another module's id", nested.getMessage());
var stilt = ModuleInfo.of("iris:stilt", "Stilt", "1");
assertEquals("iris:stilt/", stilt.stageIdPrefix());
assertEquals("iris:stilt/", stilt.idPrefix());
assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill")));
assertTrue(stilt.ownsStage(ResourceId.parse("iris:stilt/fill/deep")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilt")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:stilts/fill")));
assertFalse(stilt.ownsStage(ResourceId.parse("iris:anchor/height")));
assertFalse(stilt.ownsStage(ResourceId.parse("other:stilt/fill")));
assertTrue(stilt.ownsId(ResourceId.parse("iris:stilt")));
assertTrue(stilt.ownsId(ResourceId.parse("iris:stilt/depth")));
assertFalse(stilt.ownsId(ResourceId.parse("iris:stilts")));
assertFalse(stilt.ownsId(ResourceId.parse("iris:stilt-depth")));
assertFalse(stilt.ownsId(ResourceId.parse("iris:anchor")));
assertFalse(stilt.ownsId(ResourceId.parse("other:stilt")));
}
@Test
@@ -16,16 +16,16 @@ import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.*;
class ModuleRegistryTest {
@Feature(id = "test:one", name = "One", field = "shared", targets = FeatureAttachment.Target.BIOME)
@Feature(id = "test:core/one", name = "One", field = "shared", targets = FeatureAttachment.Target.BIOME)
static class One implements IrisFeature {}
@Feature(id = "test:two", name = "Two", field = "shared", targets = FeatureAttachment.Target.DIMENSION)
@Feature(id = "test:core/two", name = "Two", field = "shared", targets = FeatureAttachment.Target.DIMENSION)
static class Two implements IrisFeature {}
@Feature(id = "test:collision", name = "Collision", field = "shared", targets = {FeatureAttachment.Target.DIMENSION, FeatureAttachment.Target.BIOME})
@Feature(id = "test:core/collision", name = "Collision", field = "shared", targets = {FeatureAttachment.Target.DIMENSION, FeatureAttachment.Target.BIOME})
static class Collision implements IrisFeature {}
@Feature(id = "test:one", name = "Duplicate", field = "different", targets = FeatureAttachment.Target.REGION)
@Feature(id = "test:core/one", name = "Duplicate", field = "different", targets = FeatureAttachment.Target.REGION)
static class Duplicate implements IrisFeature {}
static class MissingAnnotation implements IrisFeature {}
@Feature(id = "test:abstract", name = "Abstract", field = "abstract", targets = FeatureAttachment.Target.BIOME)
@Feature(id = "test:core/abstract", name = "Abstract", field = "abstract", targets = FeatureAttachment.Target.BIOME)
abstract static class AbstractFeature implements IrisFeature {}
/** Runs {@code registrations} as the register(...) of a built-in module with the given id. */
@@ -42,15 +42,15 @@ class ModuleRegistryTest {
var registry = new ModuleRegistry();
registry.registerSettings(One.class);
registry.registerSettings(One.class);
registerAs(registry, "test:objects", objects -> {
registerAs(registry, "test:core", objects -> {
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Collision.class, ignored -> context -> null));
objects.registerObject(Two.class, ignored -> context -> null);
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(Duplicate.class, ignored -> context -> null));
assertThrows(IllegalArgumentException.class, () -> objects.registerObject(One.class, ignored -> context -> null));
});
var catalog = registry.freeze();
assertEquals(List.of("test:one"), catalog.settings().stream().map(value -> value.id().toString()).toList());
assertEquals(List.of("test:two"), catalog.objects().stream().map(value -> value.descriptor().id().toString()).toList());
assertEquals(List.of("test:core/one"), catalog.settings().stream().map(value -> value.id().toString()).toList());
assertEquals(List.of("test:core/two"), catalog.objects().stream().map(value -> value.descriptor().id().toString()).toList());
}
@Test
@@ -59,7 +59,7 @@ class ModuleRegistryTest {
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(One.class));
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(MissingAnnotation.class));
assertThrows(IllegalArgumentException.class, () -> registry.registerSettings(AbstractFeature.class));
registerAs(registry, "test:objects", objects -> {
registerAs(registry, "test:core", objects -> {
assertThrows(NullPointerException.class, () -> objects.registerObject(Two.class, null));
objects.registerObject(Two.class, ignored -> context -> null);
});
@@ -70,29 +70,34 @@ class ModuleRegistryTest {
@Test
void mantleIdsAreUniqueAndCodecVersionIsSnapshotted() {
var registry = new ModuleRegistry();
var key = new MantleKey<>("test:layer", String.class);
var key = new MantleKey<>("test:core/layer", String.class);
var codec = codec(3);
registerAs(registry, "test:layers", layers -> {
registerAs(registry, "test:core", layers -> {
layers.registerMantle(key, MantleLifetime.GENERATION, codec);
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(key, MantleLifetime.PERSISTENT, codec));
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>("test:bad", String.class), MantleLifetime.GENERATION, codec(-1)));
assertThrows(IllegalArgumentException.class, () -> layers.registerMantle(new MantleKey<>("test:core/bad", String.class), MantleLifetime.GENERATION, codec(-1)));
});
assertEquals(3, registry.freeze().mantle().getFirst().version());
assertEquals(1, registry.freeze().mantle().size());
}
@Test
void objectFeatureIdsAndMantleKeysUseTheRegisteringModulesNamespace() {
void objectFeatureIdsAndMantleKeysAreTheRegisteringModulesIdOrBelowIt() {
var registry = new ModuleRegistry();
var object = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:objects",
objects -> objects.registerObject(Two.class, ignored -> context -> null)));
assertEquals("Object generator settings " + Two.class.getName() + " with feature id test:two must use namespace "
+ "'other' of its registering module other:objects from host classpath", object.getMessage());
assertEquals("Object generator settings " + Two.class.getName() + " with feature id test:core/two must be "
+ "other:objects or start with other:objects/, the id of its registering module other:objects from host classpath",
object.getMessage());
var mantle = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "other:layers",
layers -> layers.registerMantle(new MantleKey<>("iris:depth", String.class), MantleLifetime.GENERATION, codec(1))));
assertEquals("Mantle layer iris:depth must use namespace 'other' of its registering module other:layers from host classpath",
mantle.getMessage());
var sameNamespace = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "test:other",
objects -> objects.registerObject(Two.class, ignored -> context -> null)));
assertTrue(sameNamespace.getMessage().contains("must be test:other or start with test:other/"), sameNamespace.getMessage());
var mantle = assertThrows(IllegalArgumentException.class, () -> registerAs(registry, "iris:snow",
layers -> layers.registerMantle(new MantleKey<>("iris:snowdepth", String.class), MantleLifetime.GENERATION, codec(1))));
assertEquals("Mantle layer iris:snowdepth must be iris:snow or start with iris:snow/, the id of its registering module "
+ "iris:snow from host classpath", mantle.getMessage());
assertThrows(IllegalStateException.class, () -> registry.registerMantle(new MantleKey<>("test:loose", String.class),
MantleLifetime.GENERATION, codec(1)));
@@ -100,16 +105,28 @@ class ModuleRegistryTest {
assertTrue(registry.freeze().mantle().isEmpty());
}
@Test
void moduleMayRegisterItsOwnIdAndIdsBelowIt() {
var registry = new ModuleRegistry();
registerAs(registry, "iris:snow", layers -> {
layers.registerMantle(new MantleKey<>("iris:snow", String.class), MantleLifetime.GENERATION, codec(1));
layers.registerMantle(new MantleKey<>("iris:snow/depth/deep", String.class), MantleLifetime.GENERATION, codec(1));
});
assertEquals(List.of("iris:snow", "iris:snow/depth/deep"),
registry.freeze().mantle().stream().map(layer -> layer.key().id()).sorted().toList());
}
@Test
void loadingModulesCatalogsStageSettingsAndRegistrationsTogether() {
var conditioned = new IrisModule() {
static final PlacementStage<Void> WORK = Stage.placement("test:conditioned/work");
public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "Conditioned", "1"); }
static final PlacementStage<Void> WORK = Stage.placement("test:core/work");
public ModuleInfo info() { return ModuleInfo.of("test:core", "Conditioned", "1"); }
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); }
};
var loaded = LoadedModules.load(List.of(DiscoveredModule.builtIn(conditioned)), Map.of(), Set.of());
assertEquals(List.of("test:one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
assertEquals(List.of("test:core/one"), loaded.catalog().settings().stream().map(value -> value.id().toString()).toList());
assertEquals(1, loaded.catalog().objects().size());
assertEquals(1, loaded.placement().stages().size());
assertTrue(loaded.chunk().stages().isEmpty());
@@ -24,6 +24,8 @@ import com.volmit.support.MemoryEdit;
import com.volmit.support.TestContexts;
import com.volmit.support.TestTerrain;
import com.volmit.world.BlockPosition;
import com.volmit.example.placement.SnowModule;
import com.volmit.example.placement.SnowSettings;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
@@ -34,7 +36,7 @@ import static org.junit.jupiter.api.Assertions.*;
class StageGraphTest {
private static final BlockState STONE = new BlockState("minecraft:stone");
@Feature(id = "test:toggle", name = "Toggle", field = "toggle", targets = FeatureAttachment.Target.OBJECT_PLACEMENT)
@Feature(id = "test:owner/toggle", name = "Toggle", field = "toggle", targets = FeatureAttachment.Target.OBJECT_PLACEMENT)
static final class Toggle implements IrisFeature {}
static final class Unannotated implements IrisFeature {}
@@ -227,17 +229,26 @@ class StageGraphTest {
}
@Test
void settingsMustBeOwnedByAModuleOfTheirNamespace() {
void settingsMustBeOwnedByAModuleThatNamesThem() {
var foreign = assertThrows(IllegalArgumentException.class, () -> resolveSettings(
module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {}))));
assertEquals("Settings type " + Toggle.class.getName() + " has feature id test:toggle, but no module of namespace 'test' "
+ "names it in when(...); it is named by other:user from host classpath. A feature id uses the namespace "
+ "of the module that owns the settings", foreign.getMessage());
assertEquals("Settings type " + Toggle.class.getName() + " has feature id test:owner/toggle, which none of the modules "
+ "naming it in when(...) owns: other:user from host classpath. A feature id is the id of the module that owns "
+ "the settings, such as other:user, or starts with it and '/', such as other:user/toggle", foreign.getMessage());
var sameNamespace = assertThrows(IllegalArgumentException.class, () -> resolveSettings(
module("test:user", stages -> stages.define(Stage.placement("test:user/work")).when(Toggle.class).run((context, toggle) -> {}))));
assertTrue(sameNamespace.getMessage().contains("none of the modules naming it in when(...) owns: test:user from host classpath"),
sameNamespace.getMessage());
var shared = resolveSettings(
module("test:owner", stages -> stages.define(Stage.placement("test:owner/work")).when(Toggle.class).run((context, toggle) -> {})),
module("other:user", stages -> stages.define(Stage.placement("other:user/work")).when(Toggle.class).run((context, toggle) -> {})));
assertEquals(List.of(Toggle.class), shared);
var snowOnMoss = resolveSettings(new SnowModule(), module("example:moss", stages ->
stages.define(Stage.placement("example:moss/on-snow")).when(SnowSettings.class).run((context, snow) -> {})));
assertTrue(snowOnMoss.contains(SnowSettings.class), "moss may condition on iris:snow, which iris:snow owns");
}
@Test