mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Split @Ref into @Ref(Reference) and @ResourceRef(Kind.class)
@Ref had two attributes of which exactly one had to be given, checked only at pack load. One annotation per concept makes a wrong combination a compile error; giving both still fails the load.
This commit is contained in:
1 parent
c577b68c00
commit
264f754297
11 files changed
+89
-70
No files matched your search
@@ -7,6 +7,7 @@ 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;
|
||||
@@ -59,6 +60,7 @@ 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());
|
||||
}
|
||||
@@ -71,15 +73,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");
|
||||
if (ref != null && resourceRef != null) {
|
||||
throw new IllegalArgumentException("Option " + field.getName()
|
||||
+ " has both @Ref and @ResourceRef; it names either a registry entry or a pack resource");
|
||||
}
|
||||
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 || ref.value().length == 0 ? null : ref.value()[0],
|
||||
ref == null || ref.kind() == void.class ? null : ref.kind()));
|
||||
ref == null ? null : ref.value(),
|
||||
resourceRef == null ? null : resourceRef.value()));
|
||||
}
|
||||
}
|
||||
var desc = type.getAnnotation(Desc.class);
|
||||
|
||||
@@ -32,7 +32,7 @@ final class TypeDrafts {
|
||||
private final HostTypes hostTypes;
|
||||
private final Function<Class<?>, Optional<ResourceId>> resourceKinds;
|
||||
|
||||
/** @param resourceKinds the id of the module resource kind read into a class, which {@code @Ref(kind = ...)} options name */
|
||||
/** @param resourceKinds the id of the module resource kind read into a class, which {@code @ResourceRef} options name */
|
||||
TypeDrafts(HostTypes hostTypes, Function<Class<?>, Optional<ResourceId>> resourceKinds) {
|
||||
this.hostTypes = hostTypes;
|
||||
this.resourceKinds = resourceKinds;
|
||||
|
||||
@@ -35,7 +35,7 @@ import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Maps a settings field's declared type and {@code @Ref} to the JSON it takes. Host types
|
||||
/** Maps a settings field's declared type and {@code @Ref} or {@code @ResourceRef} 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.
|
||||
@@ -73,7 +73,10 @@ final class ValueKinds {
|
||||
return kind(option.type(), new Naming(option.reference(), option.resource()));
|
||||
}
|
||||
|
||||
/** What a text option names: the registry or the kind's type of the field's {@code @Ref}, each null when absent. */
|
||||
/**
|
||||
* What a text option names: the registry of the field's {@code @Ref} or the kind's type of its {@code @ResourceRef},
|
||||
* each null when absent.
|
||||
*/
|
||||
private record Naming(Reference reference, Class<?> resource) {
|
||||
static final Naming NONE = new Naming(null, null);
|
||||
|
||||
@@ -81,10 +84,15 @@ final class ValueKinds {
|
||||
return reference == null && resource == null;
|
||||
}
|
||||
|
||||
/** The annotation's name, for messages. */
|
||||
String annotation() {
|
||||
return reference != null ? "@Ref" : "@ResourceRef";
|
||||
}
|
||||
|
||||
/** The annotation as written, for messages. */
|
||||
@Override
|
||||
public String toString() {
|
||||
return reference != null ? "@Ref(" + reference + ")" : "@Ref(kind = " + resource.getName() + ")";
|
||||
return reference != null ? "@Ref(" + reference + ")" : "@ResourceRef(" + resource.getName() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +159,7 @@ final class ValueKinds {
|
||||
var hostKind = HOST_KINDS.get(resource);
|
||||
if (hostKind != null) return new ResourceReferenceKind(hostKind);
|
||||
return resourceKinds.apply(resource).map(ResourceReferenceKind::new).orElseThrow(() -> new SettingsTypeProblem(
|
||||
"has @Ref(kind = " + resource.getName() + "), but neither the host nor a module of the pack has a resource kind"
|
||||
"has @ResourceRef(" + 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"));
|
||||
}
|
||||
@@ -161,7 +169,7 @@ final class ValueKinds {
|
||||
return new ReferenceKind(Reference.BLOCK, TextFormat.BLOCK_STATE);
|
||||
}
|
||||
throw new SettingsTypeProblem("has " + naming + " on BlockState, which always names a block;"
|
||||
+ " remove the @Ref or use String or ResourceId");
|
||||
+ " remove the " + naming.annotation() + " or use String or ResourceId");
|
||||
}
|
||||
|
||||
private static EnumKind enumKind(Class<?> type) {
|
||||
@@ -184,7 +192,7 @@ final class ValueKinds {
|
||||
private static void requireNoNaming(Type type, Naming naming) {
|
||||
if (naming.isEmpty()) return;
|
||||
throw new SettingsTypeProblem("has " + naming + " on " + type.getTypeName()
|
||||
+ ", but @Ref fits only String and ResourceId and lists, sets and arrays of them");
|
||||
+ ", but " + naming.annotation() + " fits only String and ResourceId and lists, sets and arrays of them");
|
||||
}
|
||||
|
||||
private static boolean isCollection(Class<?> type) {
|
||||
|
||||
@@ -87,7 +87,7 @@ class PackSchemaTest {
|
||||
private ResourceId palette = ResourceId.parse("example:flora/default");
|
||||
@Ref(Reference.BLOCK)
|
||||
private String ground = "minecraft:grass_block";
|
||||
@Ref(kind = com.volmit.world.PackBiome.class) @MinItems(1)
|
||||
@ResourceRef(com.volmit.world.PackBiome.class) @MinItems(1)
|
||||
private List<String> biomes = List.of("plains", "forest");
|
||||
@Ref(Reference.ITEM)
|
||||
private ResourceId[] drops = {};
|
||||
@@ -697,8 +697,8 @@ class PackSchemaTest {
|
||||
|
||||
@Feature(id = "example:flora", name = "Flora", key = "flora", targets = Level.BIOME)
|
||||
public final class FloraSettings implements IrisFeature {
|
||||
@Ref(kind = com.volmit.world.PackBiome.class) private ResourceId home;
|
||||
@Ref(kind = com.volmit.object.GeneratedObject.class) private List<ResourceId> objects = List.of();
|
||||
@ResourceRef(com.volmit.world.PackBiome.class) private ResourceId home;
|
||||
@ResourceRef(com.volmit.object.GeneratedObject.class) private List<ResourceId> objects = List.of();
|
||||
}
|
||||
""")
|
||||
.writeInto(pack, "flora.jar");
|
||||
@@ -745,7 +745,7 @@ class PackSchemaTest {
|
||||
public final class Keep {
|
||||
@Desc("Towers on the walls") @MinNumber(1)
|
||||
private int towers = 4;
|
||||
@Ref(kind = Keep.class)
|
||||
@ResourceRef(Keep.class)
|
||||
private String next = "";
|
||||
}
|
||||
""")
|
||||
@@ -759,7 +759,7 @@ class PackSchemaTest {
|
||||
|
||||
@Feature(id = "example:castle", name = "Castle", key = "castle", targets = Level.BIOME)
|
||||
public final class CastleSettings implements IrisFeature {
|
||||
@Ref(kind = Keep.class)
|
||||
@ResourceRef(Keep.class)
|
||||
private List<String> keeps = List.of("main");
|
||||
}
|
||||
""");
|
||||
@@ -831,16 +831,15 @@ class PackSchemaTest {
|
||||
|
||||
@Test
|
||||
void resourceReferencesNameARegisteredKindOnPlainText() {
|
||||
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @Ref(kind = java.lang.Integer), but"
|
||||
assertLoadFails("Field pack.flora.FloraSettings.keep of " + FLORA_SOURCE + " has @ResourceRef(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;");
|
||||
"@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("Option keep has both @Ref and @ResourceRef; it names either a registry entry or a pack resource",
|
||||
"@Ref(Reference.BLOCK) @ResourceRef(Integer.class) private String keep;");
|
||||
}
|
||||
|
||||
/** A built-in module {@code id} that registers {@code kind}. */
|
||||
|
||||
Reference in new issue
Block a user