Settings are keyed by feature id and always an object: drop aliases and the one-option bare value

The settings decoder, the studio schema renderer and the schema builder no
longer know aliases. Tests and fixtures use the iris:deposit object form.
This commit is contained in:
piexel committed 2026-10-09 20:21:14 +02:00
1 parent 2851e5d304
commit d6228c3074
10 files changed
+84 -129

No files matched your search

@@ -81,7 +81,6 @@ public final class IrisModuleHost {
private static ModuleHost build() {
return ModuleHost.of(builtInModules())
.exposing(BUILT_IN_API_PACKAGE)
.reservingFields(ModuleTargets.reservedFields())
.describingHostTypesIn(HOST_TYPE_PACKAGE)
.reservingFolders(IrisData.ownFolderNames().toArray(String[]::new))
.copyingJarsTo(jarCopyRoot())
@@ -31,7 +31,6 @@ import com.google.gson.stream.JsonWriter;
import com.volmit.FeatureAttachment.Target;
import com.volmit.IrisFeature;
import com.volmit.block.BlockState;
import com.volmit.config.OptionsMetadata;
import com.volmit.config.Snippet;
import com.volmit.host.FeatureCatalog;
import com.volmit.host.FeatureDescriptor;
@@ -46,7 +45,6 @@ import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
@@ -55,14 +53,13 @@ import java.util.stream.Stream;
/**
* Reads the module settings a dimension, region or biome declares: each settings feature of the pack at
* that target, under its id or, for built-ins, its alias. Settings are created with their no-argument
* that target, under its id. Settings are created with their no-argument
* constructor and the JSON is read into them, so an omitted option keeps its default. A {@link Snippet}
* type may be written as {@code "snippet/<name>"} or, as Iris's own snippets are, {@code "snippet/<snippet folder>/<name>"},
* both read from {@code snippet/<snippet folder>/<name>.json}. IrisApi's value types ({@code BlockSpec},
* {@code NoiseStyle} and the rest) are read like settings, and an {@code Object} as Gson reads any JSON value.
* <p>
* Settings with exactly one option may be written as that option's value: {@code "snow": 0.5} reads as
* {@code "snow": {"amount": 0.5}}. Iris's own classes that built-in settings hold (see
* Settings are always written as their object. Iris's own classes that built-in settings hold (see
* {@link IrisModuleHost#isHostType}) are read by Iris's pack Gson, exactly as Iris reads them elsewhere.
*/
public final class ModuleSettingsDecoder {
@@ -92,7 +89,7 @@ public final class ModuleSettingsDecoder {
}
}
/** Every JSON key the pack's modules read at {@code target}: feature ids and built-in aliases, object generators' included. */
/** Every JSON key the pack's modules read at {@code target}: the feature ids, object generators' included. */
public Set<String> keys(Target target) {
return keys.get(target);
}
@@ -101,13 +98,14 @@ public final class ModuleSettingsDecoder {
* Decodes the settings {@code json} declares for {@code target}.
*
* @param resource names the file in errors, such as {@code biome desert/dunes}
* @throws IllegalArgumentException when the JSON holds both a feature's alias and its id, or a value
* that does not decode into the settings type
* @throws IllegalArgumentException when a value does not decode into its settings type
*/
public ModuleSettings decode(Target target, JSONObject json, String resource) {
Map<Class<? extends IrisFeature>, IrisFeature> values = new LinkedHashMap<>();
for (FeatureDescriptor<?> feature : settings.get(target)) {
presentKey(feature, json, resource).ifPresent(key -> values.put(feature.type(), read(feature, json.get(key), key, resource)));
if (json.has(feature.key())) {
values.put(feature.type(), read(feature, json.get(feature.key()), resource));
}
}
return ModuleSettings.of(values);
}
@@ -127,44 +125,25 @@ public final class ModuleSettingsDecoder {
return decoded;
}
private static Optional<String> presentKey(FeatureDescriptor<?> feature, JSONObject json, String resource) {
boolean hasKey = json.has(feature.key());
Optional<String> alias = feature.alias().filter(json::has);
if (hasKey && alias.isPresent()) {
throw new IllegalArgumentException(resource + " sets both \"" + alias.get() + "\" and \"" + feature.key()
+ "\", the alias and the id of settings " + feature.id() + "; keep one");
}
return hasKey ? Optional.of(feature.key()) : alias;
}
private IrisFeature read(FeatureDescriptor<?> feature, Object value, String key, String resource) {
private IrisFeature read(FeatureDescriptor<?> feature, Object value, String resource) {
try {
IrisFeature decoded = gson.fromJson(JSONObject.valueToString(asObject(feature, value)), feature.type());
requireObjectOrSnippet(feature, value);
IrisFeature decoded = gson.fromJson(JSONObject.valueToString(value), feature.type());
if (decoded == null) {
throw new JsonParseException("the value is null");
}
return decoded;
} catch (RuntimeException failure) {
throw new IllegalArgumentException("Cannot read settings " + feature.id() + " under \"" + key + "\" in "
throw new IllegalArgumentException("Cannot read settings " + feature.id() + " under \"" + feature.key() + "\" in "
+ resource + ": " + failure.getMessage(), failure);
}
}
/** A bare value of settings with exactly one option, as the object holding it; anything else unchanged. */
private static Object asObject(FeatureDescriptor<?> feature, Object value) {
if (value instanceof JSONObject || isSnippetReference(feature, value)) {
return value;
private static void requireObjectOrSnippet(FeatureDescriptor<?> feature, Object value) {
if (value instanceof JSONObject || (value instanceof String && feature.type().isAnnotationPresent(Snippet.class))) {
return;
}
List<OptionsMetadata.Option> options = feature.options().options();
if (options.size() != 1) {
throw new JsonParseException("write an object; only settings with exactly one option may be written as that"
+ " option's value, and these have " + options.size());
}
return new JSONObject().put(options.getFirst().name(), value);
}
private static boolean isSnippetReference(FeatureDescriptor<?> feature, Object value) {
return feature.type().isAnnotationPresent(Snippet.class) && value instanceof String text && text.startsWith(SNIPPET_PREFIX);
throw new JsonParseException("write the settings as an object, {\"<option>\": ...}");
}
private static List<FeatureDescriptor<?>> settingsAt(FeatureCatalog catalog, Target target) {
@@ -178,10 +157,7 @@ public final class ModuleSettingsDecoder {
Set<String> found = new TreeSet<>();
Stream.concat(catalog.settings().stream(), objects)
.filter(feature -> feature.attachment().targets().contains(target))
.forEach(feature -> {
found.add(feature.key());
feature.alias().ifPresent(found::add);
});
.forEach(feature -> found.add(feature.key()));
return Set.copyOf(found);
}
@@ -18,17 +18,14 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.core.loader.JsonFieldNames;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import com.volmit.FeatureAttachment.Target;
import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* Which Iris class holds the JSON of each attachment target that module settings can appear in.
@@ -50,11 +47,4 @@ public final class ModuleTargets {
.map(Map.Entry::getKey)
.findFirst();
}
/** Iris's own JSON fields per target, which no built-in alias may take. */
static Map<Target, Set<String>> reservedFields() {
Map<Target, Set<String>> fields = new EnumMap<>(Target.class);
HOLDERS.forEach((target, holder) -> fields.put(target, JsonFieldNames.of(holder)));
return fields;
}
}
@@ -38,10 +38,9 @@ import java.util.function.Function;
/**
* Renders the settings a pack's modules add, described by IrisApi as a {@link PackSchema}, into the JSON
* schema studio writes: each feature as a property of its target under its id and, for built-ins, its
* alias, each settings class once as a definition, references as completion lists (resource references as the keys
* of the module resource kind they name). Settings with one option also accept that option's bare value; Iris's own
* classes in built-in settings use Iris's own definitions, and so do IrisApi's value types, which read the same JSON as
* schema studio writes: each feature as a property of its target under its id, each settings class once as a
* definition, references as completion lists (resource references as the keys of the module resource kind they
* name). Iris's own classes in built-in settings use Iris's own definitions, and so do IrisApi's value types, which read the same JSON as
* an Iris class ({@code BlockSpec} as Iris's block data), with the snippet references their {@code @Snippet} allows.
*/
final class ModuleSchemaRenderer {
@@ -90,18 +89,10 @@ final class ModuleSchemaRenderer {
this.snippetFiles = snippetFiles;
}
/**
* Adds every feature at {@code target} to {@code properties} under its id and alias, and to
* {@code constraints} that one JSON object may not hold both.
*/
void addFeatures(Target target, JSONObject properties, JSONArray constraints) {
/** Adds every feature at {@code target} to {@code properties} under its id. */
void addFeatures(Target target, JSONObject properties) {
for (FeatureSchema feature : schema.fields(target)) {
JSONObject property = featureProperty(feature);
properties.put(feature.key(), property);
feature.alias().ifPresent(alias -> {
properties.put(alias, property);
constraints.put(notBoth(alias, feature.key()));
});
properties.put(feature.key(), featureProperty(feature));
}
}
@@ -115,27 +106,15 @@ final class ModuleSchemaRenderer {
return file;
}
/** The settings object, or with exactly one option also that option's bare value. */
private JSONObject featureProperty(FeatureSchema feature) {
JSONObject object = new JSONObject();
object.put("$ref", DEFINITION_REF + definition(feature.settings().name()));
JSONObject property = feature.shorthand()
.map(option -> new JSONObject().put("anyOf", new JSONArray(List.of(object, optionSchema(option)))))
.orElse(object);
JSONObject property = new JSONObject();
property.put("$ref", DEFINITION_REF + definition(feature.settings().name()));
String description = feature.name() + " (" + feature.id() + "), from module " + feature.module().name()
+ " (" + feature.module().id() + ")" + (feature.settings().description().isEmpty() ? "" : ". " + feature.settings().description());
property.put("description", description);
return property;
}
private static JSONObject notBoth(String alias, String key) {
JSONObject both = new JSONObject();
both.put("required", new JSONArray(List.of(alias, key)));
JSONObject not = new JSONObject();
not.put("not", both);
return not;
}
/** The definition key of a settings class, built the first time a property names it. */
private String definition(String typeName) {
String key = DEFINITION_PREFIX + typeName.replace('.', '-').replace('$', '-').toLowerCase(Locale.ROOT);
@@ -372,7 +372,7 @@ public class SchemaBuilder {
}
buildProperties(properties, required, extended, c);
addModuleFeatures(c, properties, extended);
addModuleFeatures(c, properties);
if (required.length() > 0) {
o.put("required", required);
@@ -388,8 +388,8 @@ public class SchemaBuilder {
}
/** The pack modules' settings at the target whose JSON {@code c} is, such as a biome's. */
private void addModuleFeatures(Class<?> c, JSONObject properties, JSONArray constraints) {
ModuleTargets.ofResource(c).ifPresent(target -> moduleSchemas().addFeatures(target, properties, constraints));
private void addModuleFeatures(Class<?> c, JSONObject properties) {
ModuleTargets.ofResource(c).ifPresent(target -> moduleSchemas().addFeatures(target, properties));
}
private ModuleSchemaRenderer moduleSchemas() {