Let a settings value read its own JSON forms

@JsonForms(Codec) marks a value packs write in more than one shape, such as a number, a [min, max]
range or a curve object. The host hands its JSON to the codec as plain values and writes it back
through it; it never creates the class or reads its fields, and the schema describes it as any
JSON value. It is the one exception to the decoding contract, so module jars no longer need Gson
(which the plugin relocates) for such values.
This commit is contained in:
piexel committed 2026-11-05 23:00:05 +01:00
1 parent 5e85e1e3b8
commit 321ab5b486
5 files changed
+97 -3

No files matched your search

@@ -2,6 +2,7 @@ package com.volmit.host;
import com.volmit.block.BlockState;
import com.volmit.config.Desc;
import com.volmit.config.JsonForms;
import com.volmit.config.Reference;
import com.volmit.host.schema.TextFormat;
import com.volmit.host.schema.ValueKind;
@@ -42,7 +43,7 @@ import java.util.stream.Stream;
@SuppressWarnings("removal") // host kinds are transitional, until the cut-over
final class ValueKinds {
static final String SUPPORTED_TYPES = "settings fields can be booleans, numbers, String, ResourceId, BlockState, enums, "
+ "lists, sets and arrays of these, Map with String keys, Object for any JSON value and classes with a no-argument constructor";
+ "lists, sets and arrays of these, Map with String keys, Object for any JSON value, @JsonForms classes and classes with a no-argument constructor";
private static final Set<Class<?>> BOOLEANS = Set.of(boolean.class, Boolean.class);
private static final Set<Class<?>> INTEGERS = Set.of(byte.class, short.class, int.class, long.class,
Byte.class, Short.class, Integer.class, Long.class);
@@ -109,7 +110,7 @@ final class ValueKinds {
if (INTEGERS.contains(type)) return new IntegerKind();
if (DECIMALS.contains(type)) return new DecimalKind();
if (type.isEnum()) return enumKind(type);
if (type == Object.class) return new AnyKind();
if (type == Object.class || type.isAnnotationPresent(JsonForms.class)) return new AnyKind();
return objects.apply(type);
}
@@ -512,6 +512,61 @@ class PackSchemaTest {
assertEquals(List.of("palette", "range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
}
@Test
void aClassWithJsonFormsHoldsAnyJsonValueAndIsNeitherCreatedNorWalked() {
var pack = pack("forms");
module("pack.flora.FloraModule", FLORA,
NO_MEMBERS, "registry.registerSettings(FloraSettings.class);")
.source("pack.flora.FloraSettings", """
package pack.flora;
import com.volmit.config.Level;
import com.volmit.IrisFeature;
import com.volmit.config.Feature;
import java.util.*;
@Feature(id = "example:flora", name = "Flora", key = "flora", targets = Level.BIOME)
public final class FloraSettings implements IrisFeature {
private Height height = new Height(4);
private List<Height> steps = List.of();
}
""")
.source("pack.flora.Height", """
package pack.flora;
import com.volmit.config.JsonForms;
@JsonForms(Height.Forms.class)
public final class Height {
private final Thread notASettingsField;
Height(double blocks) {
notASettingsField = null;
}
public static final class Forms implements JsonForms.Codec<Height> {
@Override
public Height read(Object json) {
return new Height((Double) json);
}
@Override
public Object write(Height value) {
return 4.0;
}
}
}
""")
.writeInto(pack, "flora.jar");
var schema = schemaOf(pack, NO_BUILT_INS);
var settings = schema.type("pack.flora.FloraSettings");
assertEquals(new AnyKind(), option(settings, "height").kind());
assertEquals(Optional.empty(), option(settings, "height").defaultValue());
assertEquals(new ListKind(new AnyKind(), false), option(settings, "steps").kind());
assertTrue(schema.types().stream().noneMatch(type -> type.name().equals("pack.flora.Height")), "the host describes no more of it");
}
@Test
void defaultsLeaveOutWhatJsonCannotHold() {
var pack = pack("defaults");