Batch 3 of the API review: extension point, resources by kind

- PackWorld, InvocationContext and PackSettings get extension(type),
  an Optional empty by default: the one door a host's own modules use
  to reach its facades. The runners forward the inputs' extensions;
  the world a module sees (ModuleWorld, built by the host) forwards
  the world's. PackWorld.withLog is gone.
- A chunk stage writes registered mantle layers inside its own chunk
  and reads the rest (ChunkMantle); ReadOnlyMantle and its host() are
  gone.
- PackResources.find(kind, id), require(kind, id), ids(kind) and the
  host kinds BIOMES, REGIONS, OBJECTS replace ResourceKey and
  ResourceKind.key. Services and LinkedBiomes name pack resources by
  ResourceId. @Ref(PACK_*) and @ResourceRef fit ResourceId fields.
- BlockAccess extends BlockView.
- Absent values in ItemDrop, EntitySpec and AmbientEffects.Effect are
  Optionals; StructureFit.Start.key() returns a StartKey.
- Mantle.caverns() becomes isCarved(x, y, z).
- OptionsMetadata and DeclaredFieldOrder move to com.volmit.host.
- README, DESIGN and CONTRACTS updated; ExtensionsTest added.
This commit is contained in:
piexel committed 2026-10-24 00:17:10 +02:00
1 parent 7143908d4b
commit 3a307b5b8e
54 files changed
+821 -398

No files matched your search

