Type platform keys and fold pack references into @Ref(kind)

Batch 6, findings 41 and 42.

- Platform keys travel as ResourceId: enchantments, attributes, sounds,
  particles, potions, loot tables, sapling species, marker tags, upper
  dimension ids and jigsaw start pools. ResourceId.ofPlatformKey normalises
  a key a pack wrote.
- Item and entity types are a ContentKey (Registered id, or Provided
  provider and name) instead of strings and a special-type flag.
- ItemDrop groups into Look, Wear and PlatformExtras; colours are an RGB
  int and DyeColor, attribute operations an enum.
- EntitySpec groups into Appearance, Behaviour, Company, Drops and Arrival,
  and carries host extensions instead of Iris-only fields.
- @Ref(kind = Type.class) replaces @ResourceRef and the PACK_* references;
  expressions and image maps become the host kinds EXPRESSIONS and
  IMAGE_MAPS.
This commit is contained in:
piexel committed 2026-10-24 06:06:44 +02:00
1 parent 05f621d82d
commit 6b24e6f150
31 files changed
+620 -295

No files matched your search

@@ -7,7 +7,6 @@ import com.volmit.config.MinNumber;
import com.volmit.config.Ref;
import com.volmit.config.Reference;
import com.volmit.config.Required;
import com.volmit.config.ResourceRef;
import com.volmit.config.Snippet;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
@@ -25,7 +24,8 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
}
/** 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}.
* {@code resource} are null when not declared; {@code reference} is the registry and {@code resource} the
* kind's type a {@link Ref} gives, of which at most one is set.
*/
public record Option(Field field, String description, boolean required,
Double minimum, Double maximum, Integer minItems, Reference reference, Class<?> resource) {
@@ -59,7 +59,6 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
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());
}
@@ -72,10 +71,15 @@ public record OptionsMetadata(String description, String snippet, List<Option> o
throw new IllegalArgumentException("Invalid @MinItems on option: " + field.getName()
+ "; it fits lists, sets and arrays, and its value is not negative");
}
if (ref != null && ref.value().length + (ref.kind() == void.class ? 0 : 1) != 1) {
throw new IllegalArgumentException("Invalid @Ref on option: " + field.getName()
+ "; give exactly one of a Reference or a kind");
}
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()));
ref == null || ref.value().length == 0 ? null : ref.value()[0],
ref == null || ref.kind() == void.class ? null : ref.kind()));
}
}
var desc = type.getAnnotation(Desc.class);
@@ -20,8 +20,8 @@ import java.util.regex.Pattern;
import java.util.stream.Stream;
/** Describes a loaded pack's catalogued settings and resource kinds for studio. Fails the load, naming the class,
* field and module, on what the host could not render: an unsupported field type, a misplaced {@code @Ref} or
* {@code @ResourceRef}, a class without a usable no-argument constructor, a snippet name used twice, a pack
* field and module, on what the host could not render: an unsupported field type, a misplaced {@code @Ref} or one
* naming a kind nobody has, a class without a usable no-argument constructor, a snippet name used twice, a pack
* class's snippet name outside its module's path, or a pack module's resource kind read into a host type.
*/
@SuppressWarnings("removal") // held types and host kinds are transitional, until the cut-over
@@ -32,7 +32,7 @@ final class TypeDrafts {
private final HostTypes hostTypes;
private final Function<Class<?>, Optional<ResourceId>> resourceKinds;
/** @param resourceKinds the id of the resource kind read into a class, which {@code @ResourceRef} options name */
/** @param resourceKinds the id of the module resource kind read into a class, which {@code @Ref(kind = ...)} options name */
TypeDrafts(HostTypes hostTypes, Function<Class<?>, Optional<ResourceId>> resourceKinds) {
this.hostTypes = hostTypes;
this.resourceKinds = resourceKinds;
@@ -18,20 +18,23 @@ import com.volmit.host.schema.ValueKind.ObjectKind;
import com.volmit.host.schema.ValueKind.ReferenceKind;
import com.volmit.host.schema.ValueKind.ResourceReferenceKind;
import com.volmit.host.schema.ValueKind.TextKind;
import com.volmit.resource.PackResources;
import com.volmit.resource.ResourceId;
import com.volmit.resource.ResourceKind;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** Maps a settings field's declared type, {@code @Ref} and {@code @ResourceRef} to the JSON it takes. Host types
/** Maps a settings field's declared type and {@code @Ref} to the JSON it takes. Host types
* become host kinds where the settings may use them; other classes that are none of the built-in kinds become
* object kinds through the given function. Throws {@link SettingsTypeProblem} for a type or reference that does
* not fit.
@@ -45,8 +48,9 @@ final class ValueKinds {
Byte.class, Short.class, Integer.class, Long.class);
private static final Set<Class<?>> DECIMALS = Set.of(float.class, double.class, Float.class, Double.class);
private static final String DECLARE_COLLECTION = "declare it as List<...>, Set<...> or Map<String, ...>";
private static final Set<Reference> PACK_REFERENCES = EnumSet.of(Reference.PACK_BIOME, Reference.PACK_REGION, Reference.PACK_OBJECT,
Reference.PACK_EXPRESSION, Reference.PACK_IMAGE_MAP, Reference.PACK_STRUCTURE);
private static final Map<Class<?>, ResourceId> HOST_KINDS = Stream.of(PackResources.BIOMES, PackResources.REGIONS,
PackResources.OBJECTS, PackResources.EXPRESSIONS, PackResources.IMAGE_MAPS)
.collect(Collectors.toUnmodifiableMap(ResourceKind::type, ResourceKind::id));
private final Function<Class<?>, ObjectKind> objects;
private final HostTypes hostTypes;
@@ -68,13 +72,19 @@ final class ValueKinds {
return kind(option.type(), new Naming(option.reference(), option.resource()));
}
/** What a text option names: the field's {@code @Ref} and the class of its {@code @ResourceRef}, each null when absent. */
/** What a text option names: the registry or the kind's type of the field's {@code @Ref}, each null when absent. */
private record Naming(Reference reference, Class<?> resource) {
static final Naming NONE = new Naming(null, null);
boolean isEmpty() {
return reference == null && resource == null;
}
/** The annotation as written, for messages. */
@Override
public String toString() {
return reference != null ? "@Ref(" + reference + ")" : "@Ref(kind = " + resource.getName() + ")";
}
}
/** Lists, sets and arrays pass {@code naming} on to their elements. */
@@ -127,37 +137,29 @@ final class ValueKinds {
}
private ValueKind text(TextFormat format, Naming naming) {
if (naming.resource() != null) return resourceReference(naming);
if (naming.resource() != null) return resourceReference(naming.resource());
var reference = naming.reference();
if (reference == null) return new TextKind(format);
if (PACK_REFERENCES.contains(reference)) return new ReferenceKind(reference, TextFormat.PLAIN);
return new ReferenceKind(reference, format);
}
/** Packs name a kind's resources by key without a namespace, as they name their biomes, so the JSON holds a key
* whether the field is a String or a ResourceId (which the host reads as the pack's id of that key).
*/
private ValueKind resourceReference(Naming naming) {
var resource = naming.resource();
if (naming.reference() != null) {
throw new SettingsTypeProblem("has both @Ref(" + naming.reference() + ") and @ResourceRef(" + resource.getName()
+ "); an option names one kind of thing, so keep one");
}
private ValueKind resourceReference(Class<?> resource) {
var hostKind = HOST_KINDS.get(resource);
if (hostKind != null) return new ResourceReferenceKind(hostKind);
return resourceKinds.apply(resource).map(ResourceReferenceKind::new).orElseThrow(() -> new SettingsTypeProblem(
"has @ResourceRef(" + resource.getName() + "), but no module of the pack registers a resource kind read into"
+ " that class; name the class of a kind registered with FeatureRegistry.registerResourceKind"));
"has @Ref(kind = " + resource.getName() + "), but neither the host nor a module of the pack has a resource kind"
+ " read into that class; name the type of a PackResources kind or of a kind registered with"
+ " FeatureRegistry.registerResourceKind"));
}
private static ValueKind blockState(Naming naming) {
var reference = naming.reference();
if (naming.resource() == null && (reference == null || reference == Reference.BLOCK)) {
if (naming.isEmpty() || naming.reference() == Reference.BLOCK) {
return new ReferenceKind(Reference.BLOCK, TextFormat.BLOCK_STATE);
}
if (naming.resource() != null) {
throw new SettingsTypeProblem("has @ResourceRef(" + naming.resource().getName() + ") on BlockState, which always"
+ " names a block; remove the @ResourceRef or use String");
}
throw new SettingsTypeProblem("has @Ref(" + reference + ") on BlockState, which always names a block;"
throw new SettingsTypeProblem("has " + naming + " on BlockState, which always names a block;"
+ " remove the @Ref or use String or ResourceId");
}
@@ -180,12 +182,8 @@ final class ValueKinds {
private static void requireNoNaming(Type type, Naming naming) {
if (naming.isEmpty()) return;
if (naming.reference() != null) {
throw new SettingsTypeProblem("has @Ref(" + naming.reference() + ") on " + type.getTypeName()
+ ", but @Ref fits only String and ResourceId and lists, sets and arrays of them");
}
throw new SettingsTypeProblem("has @ResourceRef(" + naming.resource().getName() + ") on " + type.getTypeName()
+ ", but @ResourceRef fits only String and ResourceId and lists, sets and arrays of them");
throw new SettingsTypeProblem("has " + naming + " on " + type.getTypeName()
+ ", but @Ref fits only String and ResourceId and lists, sets and arrays of them");
}
private static boolean isCollection(Class<?> type) {
@@ -39,8 +39,12 @@ class VanillaStructureValuesTest {
"liquidSettings"), names(JigsawOverrides.class));
assertEquals(OptionalInt.empty(), JigsawOverrides.none().maxDepth());
assertEquals(JigsawOverrides.Heightmap.SOURCE, JigsawOverrides.none().projectStartToHeightmap());
var overrides = JigsawOverrides.none().withStartPool("minecraft:village/plains/town_centers").withMaxDepth(4)
var pool = com.volmit.resource.ResourceId.parse("minecraft:village/plains/town_centers");
var overrides = JigsawOverrides.none().withStartPool(pool).withMaxDepth(4)
.withMaxDistance(80, 120).withHeightmap(JigsawOverrides.Heightmap.NONE);
assertEquals(Optional.of(pool), overrides.startPool());
assertTrue(JigsawOverrides.none().withoutStartJigsaw().removesStartJigsaw());
assertNotEquals(JigsawOverrides.none(), JigsawOverrides.none().withoutStartJigsaw());
assertEquals(OptionalInt.of(4), overrides.maxDepth());
assertEquals(OptionalInt.of(120), overrides.maxDistanceVertical());
assertEquals(Optional.empty(), overrides.useExpansionHack());
@@ -29,6 +29,7 @@ import com.volmit.host.schema.ValueKind.ObjectKind;
import com.volmit.host.schema.ValueKind.ReferenceKind;
import com.volmit.host.schema.ValueKind.ResourceReferenceKind;
import com.volmit.host.schema.ValueKind.TextKind;
import com.volmit.resource.PackResources;
import com.volmit.resource.ResourceId;
import com.volmit.resource.ResourceKind;
import com.volmit.support.TestModuleJar;
@@ -89,7 +90,7 @@ class PackSchemaTest {
private ResourceId palette = ResourceId.parse("example:flora/default");
@Ref(Reference.BLOCK)
private String ground = "minecraft:grass_block";
@Ref(Reference.PACK_BIOME) @MinItems(1)
@Ref(kind = com.volmit.world.PackBiome.class) @MinItems(1)
private List<String> biomes = List.of("plains", "forest");
@Ref(Reference.ITEM)
private ResourceId[] drops = {};
@@ -261,7 +262,7 @@ class PackSchemaTest {
var settings = floraSchema().type("pack.flora.FloraSettings");
assertEquals(new ReferenceKind(Reference.BLOCK, TextFormat.PLAIN), option(settings, "ground").kind());
assertEquals(new ListKind(new ReferenceKind(Reference.PACK_BIOME, TextFormat.PLAIN), false), option(settings, "biomes").kind());
assertEquals(new ListKind(new ResourceReferenceKind(PackResources.BIOMES.id()), false), option(settings, "biomes").kind());
assertEquals(Optional.of(List.of("plains", "forest")), option(settings, "biomes").defaultValue());
assertEquals(OptionalInt.of(1), option(settings, "biomes").minItems(), "@MinItems is described");
assertEquals(OptionalInt.empty(), option(settings, "drops").minItems());
@@ -544,8 +545,8 @@ class PackSchemaTest {
var style = schema.type("com.volmit.noise.NoiseStyle");
assertEquals(Optional.of("style"), style.snippet(), "API classes keep the snippet folders packs already use");
assertEquals(new ReferenceKind(Reference.NOISE_STYLE, TextFormat.PLAIN), option(style, "style").kind());
assertEquals(new ReferenceKind(Reference.PACK_EXPRESSION, TextFormat.PLAIN), option(style, "expression").kind());
assertEquals(new ReferenceKind(Reference.PACK_IMAGE_MAP, TextFormat.PLAIN), option(style, "imageMap").kind());
assertEquals(new ResourceReferenceKind(PackResources.EXPRESSIONS.id()), option(style, "expression").kind());
assertEquals(new ResourceReferenceKind(PackResources.IMAGE_MAPS.id()), option(style, "imageMap").kind());
assertEquals(Optional.of("FLAT"), option(style, "style").defaultValue());
assertEquals(List.of("palette", "range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
}
@@ -684,16 +685,16 @@ class PackSchemaTest {
@Feature(id = "example:flora", name = "Flora", key = "flora", targets = Level.BIOME)
public final class FloraSettings implements IrisFeature {
@Ref(Reference.PACK_BIOME) private ResourceId home;
@Ref(Reference.PACK_OBJECT) private List<ResourceId> objects = List.of();
@Ref(kind = com.volmit.world.PackBiome.class) private ResourceId home;
@Ref(kind = com.volmit.object.GeneratedObject.class) private List<ResourceId> objects = List.of();
}
""")
.writeInto(pack, "flora.jar");
var settings = schemaOf(pack, NO_BUILT_INS).type("pack.flora.FloraSettings");
assertEquals(new ReferenceKind(Reference.PACK_BIOME, TextFormat.PLAIN), option(settings, "home").kind());
assertEquals(new ListKind(new ReferenceKind(Reference.PACK_OBJECT, TextFormat.PLAIN), false), option(settings, "objects").kind());
assertEquals(new ResourceReferenceKind(PackResources.BIOMES.id()), option(settings, "home").kind());
assertEquals(new ListKind(new ResourceReferenceKind(PackResources.OBJECTS.id()), false), option(settings, "objects").kind());
}
@Test
@@ -732,7 +733,7 @@ class PackSchemaTest {
public final class Keep {
@Desc("Towers on the walls") @MinNumber(1)
private int towers = 4;
@ResourceRef(Keep.class)
@Ref(kind = Keep.class)
private String next = "";
}
""")
@@ -746,7 +747,7 @@ class PackSchemaTest {
@Feature(id = "example:castle", name = "Castle", key = "castle", targets = Level.BIOME)
public final class CastleSettings implements IrisFeature {
@ResourceRef(Keep.class)
@Ref(kind = Keep.class)
private List<String> keeps = List.of("main");
}
""");
@@ -818,15 +819,16 @@ class PackSchemaTest {
@Test
void resourceReferencesNameARegisteredKindOnPlainText() {
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @ResourceRef(java.lang.Integer), but no"
+ " module of the pack registers a resource kind read into that class; name the class of a kind registered with"
+ " FeatureRegistry.registerResourceKind", "@ResourceRef(Integer.class) private String keep;");
assertLoadFails("Field pack.flora.FloraSettings.count of " + FLORA_SOURCE + " has @ResourceRef(java.lang.Integer) on int,"
+ " but @ResourceRef fits only String and ResourceId and lists, sets and arrays of them",
"@ResourceRef(Integer.class) private int count;");
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has both @Ref(BLOCK) and"
+ " @ResourceRef(java.lang.Integer); an option names one kind of thing, so keep one",
"@Ref(Reference.BLOCK) @ResourceRef(Integer.class) private String keep;");
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @Ref(kind = java.lang.Integer), but"
+ " neither the host nor a module of the pack has a resource kind read into that class; name the type of a"
+ " PackResources kind or of a kind registered with FeatureRegistry.registerResourceKind",
"@Ref(kind = Integer.class) private String keep;");
assertLoadFails("Field pack.flora.FloraSettings.count of " + FLORA_SOURCE + " has @Ref(kind = java.lang.Integer) on int,"
+ " but @Ref fits only String and ResourceId and lists, sets and arrays of them",
"@Ref(kind = Integer.class) private int count;");
assertLoadFails("Invalid @Ref on option: keep; give exactly one of a Reference or a kind",
"@Ref(value = Reference.BLOCK, kind = Integer.class) private String keep;");
assertLoadFails("Invalid @Ref on option: keep; give exactly one of a Reference or a kind", "@Ref private String keep;");
}
/** A built-in module {@code id} that registers {@code kind}. */