Close the object-placement couplings of the tooling audit

PlacementRun carries the placement's display name and its JSON, from which the
host reads the settings other modules declare at the placement level.
FeatureRegistry.registerPlacementType lets the module that owns placements
declare the type and its JSON names for the placement level. RunningWorld and
PlacementTerrain keep a typed PlacementRecord (object id, placement id,
optional structure id) instead of a text. Loot gives the seed a container is
shuffled with, the ProtectedFloor service says whether the lowest layer belongs
to the terrain module, and EntitySpec holds the spawn reason, rising out of the
ground, panda genes and applying to a provided mob.
This commit is contained in:
piexel committed 2026-10-28 04:02:32 +01:00
1 parent 0347e106c3
commit a9e3cbaa93
18 files changed
+355 -50

No files matched your search

@@ -40,6 +40,7 @@ public final class FeatureCatalog {
private final List<LifecycleRegistration> lifecycles;
private final Map<Class<?>, List<ServiceRegistration>> registrationsByService;
private final Map<Class<?>, List<?>> providersByService;
private final Optional<PlacementTypeRegistration> placementType;
FeatureCatalog(
Map<Class<?>, FeatureDescriptor<?>> settings,
@@ -48,7 +49,9 @@ public final class FeatureCatalog {
Collection<ResourceKindRegistration> resourceKinds,
List<ValidatorRegistration> validators,
List<LifecycleRegistration> lifecycles,
List<ServiceRegistration> services) {
List<ServiceRegistration> services,
PlacementTypeRegistration placementType) {
this.placementType = Optional.ofNullable(placementType);
this.settings = Map.copyOf(settings);
this.objects = Map.copyOf(objects);
this.mantle = Map.copyOf(mantle);
@@ -72,6 +75,11 @@ public final class FeatureCatalog {
return sorted(mantle.values(), registration -> registration.key().id());
}
/** The type packs write their object placements as, if a module declared it ({@code FeatureRegistry.registerPlacementType}). */
public Optional<PlacementTypeRegistration> placementType() {
return placementType;
}
/** The pack resource kinds modules read, by kind id. */
public List<ResourceKindRegistration> resourceKinds() {
return resourceKinds;
@@ -47,6 +47,7 @@ public final class ModuleRegistry implements FeatureRegistry {
private final List<ServiceRegistration> services = new ArrayList<>();
private final Set<String> reservedFolders;
private final List<DiscoveredModule> registered = new ArrayList<>();
private PlacementTypeRegistration placementType;
private FeatureCatalog catalog;
private DiscoveredModule registering;
@@ -128,6 +129,36 @@ public final class ModuleRegistry implements FeatureRegistry {
resourceKinds.put(kind.id(), registration);
}
@Override
public void registerPlacementType(Class<?> type, Set<String> jsonFields) {
checkOpen();
Objects.requireNonNull(type, "type");
Objects.requireNonNull(jsonFields, "jsonFields");
requireRegistering("Placement type " + type.getName());
var added = new PlacementTypeRegistration(type, jsonFields, registering);
if (placementType != null) {
throw new IllegalArgumentException("A pack has one placement type: " + placementType + " declared it already, and "
+ added + " declares it too; remove one of them");
}
added.jsonFields().forEach(FeatureDescriptor::requireFieldName);
for (var field : added.jsonFields().stream().sorted().toList()) {
requireUnclaimedPlacementField(field, added);
}
added.jsonFields().forEach(field -> keyClaimants.put(new KeyAtTarget(Level.PLACEMENT, field), KeyClaimant.of(added)));
placementType = added;
}
/** A field of the placement type is a JSON key of the placement, which a setting cannot also be. */
private void requireUnclaimedPlacementField(String field, PlacementTypeRegistration added) {
var earlier = keyClaimants.get(new KeyAtTarget(Level.PLACEMENT, field));
if (earlier == null) return;
var later = KeyClaimant.of(added);
var first = earlier.compareTo(later) <= 0 ? earlier : later;
var second = first == earlier ? later : earlier;
throw new IllegalArgumentException(targetName(Level.PLACEMENT) + " key \"" + field + "\" is claimed by " + first
+ " and " + second + "; give the setting another key in its @Feature");
}
@Override
public void registerValidator(Validator validator) {
checkOpen();
@@ -184,7 +215,8 @@ public final class ModuleRegistry implements FeatureRegistry {
public FeatureCatalog freeze() {
if (catalog == null) {
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services);
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services,
placementType);
}
return catalog;
}
@@ -308,8 +340,17 @@ public final class ModuleRegistry implements FeatureRegistry {
}
static KeyClaimant of(FeatureDescriptor<?> descriptor, DiscoveredModule owner) {
var source = owner.source() == DiscoveredModule.Source.HOST ? Source.BUILT_IN : Source.PACK;
return new KeyClaimant(source, descriptor.id().toString(), descriptor.id() + " (" + owner.origin() + ")");
return new KeyClaimant(sourceOf(owner), descriptor.id().toString(), descriptor.id() + " (" + owner.origin() + ")");
}
static KeyClaimant of(PlacementTypeRegistration placementType) {
var owner = placementType.module();
return new KeyClaimant(sourceOf(owner), placementType.type().getName(),
"the fields of " + placementType.type().getName() + " (" + owner.origin() + ")");
}
private static Source sourceOf(DiscoveredModule owner) {
return owner.source() == DiscoveredModule.Source.HOST ? Source.BUILT_IN : Source.PACK;
}
@Override
@@ -51,7 +51,7 @@ final class PackSchemaBuilder {
requireUniqueSnippets();
var types = completeTypes();
return new PackSchema(features.stream().map(feature -> feature.complete(types)).toList(), resources,
List.copyOf(types.values()));
List.copyOf(types.values()), catalog.placementType().map(placement -> placement.type().getName()));
}
private static Optional<ResourceId> resourceKindOf(FeatureCatalog catalog, Class<?> type) {
@@ -0,0 +1,18 @@
package com.volmit.host;
import java.util.Objects;
import java.util.Set;
/** The type a pack's object placements are written as, its JSON field names and the module that declared it. */
public record PlacementTypeRegistration(Class<?> type, Set<String> jsonFields, DiscoveredModule module) {
public PlacementTypeRegistration {
Objects.requireNonNull(type, "type");
jsonFields = Set.copyOf(jsonFields);
Objects.requireNonNull(module, "module");
}
@Override
public String toString() {
return "placement type " + type.getName() + " from module " + module;
}
}
@@ -6,6 +6,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
/** What the modules of a loaded pack add to the pack's JSON, for the host to render as JSON Schema and
* studio completion. It holds module-provided entries only: the host describes its own fields.
@@ -17,12 +18,16 @@ import java.util.Objects;
* @param features every catalogued settings type of the pack's modules, built-in ones included
* @param resources every resource kind of the pack's modules, built-in ones included
* @param types every class reachable from those settings and resource kinds, each once
* @param placementType the name of the class packs write their object placements as, the JSON holder of
* {@link Level#PLACEMENT}, if a module declared it ({@code FeatureRegistry.registerPlacementType})
*/
public record PackSchema(List<FeatureSchema> features, List<ResourceKindSchema> resources, List<ObjectType> types) {
public record PackSchema(List<FeatureSchema> features, List<ResourceKindSchema> resources, List<ObjectType> types,
Optional<String> placementType) {
public PackSchema {
features = features.stream().sorted(Comparator.comparing(FeatureSchema::id)).toList();
resources = resources.stream().sorted(Comparator.comparing(ResourceKindSchema::id)).toList();
types = types.stream().sorted(Comparator.comparing(ObjectType::name)).toList();
Objects.requireNonNull(placementType, "placementType");
}
/** The resource kind {@code id}, such as the one a {@link ValueKind.ResourceReferenceKind} names. */
@@ -36,6 +36,10 @@ class ModuleRegistryTest {
static class Veins implements IrisFeature {}
@Feature(id = "test:named", name = "Named", key = "name", targets = Level.BIOME)
static class Named implements IrisFeature {}
@Feature(id = "test:seams", name = "Seams", key = "deposits", targets = Level.PLACEMENT)
static class Seams implements IrisFeature {}
static class Placement {}
static class OtherPlacement {}
static class MissingAnnotation implements IrisFeature {}
@Feature(id = "test:core/abstract", name = "Abstract", key = "coreAbstract", targets = Level.BIOME)
abstract static class AbstractFeature implements IrisFeature {}
@@ -224,6 +228,62 @@ class ModuleRegistryTest {
assertTrue(stage.getMessage().startsWith("region key \"deposits\" is claimed by test:minerals (host classpath) and test:ores"), stage.getMessage());
}
@Test
void aPlacementTypeIsKeptWithItsFieldsAndReservesThemAsKeysOfThePlacementLevel() {
var registry = new ModuleRegistry();
registerAs(registry, "test:objects", placements -> placements.registerPlacementType(Placement.class, Set.of("place", "chance")));
var declared = registry.freeze().placementType().orElseThrow();
assertEquals(Placement.class, declared.type());
assertEquals(Set.of("place", "chance"), declared.jsonFields());
assertTrue(new ModuleRegistry().freeze().placementType().isEmpty());
}
@Test
void theSchemaNamesThePlacementTypeForStudio() {
var objects = new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("test:objects", "Objects", "1"); }
public void setup(ModuleSetup setup) { setup.registry().registerPlacementType(Placement.class, Set.of("place")); }
};
var loaded = LoadedModules.load(discovered(objects), Map.of(), Set.of(), Set.of());
assertEquals(Optional.of(Placement.class.getName()), loaded.schema().placementType());
assertTrue(LoadedModules.load(discovered(), Map.of(), Set.of(), Set.of()).schema().placementType().isEmpty());
}
@Test
void aSettingsKeyThatIsAFieldOfThePlacementTypeIsRefusedInEitherOrder() {
var placementFirst = new ModuleRegistry();
registerAs(placementFirst, "test:objects", placements -> placements.registerPlacementType(Placement.class, Set.of("deposits")));
var late = assertThrows(IllegalArgumentException.class,
() -> registerAs(placementFirst, "test:seams", settings -> settings.registerSettings(Seams.class)));
assertEquals("placement key \"deposits\" is claimed by the fields of " + Placement.class.getName()
+ " (host classpath) and test:seams (host classpath); give one of the settings another key in its @Feature", late.getMessage());
var settingsFirst = new ModuleRegistry();
registerAs(settingsFirst, "test:seams", settings -> settings.registerSettings(Seams.class));
var early = assertThrows(IllegalArgumentException.class,
() -> registerAs(settingsFirst, "test:objects", placements -> placements.registerPlacementType(Placement.class, Set.of("deposits"))));
assertEquals("placement key \"deposits\" is claimed by the fields of " + Placement.class.getName()
+ " (host classpath) and test:seams (host classpath); give the setting another key in its @Feature", early.getMessage());
assertTrue(settingsFirst.freeze().placementType().isEmpty(), "a refused placement type is not kept");
}
@Test
void aPackHasOnePlacementType() {
var registry = new ModuleRegistry();
registerAs(registry, "test:objects", placements -> placements.registerPlacementType(Placement.class, Set.of("place")));
var second = assertThrows(IllegalArgumentException.class,
() -> registerAs(registry, "test:other", placements -> placements.registerPlacementType(OtherPlacement.class, Set.of("kind"))));
assertTrue(second.getMessage().startsWith("A pack has one placement type: placement type " + Placement.class.getName()
+ " from module test:objects from host classpath declared it already"), second.getMessage());
assertEquals(Placement.class, registry.freeze().placementType().orElseThrow().type());
}
@Test
void aRefusedKeyLeavesNothingClaimedByTheFailedFeature() {
var registry = new ModuleRegistry(Map.of(Level.REGION, Set.of("deposits")), Set.of());