@@ -1,55 +0,0 @@
package com.volmit.config;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.classfile.ClassFile;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/** Orders a class's own fields as its source declares them. {@link Class#getDeclaredFields()} promises
* no order, but the class file lists fields in declaration order, so this reads it.
*/
final class DeclaredFieldOrder {
private static final String CLASS_FILE_SUFFIX = ".class";
private DeclaredFieldOrder() {}
static List<Field> of(Class<?> type) {
var names = fieldNames(type);
return Arrays.stream(type.getDeclaredFields())
.sorted(Comparator.comparingInt((Field field) -> position(names, field, type)))
.toList();
}
private static int position(List<String> names, Field field, Class<?> type) {
int position = names.indexOf(field.getName());
if (position < 0) {
throw new IllegalArgumentException("Field " + field.getName() + " of " + type.getName()
+ " is not in its class file, so its declaration order is unknown");
}
return position;
}
private static List<String> fieldNames(Class<?> type) {
try (var classFile = openClassFile(type)) {
return ClassFile.of().parse(classFile.readAllBytes()).fields().stream()
.map(field -> field.fieldName().stringValue())
.toList();
} catch (IOException failure) {
throw new UncheckedIOException("Cannot read the class file of " + type.getName(), failure);
}
}
private static InputStream openClassFile(Class<?> type) {
var resource = "/" + type.getName().replace('.', '/') + CLASS_FILE_SUFFIX;
var stream = type.getResourceAsStream(resource);
if (stream == null) {
throw new IllegalArgumentException("Cannot read the class file of " + type.getName()
+ " to order its options as declared; settings classes must come from a jar or a class directory");
}
return stream;
}
}
@@ -1,6 +1,8 @@
package com.volmit.config;
import com.volmit.IrisFeature;
import com.volmit.resource.PackResources;
import com.volmit.resource.ResourceId;
import java.util.Collection;
import java.util.Set;
@@ -9,15 +11,17 @@ import java.util.Set;
* Implemented by settings that make pack biomes part of a pack's layout besides the ones its regions list: the biome
* that a cave band puts underground, or the target of a floating island. The host asks the settings of every dimension
* and biome it reaches, without knowing the module behind them, so such a biome is loaded, registered and given its own
* settings like any other. Keys are pack biome keys as packs write them; blank keys and keys naming no biome are
* ignored.
* settings like any other. Biomes are named by their ids, {@link PackResources#idOf} of the keys the settings hold; ids
* naming no biome of the pack are ignored.
*/
public interface LinkedBiomes {
/**
* Biomes that join the biome tree of every region the owner is part of, like the owner's children: the host registers
* them with the region, so their terrain generators take part in the region's terrain.
*
* @param pack the pack the settings belong to, which turns their keys into ids
*/
default Set<String> treeBiomes() {
default Set<ResourceId> treeBiomes(PackResources pack) {
return Set.of();
}
@@ -25,10 +29,11 @@ public interface LinkedBiomes {
* Biomes the settings use besides, which a dimension reaches (their objects, decorators and settings take part in
* generation) without joining the region's biome tree.
*
* @param pack the pack the settings belong to, which turns their keys into ids
* @param dimension the settings the owner's dimension declares, for settings that name something the dimension
* declares, such as a band of its caves
*/
default Set<String> usedBiomes(Collection<IrisFeature> dimension) {
default Set<ResourceId> usedBiomes(PackResources pack, Collection<IrisFeature> dimension) {
return Set.of();
}
}
@@ -1,93 +0,0 @@
package com.volmit.config;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/** Annotation metadata, independent of JSON libraries. Does not decode or validate values. */
public record OptionsMetadata(String description, String snippet, List<Option> options) {
public OptionsMetadata {
options = List.copyOf(options);
}
/** One field of a settings type. {@code minimum}, {@code maximum}, {@code minItems}, {@code reference} and
* {@code resource} are null when not declared; {@code resource} is the class of {@link ResourceRef}.
*/
public record Option(Field field, String description, boolean required,
Double minimum, Double maximum, Integer minItems, Reference reference, Class<?> resource) {
public String name() {
return field.getName();
}
public Type type() {
return field.getGenericType();
}
}
/** Reads private and inherited fields without constructing settings or accessing values. Options are
* in declaration order, a superclass's before its subclass's. Nested object types are preserved for
* the schema builder to inspect recursively.
*/
public static OptionsMetadata inspect(Class<?> type) {
var options = new ArrayList<Option>();
var names = new HashSet<String>();
for (var current : hierarchy(type)) {
for (var field : DeclaredFieldOrder.of(current)) {
int modifiers = field.getModifiers();
if (field.isSynthetic() || Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) {
continue;
}
if (!names.add(field.getName())) {
throw new IllegalArgumentException("Duplicate option: " + field.getName());
}
var desc = field.getAnnotation(Desc.class);
var min = field.getAnnotation(MinNumber.class);
var max = field.getAnnotation(MaxNumber.class);
var minItems = field.getAnnotation(MinItems.class);
var ref = field.getAnnotation(Ref.class);
var resourceRef = field.getAnnotation(ResourceRef.class);
if ((min != null || max != null) && !isNumeric(field.getType())) {
throw new IllegalArgumentException("Numeric bounds on nonnumeric option: " + field.getName());
}
if ((min != null && !Double.isFinite(min.value()))
|| (max != null && !Double.isFinite(max.value()))
|| (min != null && max != null && min.value() > max.value())) {
throw new IllegalArgumentException("Invalid bounds for option: " + field.getName());
}
if (minItems != null && (!isCollection(field.getType()) || minItems.value() < 0)) {
throw new IllegalArgumentException("Invalid @MinItems on option: " + field.getName()
+ "; it fits lists, sets and arrays, and its value is not negative");
}
options.add(new Option(field, desc == null ? "" : desc.value(), field.isAnnotationPresent(Required.class),
min == null ? null : min.value(), max == null ? null : max.value(),
minItems == null ? null : minItems.value(),
ref == null ? null : ref.value(), resourceRef == null ? null : resourceRef.value()));
}
}
var desc = type.getAnnotation(Desc.class);
var snippet = type.getAnnotation(Snippet.class);
return new OptionsMetadata(desc == null ? "" : desc.value(),
snippet == null ? "" : snippet.value(), options);
}
/** {@code type} and its superclasses below Object, the topmost first. */
private static List<Class<?>> hierarchy(Class<?> type) {
var classes = new ArrayList<Class<?>>();
for (Class<?> current = type; current != null && current != Object.class; current = current.getSuperclass()) {
classes.addFirst(current);
}
return classes;
}
private static boolean isCollection(Class<?> type) {
return List.class.isAssignableFrom(type) || java.util.Set.class.isAssignableFrom(type) || type.isArray();
}
private static boolean isNumeric(Class<?> type) {
return Number.class.isAssignableFrom(type) || type == byte.class || type == short.class
|| type == int.class || type == long.class || type == float.class || type == double.class;
}
}
@@ -1,6 +1,7 @@
package com.volmit.config;
import com.volmit.IrisFeature;
import java.util.Optional;
/** Every settings declaration of the pack the host is loading, for what depends on the whole pack rather
* than one column, such as how far a mantle stage reaches ({@link com.volmit.stage.PackReach}).
@@ -14,4 +15,14 @@ public interface PackSettings {
* empty when nothing declares it.
*/
<F extends IrisFeature> SettingsStack<F> declared(Class<F> featureType);
/**
* What the host offers for the pack it is loading beyond the API, as an object of {@code type}: empty when it offers
* no such thing. The same door as {@link com.volmit.world.PackWorld#extension}, for what is worked out from the
* settings before any world generates, such as a {@link com.volmit.stage.PackReach} that needs the size of the pack's
* objects from the host.
*/
default <T> Optional<T> extension(Class<T> type) {
return Optional.empty();
}
}