From 77e4ab471032a64b9edf6321bf3588aa9acd1372 Mon Sep 17 00:00:00 2001 From: piexel Date: Mon, 5 Oct 2026 22:14:06 +0200 Subject: [PATCH] Make ModuleHost.reservingFields add to earlier reservations and check field names when built reservingFields replaced the reserved fields, unlike exposing and reservingNamespaces, which add. It now merges the given names per attachment target with those reserved before, so every ModuleHost method accumulates. Blank field names now fail when the host configuration is built, like invalid packages and namespaces, instead of at the first pack load. --- src/main/java/com/volmit/host/ModuleHost.java | 27 ++++++++++++++++--- .../java/com/volmit/host/LoadedPackTest.java | 21 ++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/volmit/host/ModuleHost.java b/src/main/java/com/volmit/host/ModuleHost.java index 61def7252..6e41b764a 100644 --- a/src/main/java/com/volmit/host/ModuleHost.java +++ b/src/main/java/com/volmit/host/ModuleHost.java @@ -23,8 +23,10 @@ import java.util.TreeSet; * .reservingFields(reservedHostFields); * } * - * Built-in modules' {@code info()} is read here, once. Exposed package names and reserved namespaces - * are checked here, so a mistake fails when the host starts rather than at the first pack load. + * Every method adds to what earlier calls configured, so a host can build the configuration in steps. + * Built-in modules' {@code info()} is read here, once. Exposed package names, reserved namespaces and + * reserved field names are checked here, so a mistake fails when the host starts rather than at the + * first pack load. */ public record ModuleHost( List builtIns, @@ -38,6 +40,7 @@ public record ModuleHost( reservedNamespaces = Set.copyOf(reservedNamespaces); ExposedPackages.apiAnd(exposedHostPackages); ReservedNamespaces.of(reservedNamespaces); + requireFieldNames(reservedHostFields); } /** The given built-in modules, exposing only the Iris API and reserving nothing. */ @@ -58,9 +61,11 @@ public record ModuleHost( return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, union(reservedNamespaces, namespaces)); } - /** This configuration with {@code fields} as the JSON fields the host itself uses, per target. */ + /** This configuration with {@code fields} also reserved as JSON fields the host itself uses, + * merged per attachment target with the fields reserved before. + */ public ModuleHost reservingFields(Map> fields) { - return new ModuleHost(builtIns, exposedHostPackages, fields, reservedNamespaces); + return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields), reservedNamespaces); } /** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} installs them. */ @@ -76,6 +81,20 @@ public record ModuleHost( return Map.copyOf(copy); } + /** A blank field name fails here, as it would in a settings type's {@code @Feature}. */ + private static void requireFieldNames(Map> fields) { + fields.forEach((target, names) -> names.forEach(name -> new FeatureAttachment(name, Set.of(target)))); + } + + private static Map> merge( + Map> existing, + Map> added) { + var merged = new EnumMap>(FeatureAttachment.Target.class); + existing.forEach((target, names) -> merged.put(target, new TreeSet<>(names))); + added.forEach((target, names) -> merged.computeIfAbsent(target, ignored -> new TreeSet<>()).addAll(names)); + return merged; + } + private static Set union(Set existing, String... added) { var names = new TreeSet<>(existing); names.addAll(Arrays.asList(added)); diff --git a/src/test/java/com/volmit/host/LoadedPackTest.java b/src/test/java/com/volmit/host/LoadedPackTest.java index 5b08f7317..7f689448b 100644 --- a/src/test/java/com/volmit/host/LoadedPackTest.java +++ b/src/test/java/com/volmit/host/LoadedPackTest.java @@ -1,5 +1,6 @@ package com.volmit.host; +import com.volmit.FeatureAttachment.Target; import com.volmit.support.TestModuleJar; import example.builtin.CarveModule; import org.junit.jupiter.api.AfterEach; @@ -7,6 +8,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.nio.file.Path; import java.util.List; +import java.util.Map; +import java.util.Set; import static com.volmit.support.TestModuleJar.module; import static org.junit.jupiter.api.Assertions.*; @@ -83,7 +86,23 @@ class LoadedPackTest { var host = ModuleHost.of(List.of()); assertThrows(IllegalArgumentException.class, () -> host.exposing("org.*")); assertThrows(IllegalArgumentException.class, () -> host.reservingNamespaces("iris:core")); + assertThrows(IllegalArgumentException.class, () -> host.reservingFields(Map.of(Target.BIOME, Set.of(" ")))); assertEquals(List.of(), host.builtIns()); - assertEquals(java.util.Set.of(), host.exposedHostPackages()); + assertEquals(Set.of(), host.exposedHostPackages()); + } + + @Test + void everyHostSettingAddsToWhatEarlierCallsConfigured() { + var host = ModuleHost.of(List.of()) + .exposing("example.first") + .exposing("example.second") + .reservingNamespaces("iris") + .reservingNamespaces("vanilla") + .reservingFields(Map.of(Target.BIOME, Set.of("carving"), Target.REGION, Set.of("spawns"))) + .reservingFields(Map.of(Target.BIOME, Set.of("objects"))); + + assertEquals(Set.of("example.first", "example.second"), host.exposedHostPackages()); + assertEquals(Set.of("iris", "vanilla"), host.reservedNamespaces()); + assertEquals(Map.of(Target.BIOME, Set.of("carving", "objects"), Target.REGION, Set.of("spawns")), host.reservedHostFields()); } }