mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Settings are keyed by feature id and always an object: drop aliases and the one-option bare value
The settings decoder, the studio schema renderer and the schema builder no longer know aliases. Tests and fixtures use the iris:deposit object form.
This commit is contained in:
1 parent
2851e5d304
commit
d6228c3074
10 files changed
+84
-129
No files matched your search
@@ -81,7 +81,6 @@ public final class IrisModuleHost {
|
||||
private static ModuleHost build() {
|
||||
return ModuleHost.of(builtInModules())
|
||||
.exposing(BUILT_IN_API_PACKAGE)
|
||||
.reservingFields(ModuleTargets.reservedFields())
|
||||
.describingHostTypesIn(HOST_TYPE_PACKAGE)
|
||||
.reservingFolders(IrisData.ownFolderNames().toArray(String[]::new))
|
||||
.copyingJarsTo(jarCopyRoot())
|
||||
|
||||
@@ -31,7 +31,6 @@ import com.google.gson.stream.JsonWriter;
|
||||
import com.volmit.FeatureAttachment.Target;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.config.OptionsMetadata;
|
||||
import com.volmit.config.Snippet;
|
||||
import com.volmit.host.FeatureCatalog;
|
||||
import com.volmit.host.FeatureDescriptor;
|
||||
@@ -46,7 +45,6 @@ import java.util.EnumMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Function;
|
||||
@@ -55,14 +53,13 @@ import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Reads the module settings a dimension, region or biome declares: each settings feature of the pack at
|
||||
* that target, under its id or, for built-ins, its alias. Settings are created with their no-argument
|
||||
* that target, under its id. Settings are created with their no-argument
|
||||
* constructor and the JSON is read into them, so an omitted option 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.
|
||||
* <p>
|
||||
* Settings with exactly one option may be written as that option's value: {@code "snow": 0.5} reads as
|
||||
* {@code "snow": {"amount": 0.5}}. Iris's own classes that built-in settings hold (see
|
||||
* Settings are always written as their object. Iris's own classes that built-in settings hold (see
|
||||
* {@link IrisModuleHost#isHostType}) are read by Iris's pack Gson, exactly as Iris reads them elsewhere.
|
||||
*/
|
||||
public final class ModuleSettingsDecoder {
|
||||
@@ -92,7 +89,7 @@ public final class ModuleSettingsDecoder {
|
||||
}
|
||||
}
|
||||
|
||||
/** Every JSON key the pack's modules read at {@code target}: feature ids and built-in aliases, object generators' included. */
|
||||
/** Every JSON key the pack's modules read at {@code target}: the feature ids, object generators' included. */
|
||||
public Set<String> keys(Target target) {
|
||||
return keys.get(target);
|
||||
}
|
||||
@@ -101,13 +98,14 @@ public final class ModuleSettingsDecoder {
|
||||
* Decodes the settings {@code json} declares for {@code target}.
|
||||
*
|
||||
* @param resource names the file in errors, such as {@code biome desert/dunes}
|
||||
* @throws IllegalArgumentException when the JSON holds both a feature's alias and its id, or a value
|
||||
* that does not decode into the settings type
|
||||
* @throws IllegalArgumentException when a value does not decode into its settings type
|
||||
*/
|
||||
public ModuleSettings decode(Target target, JSONObject json, String resource) {
|
||||
Map<Class<? extends IrisFeature>, IrisFeature> values = new LinkedHashMap<>();
|
||||
for (FeatureDescriptor<?> feature : settings.get(target)) {
|
||||
presentKey(feature, json, resource).ifPresent(key -> values.put(feature.type(), read(feature, json.get(key), key, resource)));
|
||||
if (json.has(feature.key())) {
|
||||
values.put(feature.type(), read(feature, json.get(feature.key()), resource));
|
||||
}
|
||||
}
|
||||
return ModuleSettings.of(values);
|
||||
}
|
||||
@@ -127,44 +125,25 @@ public final class ModuleSettingsDecoder {
|
||||
return decoded;
|
||||
}
|
||||
|
||||
private static Optional<String> presentKey(FeatureDescriptor<?> feature, JSONObject json, String resource) {
|
||||
boolean hasKey = json.has(feature.key());
|
||||
Optional<String> alias = feature.alias().filter(json::has);
|
||||
if (hasKey && alias.isPresent()) {
|
||||
throw new IllegalArgumentException(resource + " sets both \"" + alias.get() + "\" and \"" + feature.key()
|
||||
+ "\", the alias and the id of settings " + feature.id() + "; keep one");
|
||||
}
|
||||
return hasKey ? Optional.of(feature.key()) : alias;
|
||||
}
|
||||
|
||||
private IrisFeature read(FeatureDescriptor<?> feature, Object value, String key, String resource) {
|
||||
private IrisFeature read(FeatureDescriptor<?> feature, Object value, String resource) {
|
||||
try {
|
||||
IrisFeature decoded = gson.fromJson(JSONObject.valueToString(asObject(feature, value)), feature.type());
|
||||
requireObjectOrSnippet(feature, value);
|
||||
IrisFeature decoded = gson.fromJson(JSONObject.valueToString(value), feature.type());
|
||||
if (decoded == null) {
|
||||
throw new JsonParseException("the value is null");
|
||||
}
|
||||
return decoded;
|
||||
} catch (RuntimeException failure) {
|
||||
throw new IllegalArgumentException("Cannot read settings " + feature.id() + " under \"" + key + "\" in "
|
||||
throw new IllegalArgumentException("Cannot read settings " + feature.id() + " under \"" + feature.key() + "\" in "
|
||||
+ resource + ": " + failure.getMessage(), failure);
|
||||
}
|
||||
}
|
||||
|
||||
/** A bare value of settings with exactly one option, as the object holding it; anything else unchanged. */
|
||||
private static Object asObject(FeatureDescriptor<?> feature, Object value) {
|
||||
if (value instanceof JSONObject || isSnippetReference(feature, value)) {
|
||||
return value;
|
||||
private static void requireObjectOrSnippet(FeatureDescriptor<?> feature, Object value) {
|
||||
if (value instanceof JSONObject || (value instanceof String && feature.type().isAnnotationPresent(Snippet.class))) {
|
||||
return;
|
||||
}
|
||||
List<OptionsMetadata.Option> options = feature.options().options();
|
||||
if (options.size() != 1) {
|
||||
throw new JsonParseException("write an object; only settings with exactly one option may be written as that"
|
||||
+ " option's value, and these have " + options.size());
|
||||
}
|
||||
return new JSONObject().put(options.getFirst().name(), value);
|
||||
}
|
||||
|
||||
private static boolean isSnippetReference(FeatureDescriptor<?> feature, Object value) {
|
||||
return feature.type().isAnnotationPresent(Snippet.class) && value instanceof String text && text.startsWith(SNIPPET_PREFIX);
|
||||
throw new JsonParseException("write the settings as an object, {\"<option>\": ...}");
|
||||
}
|
||||
|
||||
private static List<FeatureDescriptor<?>> settingsAt(FeatureCatalog catalog, Target target) {
|
||||
@@ -178,10 +157,7 @@ public final class ModuleSettingsDecoder {
|
||||
Set<String> found = new TreeSet<>();
|
||||
Stream.concat(catalog.settings().stream(), objects)
|
||||
.filter(feature -> feature.attachment().targets().contains(target))
|
||||
.forEach(feature -> {
|
||||
found.add(feature.key());
|
||||
feature.alias().ifPresent(found::add);
|
||||
});
|
||||
.forEach(feature -> found.add(feature.key()));
|
||||
return Set.copyOf(found);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,17 +18,14 @@
|
||||
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.JsonFieldNames;
|
||||
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
|
||||
import art.arcane.iris.engine.object.world.region.IrisRegion;
|
||||
import com.volmit.FeatureAttachment.Target;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Which Iris class holds the JSON of each attachment target that module settings can appear in.
|
||||
@@ -50,11 +47,4 @@ public final class ModuleTargets {
|
||||
.map(Map.Entry::getKey)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** Iris's own JSON fields per target, which no built-in alias may take. */
|
||||
static Map<Target, Set<String>> reservedFields() {
|
||||
Map<Target, Set<String>> fields = new EnumMap<>(Target.class);
|
||||
HOLDERS.forEach((target, holder) -> fields.put(target, JsonFieldNames.of(holder)));
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
@@ -38,10 +38,9 @@ import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Renders the settings a pack's modules add, described by IrisApi as a {@link PackSchema}, into the JSON
|
||||
* schema studio writes: each feature as a property of its target under its id and, for built-ins, its
|
||||
* alias, each settings class once as a definition, references as completion lists (resource references as the keys
|
||||
* of the module resource kind they name). Settings with one option also accept that option's bare value; Iris's own
|
||||
* classes in built-in settings use Iris's own definitions, and so do IrisApi's value types, which read the same JSON as
|
||||
* schema studio writes: each feature as a property of its target under its id, each settings class once as a
|
||||
* definition, references as completion lists (resource references as the keys of the module resource kind they
|
||||
* name). Iris's own classes in built-in settings use Iris's own definitions, and so do IrisApi's value types, which read the same JSON as
|
||||
* an Iris class ({@code BlockSpec} as Iris's block data), with the snippet references their {@code @Snippet} allows.
|
||||
*/
|
||||
final class ModuleSchemaRenderer {
|
||||
@@ -90,18 +89,10 @@ final class ModuleSchemaRenderer {
|
||||
this.snippetFiles = snippetFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds every feature at {@code target} to {@code properties} under its id and alias, and to
|
||||
* {@code constraints} that one JSON object may not hold both.
|
||||
*/
|
||||
void addFeatures(Target target, JSONObject properties, JSONArray constraints) {
|
||||
/** Adds every feature at {@code target} to {@code properties} under its id. */
|
||||
void addFeatures(Target target, JSONObject properties) {
|
||||
for (FeatureSchema feature : schema.fields(target)) {
|
||||
JSONObject property = featureProperty(feature);
|
||||
properties.put(feature.key(), property);
|
||||
feature.alias().ifPresent(alias -> {
|
||||
properties.put(alias, property);
|
||||
constraints.put(notBoth(alias, feature.key()));
|
||||
});
|
||||
properties.put(feature.key(), featureProperty(feature));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,27 +106,15 @@ final class ModuleSchemaRenderer {
|
||||
return file;
|
||||
}
|
||||
|
||||
/** The settings object, or with exactly one option also that option's bare value. */
|
||||
private JSONObject featureProperty(FeatureSchema feature) {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("$ref", DEFINITION_REF + definition(feature.settings().name()));
|
||||
JSONObject property = feature.shorthand()
|
||||
.map(option -> new JSONObject().put("anyOf", new JSONArray(List.of(object, optionSchema(option)))))
|
||||
.orElse(object);
|
||||
JSONObject property = new JSONObject();
|
||||
property.put("$ref", DEFINITION_REF + definition(feature.settings().name()));
|
||||
String description = feature.name() + " (" + feature.id() + "), from module " + feature.module().name()
|
||||
+ " (" + feature.module().id() + ")" + (feature.settings().description().isEmpty() ? "" : ". " + feature.settings().description());
|
||||
property.put("description", description);
|
||||
return property;
|
||||
}
|
||||
|
||||
private static JSONObject notBoth(String alias, String key) {
|
||||
JSONObject both = new JSONObject();
|
||||
both.put("required", new JSONArray(List.of(alias, key)));
|
||||
JSONObject not = new JSONObject();
|
||||
not.put("not", both);
|
||||
return not;
|
||||
}
|
||||
|
||||
/** The definition key of a settings class, built the first time a property names it. */
|
||||
private String definition(String typeName) {
|
||||
String key = DEFINITION_PREFIX + typeName.replace('.', '-').replace('$', '-').toLowerCase(Locale.ROOT);
|
||||
|
||||
@@ -372,7 +372,7 @@ public class SchemaBuilder {
|
||||
}
|
||||
|
||||
buildProperties(properties, required, extended, c);
|
||||
addModuleFeatures(c, properties, extended);
|
||||
addModuleFeatures(c, properties);
|
||||
|
||||
if (required.length() > 0) {
|
||||
o.put("required", required);
|
||||
@@ -388,8 +388,8 @@ public class SchemaBuilder {
|
||||
}
|
||||
|
||||
/** The pack modules' settings at the target whose JSON {@code c} is, such as a biome's. */
|
||||
private void addModuleFeatures(Class<?> c, JSONObject properties, JSONArray constraints) {
|
||||
ModuleTargets.ofResource(c).ifPresent(target -> moduleSchemas().addFeatures(target, properties, constraints));
|
||||
private void addModuleFeatures(Class<?> c, JSONObject properties) {
|
||||
ModuleTargets.ofResource(c).ifPresent(target -> moduleSchemas().addFeatures(target, properties));
|
||||
}
|
||||
|
||||
private ModuleSchemaRenderer moduleSchemas() {
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.volmit.config.PackSettings;
|
||||
import com.volmit.host.LoadedPack;
|
||||
import com.volmit.host.ResolvedGraph;
|
||||
import com.volmit.host.schema.FeatureSchema;
|
||||
import com.volmit.host.schema.SchemaOption;
|
||||
import com.volmit.host.schema.ValueKind;
|
||||
import com.volmit.stage.MantleStage;
|
||||
import com.volmit.stage.Stage;
|
||||
@@ -91,14 +92,16 @@ public class BuiltInGenerationGraphTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depositsAreSettingsOfEveryLevelWrittenAsTheirOldList() throws IOException {
|
||||
public void depositsAreSettingsOfEveryLevelWrittenAsTheirIdWithGenerators() throws IOException {
|
||||
try (LoadedPack pack = BuiltInPack.serve(mock(IrisData.class), folder, List.of())) {
|
||||
for (Target level : List.of(Target.DIMENSION, Target.REGION, Target.BIOME)) {
|
||||
FeatureSchema deposits = pack.schema().fields(level).stream()
|
||||
.filter(feature -> feature.settings().name().equals(DepositSettings.class.getName()))
|
||||
.findFirst().orElseThrow();
|
||||
assertEquals("deposits", deposits.alias().orElseThrow());
|
||||
ValueKind generators = deposits.shorthand().orElseThrow().kind();
|
||||
assertEquals("iris:deposit", deposits.key());
|
||||
SchemaOption option = deposits.settings().options().getFirst();
|
||||
assertEquals("generators", option.name());
|
||||
ValueKind generators = option.kind();
|
||||
assertEquals("generators are a module class of IrisApi types, not an Iris class",
|
||||
new ValueKind.ListKind(new ValueKind.ObjectKind(DepositGenerator.class.getName()), false), generators);
|
||||
}
|
||||
|
||||
@@ -27,15 +27,17 @@ import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* The deposits packs have always written read into {@link DepositGenerator}, a settings class of IrisApi types only,
|
||||
* The deposit generators packs have always written, now under {@code "iris:deposit": {"generators": [...]}}, read into {@link DepositGenerator}, a settings class of IrisApi types only,
|
||||
* with the values Iris's own deposit generator class read from the same JSON.
|
||||
*/
|
||||
public class DepositSettingsDecodingTest {
|
||||
private static final String REGION_DEPOSITS = """
|
||||
{"deposits": [
|
||||
{"iris:deposit": {"generators": [
|
||||
{"minHeight": 15, "maxPerChunk": 26, "maxHeight": 410, "minPerChunk": 4, "minSize": 3, "maxSize": 8,
|
||||
"palette": [{"block": "minecraft:iron_ore"}, {"block": "minecraft:coal_ore"}], "varience": 4},
|
||||
{"heightDistribution": "TRIANGLE", "placementScope": "FULL_HEIGHT", "surfaceClearance": 0, "shape": "VANILLA_ELLIPSOID",
|
||||
@@ -45,7 +47,7 @@ public class DepositSettingsDecodingTest {
|
||||
"snippet/deposit/diamonds",
|
||||
"snippet/diamonds",
|
||||
{}
|
||||
]}
|
||||
]}}
|
||||
""";
|
||||
|
||||
@Rule
|
||||
@@ -140,10 +142,20 @@ public class DepositSettingsDecodingTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theFeatureIdReadsTheSameAsTheOldKey() {
|
||||
String byId = "{\"iris:deposit\": {\"generators\": [{\"maxHeight\": 16}]}}";
|
||||
public void theOldBareListUnderTheOldKeyIsNotSettings() {
|
||||
JSONObject old = new JSONObject("{\"deposits\": [{\"maxHeight\": 16}]}");
|
||||
|
||||
assertEquals(16, deposits(byId).getFirst().maxHeight());
|
||||
assertSame(ModuleSettings.NONE, decoder.decode(Target.REGION, old, "region forests"));
|
||||
assertFalse(decoder.keys(Target.REGION).contains("deposits"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aBareListUnderTheFeatureIdIsRefused() {
|
||||
JSONObject bare = new JSONObject("{\"iris:deposit\": [{\"maxHeight\": 16}]}");
|
||||
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class, () -> decoder.decode(Target.REGION, bare, "region forests"));
|
||||
|
||||
assertTrue(refused.getMessage(), refused.getMessage().contains("write the settings as an object"));
|
||||
}
|
||||
|
||||
private List<DepositGenerator> deposits(String regionJson) {
|
||||
|
||||
@@ -65,7 +65,7 @@ public class ModuleChunkGenerationTest {
|
||||
private Engine engine;
|
||||
private Hunk<PlatformBlockState> blocks;
|
||||
|
||||
@Feature(id = "iris:pillar", name = "Pillar", alias = "pillar", targets = {Target.BIOME, Target.REGION, Target.DIMENSION})
|
||||
@Feature(id = "iris:pillar", name = "Pillar", targets = {Target.BIOME, Target.REGION, Target.DIMENSION})
|
||||
public static final class PillarSettings implements IrisFeature {
|
||||
private BlockState block = BlockState.parse("minecraft:stone");
|
||||
private int x;
|
||||
|
||||
@@ -70,7 +70,7 @@ public class ModuleSettingsDecoderTest {
|
||||
private LoadedPack pack;
|
||||
private ModuleSettingsDecoder decoder;
|
||||
|
||||
@Feature(id = "iris:moss", name = "Moss", alias = "moss", targets = {Target.BIOME, Target.REGION})
|
||||
@Feature(id = "iris:moss", name = "Moss", targets = {Target.BIOME, Target.REGION})
|
||||
public static final class MossSettings implements IrisFeature {
|
||||
private int radius = 3;
|
||||
private BlockState block = new BlockState("minecraft:moss_block");
|
||||
@@ -83,12 +83,12 @@ public class ModuleSettingsDecoderTest {
|
||||
private int size = 1;
|
||||
}
|
||||
|
||||
@Feature(id = "iris:moss/cover", name = "Cover", alias = "cover", targets = Target.BIOME)
|
||||
@Feature(id = "iris:moss/cover", name = "Cover", targets = Target.BIOME)
|
||||
public static final class CoverSettings implements IrisFeature {
|
||||
private double amount = 1;
|
||||
}
|
||||
|
||||
@Feature(id = "iris:moss/marks", name = "Marks", alias = "marks", targets = Target.BIOME)
|
||||
@Feature(id = "iris:moss/marks", name = "Marks", targets = Target.BIOME)
|
||||
public static final class MarksSettings implements IrisFeature {
|
||||
private List<IrisPosition> marks = List.of();
|
||||
}
|
||||
@@ -141,31 +141,27 @@ public class ModuleSettingsDecoderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsSettingsUnderTheirIdOrBuiltInAliasKeepingOmittedDefaults() {
|
||||
public void readsSettingsUnderTheirIdKeepingOmittedDefaults() {
|
||||
MossSettings byId = moss("{\"iris:moss\": {\"radius\": 7, \"block\": \"minecraft:oak_log[axis=y]\"}}");
|
||||
MossSettings byAlias = moss("{\"moss\": {\"palette\": \"iris:moss/wet\"}}");
|
||||
MossSettings partial = moss("{\"iris:moss\": {\"palette\": \"iris:moss/wet\"}}");
|
||||
|
||||
assertEquals(7, byId.radius);
|
||||
assertEquals(BlockState.parse("minecraft:oak_log[axis=y]"), byId.block);
|
||||
assertEquals(ResourceId.parse("iris:moss/default"), byId.palette);
|
||||
assertEquals(3, byAlias.radius);
|
||||
assertEquals(ResourceId.parse("iris:moss/wet"), byAlias.palette);
|
||||
assertEquals(1, byAlias.patch.size);
|
||||
assertEquals(3, partial.radius);
|
||||
assertEquals(ResourceId.parse("iris:moss/wet"), partial.palette);
|
||||
assertEquals(1, partial.patch.size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void declaresNothingWhereNoKeyIsPresent() {
|
||||
assertSame(ModuleSettings.NONE, decoder.decode(Target.BIOME, new JSONObject("{\"name\": \"Dunes\"}"), "biome desert/dunes"));
|
||||
assertSame(ModuleSettings.NONE, decoder.decode(Target.DIMENSION, new JSONObject("{\"moss\": {}}"), "dimension overworld"));
|
||||
assertSame(ModuleSettings.NONE, decoder.decode(Target.DIMENSION, new JSONObject("{\"iris:moss\": {}}"), "dimension overworld"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bothTheAliasAndTheIdInOneObjectFail() {
|
||||
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
|
||||
() -> moss("{\"moss\": {}, \"iris:moss\": {}}"));
|
||||
|
||||
assertEquals("biome desert/dunes sets both \"moss\" and \"iris:moss\", the alias and the id of settings iris:moss; keep one",
|
||||
failure.getMessage());
|
||||
public void aShortKeyIsNotAKeyOfTheSettings() {
|
||||
assertSame(ModuleSettings.NONE, decoder.decode(Target.BIOME, new JSONObject("{\"moss\": {\"radius\": 7}}"), "biome desert/dunes"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,10 +169,10 @@ public class ModuleSettingsDecoderTest {
|
||||
Path snippets = Files.createDirectories(packFolder.resolve("snippet/moss-patch"));
|
||||
Files.writeString(snippets.resolve("wide.json"), "{\"size\": 9}");
|
||||
|
||||
assertEquals(9, moss("{\"moss\": {\"patch\": \"snippet/wide\"}}").patch.size);
|
||||
assertEquals(9, moss("{\"iris:moss\": {\"patch\": \"snippet/wide\"}}").patch.size);
|
||||
|
||||
IllegalArgumentException missing = assertThrows(IllegalArgumentException.class,
|
||||
() -> moss("{\"moss\": {\"patch\": \"snippet/narrow\"}}"));
|
||||
() -> moss("{\"iris:moss\": {\"patch\": \"snippet/narrow\"}}"));
|
||||
assertTrue(missing.getMessage(), missing.getMessage().contains("Snippet snippet/narrow has no file snippet/moss-patch/narrow.json"));
|
||||
}
|
||||
|
||||
@@ -185,7 +181,7 @@ public class ModuleSettingsDecoderTest {
|
||||
Path snippets = Files.createDirectories(packFolder.resolve("snippet/moss-patch"));
|
||||
Files.writeString(snippets.resolve("wide.json"), "{\"size\": 9}");
|
||||
|
||||
assertEquals(9, moss("{\"moss\": {\"patch\": \"snippet/moss-patch/wide\"}}").patch.size);
|
||||
assertEquals(9, moss("{\"iris:moss\": {\"patch\": \"snippet/moss-patch/wide\"}}").patch.size);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -227,27 +223,28 @@ public class ModuleSettingsDecoderTest {
|
||||
@Test
|
||||
public void badValuesNameTheSettingsKeyAndResource() {
|
||||
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
|
||||
() -> moss("{\"moss\": {\"block\": \"stone\"}}"));
|
||||
() -> moss("{\"iris:moss\": {\"block\": \"stone\"}}"));
|
||||
|
||||
assertTrue(failure.getMessage(), failure.getMessage().startsWith("Cannot read settings iris:moss under \"moss\" in biome desert/dunes: "));
|
||||
assertTrue(failure.getMessage(), failure.getMessage().startsWith("Cannot read settings iris:moss under \"iris:moss\" in biome desert/dunes: "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listsTheKeysModulesReadPerTarget() {
|
||||
assertEquals(Set.of("iris:moss", "moss", "iris:moss/cover", "cover", "iris:moss/marks", "marks", "iris:moss/values"), decoder.keys(Target.BIOME));
|
||||
assertEquals(Set.of("iris:moss", "iris:moss/cover", "iris:moss/marks", "iris:moss/values"), decoder.keys(Target.BIOME));
|
||||
assertEquals(Set.of(), decoder.keys(Target.DIMENSION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void settingsWithOneOptionReadABareValueAsThatOption() {
|
||||
assertEquals(0.5, decoder.decode(Target.BIOME, new JSONObject("{\"cover\": 0.5}"), "biome desert/dunes")
|
||||
.find(CoverSettings.class).orElseThrow().amount, 0);
|
||||
assertEquals(0.25, decoder.decode(Target.BIOME, new JSONObject("{\"cover\": {\"amount\": 0.25}}"), "biome desert/dunes")
|
||||
public void settingsAreAlwaysAnObjectEvenWithOneOption() {
|
||||
assertEquals(0.25, decoder.decode(Target.BIOME, new JSONObject("{\"iris:moss/cover\": {\"amount\": 0.25}}"), "biome desert/dunes")
|
||||
.find(CoverSettings.class).orElseThrow().amount, 0);
|
||||
|
||||
IllegalArgumentException several = assertThrows(IllegalArgumentException.class, () -> moss("{\"moss\": 4}"));
|
||||
assertEquals("Cannot read settings iris:moss under \"moss\" in biome desert/dunes: write an object; only settings with exactly"
|
||||
+ " one option may be written as that option's value, and these have 4", several.getMessage());
|
||||
for (String bare : List.of("0.5", "[1, 2]", "true")) {
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
|
||||
() -> decoder.decode(Target.BIOME, new JSONObject("{\"iris:moss/cover\": " + bare + "}"), "biome desert/dunes"));
|
||||
assertEquals("Cannot read settings iris:moss/cover under \"iris:moss/cover\" in biome desert/dunes: write the settings as an object,"
|
||||
+ " {\"<option>\": ...}", refused.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -267,7 +264,7 @@ public class ModuleSettingsDecoderTest {
|
||||
}).create();
|
||||
ModuleSettingsDecoder withIrisGson = new ModuleSettingsDecoder(pack.catalog(), packFolder, () -> iris);
|
||||
|
||||
List<IrisPosition> marks = withIrisGson.decode(Target.BIOME, new JSONObject("{\"marks\": [\"a\", \"b\"]}"), "biome desert/dunes")
|
||||
List<IrisPosition> marks = withIrisGson.decode(Target.BIOME, new JSONObject("{\"iris:moss/marks\": {\"marks\": [\"a\", \"b\"]}}"), "biome desert/dunes")
|
||||
.find(MarksSettings.class).orElseThrow().marks;
|
||||
|
||||
assertEquals(List.of(marked, marked), marks);
|
||||
|
||||
@@ -256,22 +256,21 @@ public class SchemaBuilderParityTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectPlacementSchemaRendersModuleSettingsUnderTheirIdAndAliasWithTheirShorthand() throws IOException {
|
||||
public void objectPlacementSchemaRendersModuleSettingsUnderTheirId() throws IOException {
|
||||
JSONObject schema = new SchemaBuilder(IrisObjectPlacement.class, structureSchemaData(glowModuleSchema())).construct();
|
||||
JSONObject properties = rootProperties(schema);
|
||||
JSONArray forms = properties.getJSONObject("example:glow").getJSONArray("anyOf");
|
||||
String definition = definitionKey(forms.getJSONObject(0));
|
||||
String definition = definitionKey(properties.getJSONObject("example:glow"));
|
||||
JSONObject level = schema.getJSONObject("definitions").getJSONObject(definition)
|
||||
.getJSONObject("properties").getJSONObject("level");
|
||||
|
||||
assertEquals(definition, definitionKey(properties.getJSONObject("glow").getJSONArray("anyOf").getJSONObject(0)));
|
||||
assertEquals("settings with one option also take its bare value", "integer", forms.getJSONObject(1).getString("type"));
|
||||
assertFalse("no alias property", properties.has("glow"));
|
||||
assertFalse("settings are their object only, no bare value form", properties.getJSONObject("example:glow").has("anyOf"));
|
||||
assertFalse("no alias constraint", schema.has("allOf"));
|
||||
assertEquals("integer", level.getString("type"));
|
||||
assertEquals(1, level.getInt("minimum"));
|
||||
assertEquals(15, level.getInt("maximum"));
|
||||
assertEquals(7, level.getInt("default"));
|
||||
assertEquals("How bright the glow is", level.getString("description"));
|
||||
assertTrue(schema.toString().contains("{\"not\":{\"required\":[\"glow\",\"example:glow\"]}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -699,7 +698,7 @@ public class SchemaBuilderParityTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Feature(id = "example:glow", name = "Glow", alias = "glow", targets = Target.OBJECT_PLACEMENT)
|
||||
@Feature(id = "example:glow", name = "Glow", targets = Target.OBJECT_PLACEMENT)
|
||||
public static final class GlowSettings implements IrisFeature {
|
||||
@com.volmit.config.Desc("How bright the glow is")
|
||||
@com.volmit.config.MinNumber(1)
|
||||
|
||||
Reference in new issue
Block a user