Read @JsonForms values through their codec; keep a state whose block rejects a value

JsonFormsCodecs reads and writes IrisApi @JsonForms classes as the plain values Gson reads into an
Object, for the module settings decoder (and so every module resource file) and for tests'
TestSettings. IrisBlockRegistry.withProperty returns the state unchanged when the platform does
not accept the value, as IrisApi now defines it.
This commit is contained in:
piexel committed 2026-11-05 23:00:13 +01:00
1 parent 8ca183d0c2
commit d0ca502a56
20 files changed
+171 -5195

No files matched your search

@@ -159,7 +159,11 @@ final class IrisBlockRegistry implements BlockRegistry {
@Override
public BlockState withProperty(BlockState state, String name, String value) {
return apiState(platformState(state).withProperty(name, value));
try {
return apiState(platformState(state).withProperty(name, value));
} catch (IllegalArgumentException notAccepted) {
return state;
}
}
@Override
@@ -0,0 +1,82 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.modules;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.volmit.config.JsonForms;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Reads and writes IrisApi's {@link JsonForms} classes through their codec, as the plain values Gson reads into an
* {@code Object}: maps, lists, {@code Double}s, strings and booleans. Each codec is created once.
*/
public final class JsonFormsCodecs implements TypeAdapterFactory {
private final Map<Class<?>, JsonForms.Codec<?>> codecs = new ConcurrentHashMap<>();
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
JsonForms forms = type.getRawType().getAnnotation(JsonForms.class);
if (forms == null) {
return null;
}
JsonForms.Codec<T> codec = codecOf(forms);
TypeAdapter<Object> plainValues = gson.getAdapter(Object.class);
String typeName = type.getRawType().getSimpleName();
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
plainValues.write(out, codec.write(value));
}
@Override
public T read(JsonReader in) throws IOException {
Object json = plainValues.read(in);
try {
return codec.read(json);
} catch (IllegalArgumentException notAForm) {
throw new JsonParseException(typeName + ": " + notAForm.getMessage(), notAForm);
}
}
}.nullSafe();
}
@SuppressWarnings("unchecked")
private <T> JsonForms.Codec<T> codecOf(JsonForms forms) {
return (JsonForms.Codec<T>) codecs.computeIfAbsent(forms.value(), JsonFormsCodecs::newCodec);
}
private static JsonForms.Codec<?> newCodec(Class<?> type) {
try {
var constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return (JsonForms.Codec<?>) constructor.newInstance();
} catch (ReflectiveOperationException | RuntimeException failure) {
throw new IllegalStateException("Codec " + type.getName() + " cannot be created with its no-argument constructor: " + failure, failure);
}
}
}
@@ -64,7 +64,8 @@ import java.util.stream.Stream;
* 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.
* {@code NoiseStyle} and the rest) are read like settings, an {@code Object} as Gson reads any JSON value, and a
* {@link com.volmit.config.JsonForms} class through its codec ({@link JsonFormsCodecs}).
* <p>
* Settings are written in the one shape their class declares: an object of the class's options or, when the feature
* names a {@code root} option, exactly that option's value. Iris's own classes that built-in settings hold (see
@@ -93,6 +94,7 @@ public final class ModuleSettingsDecoder {
.registerTypeAdapter(ResourceId.class, textAdapter(text -> resourceIdOf(packIds, text), ResourceId::toString))
.registerTypeAdapterFactory(new HostTypes())
.registerTypeAdapterFactory(new SnippetReferences())
.registerTypeAdapterFactory(new JsonFormsCodecs())
.create();
for (Level target : Level.values()) {
settings.put(target, settingsAt(catalog, target));