diff --git a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/ImportedFeatureStage.java b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/ImportedFeatureStage.java index 1942e34ef..aef1db98f 100644 --- a/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/ImportedFeatureStage.java +++ b/adapters/bukkit/nms/v26_2_R1/src/main/java/art/arcane/iris/core/nms/v26_2_R1/ImportedFeatureStage.java @@ -4,9 +4,7 @@ import art.arcane.iris.engine.object.world.biome.custom.IrisBiomeCustom; import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.framework.NativeFeatureGenerationPolicy; import art.arcane.iris.engine.object.world.biome.IrisBiome; -import art.arcane.iris.engine.object.decoration.IrisDecorationStep; import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.engine.object.decoration.IrisImportedFeatureControl; import art.arcane.iris.spi.IrisLogging; import it.unimi.dsi.fastutil.ints.IntArraySet; import it.unimi.dsi.fastutil.ints.IntSet; @@ -32,6 +30,7 @@ import net.minecraft.world.level.levelgen.placement.PlacedFeature; import java.util.ArrayList; import java.util.Arrays; +import java.util.BitSet; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; @@ -112,22 +111,22 @@ final class ImportedFeatureStage { } private void build(WorldGenLevel level, IrisDimension dimension) { - IrisImportedFeatureControl control; + boolean enabled; try { - control = NativeFeatureGenerationPolicy.control(engine); + enabled = NativeFeatureGenerationPolicy.isEnabled(engine); } catch (RuntimeException error) { IrisLogging.error("Iris could not read importedFeatures for this dimension; features off: " + error); markInert(dimension); return; } - if (!control.shouldGenerateFeatures()) { + if (!enabled) { markInert(dimension); return; } FeatureTable built; try { - built = buildTable(level, control, dimension); + built = buildTable(level, dimension); } catch (Throwable error) { IrisLogging.error("Iris importedFeatures is off for " + dimensionKey() + ": feature table construction failed: " + error); @@ -150,8 +149,7 @@ final class ImportedFeatureStage { inertDimension = dimension; } - private FeatureTable buildTable(WorldGenLevel level, IrisImportedFeatureControl control, - IrisDimension dimension) { + private FeatureTable buildTable(WorldGenLevel level, IrisDimension dimension) { Registry registry = level.registryAccess().lookupOrThrow(Registries.BIOME); Set visibleKeys = visibleBiomeKeys(); // Registry-ordered walk, never a hash-ordered set: FeatureSorter's cycle detection walks this list and @@ -185,9 +183,17 @@ final class ImportedFeatureStage { + ". Remove or reorder the conflicting content, or leave importedFeatures.enabled false."); return null; } - boolean filtered = control.getDisabled() != null && !control.getDisabled().isEmpty(); - return new FeatureTable(dimension, control, List.copyOf(biomes), Set.copyOf(biomes), - Map.copyOf(byKey), steps, Map.copyOf(derivatives), filtered); + return new FeatureTable(dimension, List.copyOf(biomes), Set.copyOf(biomes), + Map.copyOf(byKey), steps, Map.copyOf(derivatives), generatedSteps(steps.size())); + } + + /** The decoration steps whose features generate, by the platform's step index. */ + private BitSet generatedSteps(int stepCount) { + BitSet generated = new BitSet(stepCount); + for (int stepIndex = 0; stepIndex < stepCount; stepIndex++) { + generated.set(stepIndex, NativeFeatureGenerationPolicy.shouldGenerateStep(engine, stepIndex)); + } + return generated; } /** @@ -290,7 +296,7 @@ final class ImportedFeatureStage { try { for (int stepIndex = 0; stepIndex < steps.size(); stepIndex++) { - if (!table.control().shouldGenerateStep(IrisDecorationStep.byOrdinal(stepIndex))) { + if (!table.generatedSteps().get(stepIndex)) { continue; } placeStep(level, table, steps.get(stepIndex), featureRegistry, chunkBiomes, owner, @@ -327,11 +333,9 @@ final class ImportedFeatureStage { Arrays.sort(featureIndices); for (int globalIndex : featureIndices) { PlacedFeature feature = stepData.features().get(globalIndex); - if (table.filtered()) { - Identifier featureId = featureRegistry.getKey(feature); - if (featureId != null && !table.control().shouldGenerate(featureId.toString())) { - continue; - } + Identifier featureId = featureRegistry.getKey(feature); + if (featureId != null && !NativeFeatureGenerationPolicy.shouldGenerate(engine, featureId.toString())) { + continue; } random.setFeatureSeed(decorationSeed, globalIndex, stepIndex); level.setCurrentlyGenerating(() -> describeFeature(featureRegistry, feature)); @@ -390,10 +394,10 @@ final class ImportedFeatureStage { return identifier == null ? null : identifier.toString().toLowerCase(Locale.ROOT); } - private record FeatureTable(IrisDimension dimension, IrisImportedFeatureControl control, + private record FeatureTable(IrisDimension dimension, List> biomes, Set> biomeSet, Map> byKey, List steps, - Map> derivatives, boolean filtered) { + Map> derivatives, BitSet generatedSteps) { } } diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedImportedFeatureStage.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedImportedFeatureStage.java index e501ef677..10c599f6e 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedImportedFeatureStage.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedImportedFeatureStage.java @@ -23,8 +23,6 @@ import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.framework.GenerationSessionLease; import art.arcane.iris.engine.framework.NativeFeatureGenerationPolicy; import art.arcane.iris.engine.object.world.biome.IrisBiome; -import art.arcane.iris.engine.object.decoration.IrisDecorationStep; -import art.arcane.iris.engine.object.decoration.IrisImportedFeatureControl; import art.arcane.iris.util.project.context.IrisContext; import it.unimi.dsi.fastutil.ints.IntArraySet; import it.unimi.dsi.fastutil.ints.IntSet; @@ -49,6 +47,7 @@ import net.minecraft.world.level.levelgen.placement.PlacedFeature; import java.util.ArrayList; import java.util.Arrays; +import java.util.BitSet; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; @@ -172,23 +171,23 @@ final class ModdedImportedFeatureStage { } private void build(Engine engine, long generation) { - IrisImportedFeatureControl control; + boolean enabled; try { - control = NativeFeatureGenerationPolicy.control(engine); + enabled = NativeFeatureGenerationPolicy.isEnabled(engine); } catch (RuntimeException error) { ModdedIrisLog.error("Iris could not read importedFeatures for this dimension; features off: {}", error.toString()); markInert(generation); return; } - if (!control.shouldGenerateFeatures()) { + if (!enabled) { markInert(generation); return; } FeatureTable built; try (GenerationSessionLease lease = engine.acquireGenerationLease("modded_imported_features"); IrisContext.Scope ignored = IrisContext.open(engine, lease.sessionId(), null)) { - built = buildTable(engine, control, generation); + built = buildTable(engine, generation); } catch (Throwable error) { ModdedIrisLog.error("Iris importedFeatures is off for {}: feature table construction failed: {}", dimensionKey(engine), error.toString()); @@ -214,7 +213,7 @@ final class ModdedImportedFeatureStage { inertGeneration = generation; } - private FeatureTable buildTable(Engine engine, IrisImportedFeatureControl control, long generation) { + private FeatureTable buildTable(Engine engine, long generation) { // Registry-ordered biome list. FeatureSorter's cycle detection walks it, so an unordered list makes // detection depend on JVM hash order and turns a real cycle into an intermittent one. List> biomes = biomeSource.orderedPossibleBiomes(); @@ -248,9 +247,17 @@ final class ModdedImportedFeatureStage { dimensionKey(engine), message); return null; } - boolean filtered = control.getDisabled() != null && !control.getDisabled().isEmpty(); - return new FeatureTable(generation, control, List.copyOf(biomes), Set.copyOf(biomes), - Map.copyOf(byKey), steps, Map.copyOf(derivatives), filtered); + return new FeatureTable(generation, List.copyOf(biomes), Set.copyOf(biomes), + Map.copyOf(byKey), steps, Map.copyOf(derivatives), generatedSteps(engine, steps.size())); + } + + /** The decoration steps whose features generate, by the platform's step index. */ + private static BitSet generatedSteps(Engine engine, int stepCount) { + BitSet generated = new BitSet(stepCount); + for (int stepIndex = 0; stepIndex < stepCount; stepIndex++) { + generated.set(stepIndex, NativeFeatureGenerationPolicy.shouldGenerateStep(engine, stepIndex)); + } + return generated; } /** @@ -324,10 +331,10 @@ final class ModdedImportedFeatureStage { try { for (int stepIndex = 0; stepIndex < steps.size(); stepIndex++) { - if (!table.control().shouldGenerateStep(IrisDecorationStep.byOrdinal(stepIndex))) { + if (!table.generatedSteps().get(stepIndex)) { continue; } - placeStep(level, table, steps.get(stepIndex), featureRegistry, chunkBiomes, owner, + placeStep(level, engine, table, steps.get(stepIndex), featureRegistry, chunkBiomes, owner, random, decorationSeed, origin, stepIndex); } } catch (Throwable error) { @@ -340,7 +347,7 @@ final class ModdedImportedFeatureStage { WorldCheckFeaturePlacement.recordPlacementPass(); } - private void placeStep(WorldGenLevel level, FeatureTable table, FeatureSorter.StepFeatureData stepData, + private void placeStep(WorldGenLevel level, Engine engine, FeatureTable table, FeatureSorter.StepFeatureData stepData, Registry featureRegistry, Set> chunkBiomes, IrisModdedChunkGenerator owner, WorldgenRandom random, long decorationSeed, BlockPos origin, int stepIndex) { @@ -363,11 +370,9 @@ final class ModdedImportedFeatureStage { Arrays.sort(featureIndices); for (int globalIndex : featureIndices) { PlacedFeature feature = stepData.features().get(globalIndex); - if (table.filtered()) { - Identifier featureId = featureRegistry.getKey(feature); - if (featureId != null && !table.control().shouldGenerate(featureId.toString())) { - continue; - } + Identifier featureId = featureRegistry.getKey(feature); + if (featureId != null && !NativeFeatureGenerationPolicy.shouldGenerate(engine, featureId.toString())) { + continue; } random.setFeatureSeed(decorationSeed, globalIndex, stepIndex); level.setCurrentlyGenerating(() -> describeFeature(featureRegistry, feature)); @@ -423,10 +428,10 @@ final class ModdedImportedFeatureStage { return identifier == null ? null : identifier.toString().toLowerCase(Locale.ROOT); } - private record FeatureTable(long generation, IrisImportedFeatureControl control, + private record FeatureTable(long generation, List> biomes, Set> biomeSet, Map> byKey, List steps, - Map> derivatives, boolean filtered) { + Map> derivatives, BitSet generatedSteps) { } } diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleHost.java b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleHost.java index 565107f46..5c3591120 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleHost.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleHost.java @@ -63,7 +63,7 @@ public final class IrisModuleHost { * {@code IrisModule/builtin-modules.txt}) instead of being compiled into Iris. A pack loads them from its {@code modules} * folder, and {@link MissingBuiltInModules} warns when it loads none; a test keeps this equal to the jar's list. */ - public static final List JAR_MODULE_IDS = List.of("iris:post", "iris:perfection", "iris:matter"); + public static final List JAR_MODULE_IDS = List.of("iris:post", "iris:perfection", "iris:matter", "iris:vanilla-features"); /** Iris's pack object model: built-in settings may hold these classes, which Iris reads and describes itself. */ public static final String HOST_TYPE_PACKAGE = "art.arcane.iris.engine.object"; private static final String[] JAR_COPY_FOLDER = {"cache", "module-jars"}; diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java index ad66cbb1e..d6ec8a595 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java @@ -33,6 +33,7 @@ import com.volmit.service.Loot; import com.volmit.service.ObjectsAt; import com.volmit.service.SaplingGrowth; import com.volmit.service.StructureLocator; +import com.volmit.service.VanillaFeatures; import com.volmit.world.BlockPosition; import com.volmit.world.PackWorld; import java.util.ArrayList; @@ -84,6 +85,14 @@ public final class ModuleServices { .orElseGet(() -> new CaveBiomeAnswers(owner, null, owner.getGeneration().world()))); } + /** What the modules say about the platform's own placed features in the engine's world, kept for the world. */ + public static VanillaFeatureAnswers vanillaFeatures(Engine engine) { + return engine.getGeneration().state(VanillaFeatureAnswers.class, owner -> new VanillaFeatureAnswers( + providers(owner, VanillaFeatures.class).stream() + .map(provider -> new VanillaFeatureAnswers.Provider(provider.service(), provider.world())) + .toList())); + } + /** The objects every module would place in the chunk at {@code chunkX}, {@code chunkZ}, guessed without generating it. */ public static Set objectsAt(Engine engine, int chunkX, int chunkZ) { Set keys = new LinkedHashSet<>(); diff --git a/core/src/main/java/art/arcane/iris/core/modules/VanillaFeatureAnswers.java b/core/src/main/java/art/arcane/iris/core/modules/VanillaFeatureAnswers.java new file mode 100644 index 000000000..bf28c0258 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/VanillaFeatureAnswers.java @@ -0,0 +1,99 @@ +/* + * 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 . + */ + +package art.arcane.iris.core.modules; + +import com.volmit.resource.ResourceId; +import com.volmit.service.VanillaFeatures; +import com.volmit.vanilla.DecorationStep; +import com.volmit.world.PackWorld; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Predicate; + +/** + * What the world's {@link VanillaFeatures} providers say, kept for the world: whether the platform's placed features run + * over its terrain, and which decoration steps and features they leave out. Each answer is asked once per world. + */ +public final class VanillaFeatureAnswers { + private static final DecorationStep[] STEPS = DecorationStep.values(); + + /** One provider and the world as its module sees it. */ + record Provider(VanillaFeatures service, PackWorld world) { + } + + private final List providers; + private final boolean generates; + private final Map steps = new ConcurrentHashMap<>(); + private final Map features = new ConcurrentHashMap<>(); + + VanillaFeatureAnswers(List providers) { + this.providers = List.copyOf(providers); + this.generates = this.providers.stream().anyMatch(provider -> provider.service().generates(provider.world())); + } + + /** Whether the platform runs its placed features over the world's terrain: some module turned them on. */ + public boolean generates() { + return generates; + } + + /** + * Whether the platform places the features of its decoration step {@code stepIndex}, counted in the platform's order + * (a step past the ones IrisApi names is asked as an unknown step). False while the features are off. + */ + public boolean generatesStep(int stepIndex) { + if (!generates) { + return false; + } + return steps.computeIfAbsent(stepIndex, index -> everyProviderAllows(provider -> + provider.service().generatesStep(provider.world(), stepAt(index)))); + } + + /** Whether the platform places the feature {@code featureKey}; false while the features are off and for a key that is no registry key. */ + public boolean generatesFeature(String featureKey) { + if (!generates || featureKey == null || featureKey.isBlank()) { + return false; + } + Boolean known = features.get(featureKey); + if (known != null) { + return known; + } + return features.computeIfAbsent(featureKey, key -> registryId(key) + .map(feature -> everyProviderAllows(provider -> provider.service().generatesFeature(provider.world(), feature))) + .orElse(false)); + } + + private boolean everyProviderAllows(Predicate allows) { + return providers.stream().allMatch(allows); + } + + private static Optional stepAt(int index) { + return index >= 0 && index < STEPS.length ? Optional.of(STEPS[index]) : Optional.empty(); + } + + private static Optional registryId(String key) { + try { + return Optional.of(ResourceId.parse(key.trim())); + } catch (IllegalArgumentException notAnId) { + return Optional.empty(); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/framework/NativeFeatureGenerationPolicy.java b/core/src/main/java/art/arcane/iris/engine/framework/NativeFeatureGenerationPolicy.java index cfd7ac8a7..94d96eb3d 100644 --- a/core/src/main/java/art/arcane/iris/engine/framework/NativeFeatureGenerationPolicy.java +++ b/core/src/main/java/art/arcane/iris/engine/framework/NativeFeatureGenerationPolicy.java @@ -18,75 +18,39 @@ package art.arcane.iris.engine.framework; -import art.arcane.iris.engine.object.decoration.IrisDecorationStep; -import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.engine.object.decoration.IrisImportedFeatureControl; -import art.arcane.iris.engine.object.decoration.NativeFeatureGenerationStatus; +import art.arcane.iris.core.modules.ModuleServices; +import art.arcane.iris.core.modules.VanillaFeatureAnswers; import java.util.Objects; /** - * Single decision point for native placed-feature generation, mirroring {@link NativeStructureGenerationPolicy}. - * Both platform generators call only through here so the Bukkit and modded feature passes cannot drift. + * Single decision point for native placed-feature generation, mirroring {@link NativeStructureGenerationPolicy}: what + * the pack's modules say through {@link com.volmit.service.VanillaFeatures}, kept for the engine's world. Both platform + * generators call only through here so the Bukkit and modded feature passes cannot drift. */ public final class NativeFeatureGenerationPolicy { - /** - * Stand-in for a dimension that has no control block. Immutable in practice: nothing here hands it out for - * mutation, and every default in {@link IrisImportedFeatureControl} is "off". - */ - private static final IrisImportedFeatureControl DISABLED = new IrisImportedFeatureControl(); - private NativeFeatureGenerationPolicy() { } /** - * The dimension's control block, or a disabled default when it has none. - *

- * An absent {@code importedFeatures} member deserializes to the field's initializer, but an explicit - * {@code "importedFeatures": null} in dimension JSON overwrites that initializer with null - Gson assigns what - * the document says. This runs on the generation path for every feature decision, so throwing there turns one - * stray null in a pack into a failed chunk rather than a dimension that simply generates no native features. - */ - public static IrisImportedFeatureControl control(Engine engine) { - Engine activeEngine = Objects.requireNonNull(engine, "Native feature policy requires an engine"); - IrisDimension dimension = Objects.requireNonNull(activeEngine.getDimension(), - "Native feature policy requires a bound dimension"); - IrisImportedFeatureControl control = dimension.getImportedFeatures(); - return control == null ? DISABLED : control; - } - - /** - * True when this dimension opted into the native feature pass. Checked before any feature machinery is - * built so a disabled dimension never allocates a feature table. + * True when a module turned the native feature pass on for this world. Checked before any feature machinery is + * built so a world without it never allocates a feature table. */ public static boolean isEnabled(Engine engine) { - return control(engine).shouldGenerateFeatures(); + return answers(engine).generates(); } - public static NativeFeatureGenerationStatus resolve(Engine engine, String placedFeatureKey, - IrisDecorationStep step) { - return control(engine).resolve(placedFeatureKey, step); + /** Whether the features of the platform's decoration step {@code stepIndex} (in the platform's order) generate. */ + public static boolean shouldGenerateStep(Engine engine, int stepIndex) { + return answers(engine).generatesStep(stepIndex); } - public static boolean shouldGenerateStep(Engine engine, IrisDecorationStep step) { - return control(engine).shouldGenerateStep(step); + /** Whether the placed feature {@code placedFeatureKey}, such as {@code minecraft:ore_diamond}, generates. */ + public static boolean shouldGenerate(Engine engine, String placedFeatureKey) { + return answers(engine).generatesFeature(placedFeatureKey); } - public static String generationStatusMessage(String placedFeatureKey, - NativeFeatureGenerationStatus status) { - String key = placedFeatureKey == null ? "" : placedFeatureKey.trim(); - return switch (Objects.requireNonNull(status, "Native feature status must not be null")) { - case GENERATE_NATIVE -> "Native feature " + key + " generates natively."; - case FEATURES_DISABLED -> "Native feature " + key - + " does not generate because this dimension's importedFeatures.enabled is false."; - case STEP_DISABLED -> "Native feature " + key - + " does not generate because its decoration step is excluded by importedFeatures."; - case DISABLED_BY_PACK -> "Native feature " + key - + " is disabled by this dimension's importedFeatures.disabled list."; - case CYCLE_DEGRADED -> "Native feature " + key - + " does not generate because the registered features could not be ordered " - + "(feature order cycle); importedFeatures was degraded to off for this dimension."; - case INVALID_REGISTRY_KEY -> "Native feature registry key is invalid: " + key; - }; + private static VanillaFeatureAnswers answers(Engine engine) { + return ModuleServices.vanillaFeatures(Objects.requireNonNull(engine, "Native feature policy requires an engine")); } } diff --git a/core/src/test/java/art/arcane/iris/core/modules/BuiltInPack.java b/core/src/test/java/art/arcane/iris/core/modules/BuiltInPack.java index 196de02ef..2c0faba03 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/BuiltInPack.java +++ b/core/src/test/java/art/arcane/iris/core/modules/BuiltInPack.java @@ -21,6 +21,7 @@ package art.arcane.iris.core.modules; import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.framework.SeedManager; +import art.arcane.iris.engine.framework.vanillafeatures.VanillaFeaturesModule; import art.arcane.iris.engine.modifier.MatterModule; import art.arcane.iris.engine.modifier.PerfectionModule; import art.arcane.iris.engine.modifier.PostModule; @@ -51,7 +52,7 @@ public final class BuiltInPack { * with Iris; {@code BuiltInModulesJarTest} loads the same modules from the jar instead. */ public static List jarModules() { - return List.of(new PostModule(), new PerfectionModule(), new MatterModule()); + return List.of(new PostModule(), new PerfectionModule(), new MatterModule(), new VanillaFeaturesModule()); } /** Every built-in module: Iris's own and the compiled-in copies of the jar's ({@link #jarModules()}). */ diff --git a/core/src/test/java/art/arcane/iris/core/modules/VanillaFeaturesSettingsDecodingTest.java b/core/src/test/java/art/arcane/iris/core/modules/VanillaFeaturesSettingsDecodingTest.java new file mode 100644 index 000000000..b7fd4e98b --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/VanillaFeaturesSettingsDecodingTest.java @@ -0,0 +1,112 @@ +/* + * 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 . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.builtin.vanillafeatures.ImportedFeaturesSettings; +import art.arcane.volmlib.util.json.JSONObject; +import com.google.gson.Gson; +import com.volmit.FeatureAttachment.Target; +import com.volmit.host.LoadedPack; +import com.volmit.host.ModuleHost; +import com.volmit.vanilla.DecorationStep; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + +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; + +/** A dimension's {@code importedFeatures} as packs have always written it, read into {@link ImportedFeaturesSettings}. */ +public class VanillaFeaturesSettingsDecodingTest { + private static final String DIMENSION = """ + {"importedFeatures": { + "enabled": true, + "disabled": ["minecraft:ore", "minecraft:trees_plains"], + "steps": ["UNDERGROUND_ORES", "VEGETAL_DECORATION"], + "disabledSteps": ["VEGETAL_DECORATION"] + }} + """; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private LoadedPack pack; + private ModuleSettingsDecoder decoder; + + @Before + public void loadBuiltIns() throws IOException { + Path packFolder = folder.newFolder("pack").toPath(); + pack = LoadedPack.load(packFolder, ModuleHost.of(BuiltInPack.allBuiltIns()) + .describingHostTypesIn(IrisModuleHost.HOST_TYPE_PACKAGE).copyingJarsTo(folder.newFolder("copies").toPath())); + decoder = new ModuleSettingsDecoder(pack.catalog(), packFolder, Gson::new); + } + + @After + public void closePack() { + pack.close(); + } + + @Test + public void everyOptionReadsAsPacksWroteIt() { + ImportedFeaturesSettings settings = settings(DIMENSION); + + assertTrue(settings.enabled()); + assertEquals(List.of("minecraft:ore", "minecraft:trees_plains"), settings.disabled()); + assertEquals(List.of(DecorationStep.UNDERGROUND_ORES, DecorationStep.VEGETAL_DECORATION), settings.steps()); + assertEquals(List.of(DecorationStep.VEGETAL_DECORATION), settings.disabledSteps()); + } + + @Test + public void emptySettingsAreOffAndTheFeatureIdIsNotAKey() { + ImportedFeaturesSettings defaults = settings("{\"importedFeatures\": {}}"); + JSONObject byId = new JSONObject("{\"iris:vanilla-features\": {\"enabled\": true}}"); + + assertFalse(defaults.enabled()); + assertEquals(List.of(), defaults.disabled()); + assertEquals(List.of(), defaults.steps()); + assertEquals(List.of(), defaults.disabledSteps()); + assertSame(ModuleSettings.NONE, decoder.decode(Target.DIMENSION, byId, "dimension overworld")); + assertTrue(decoder.keys(Target.DIMENSION).contains("importedFeatures")); + } + + @Test + public void aBareSwitchIsRefusedNamingTheKey() { + JSONObject bare = new JSONObject("{\"importedFeatures\": true}"); + + IllegalArgumentException refused = assertThrows(IllegalArgumentException.class, + () -> decoder.decode(Target.DIMENSION, bare, "dimension overworld")); + + assertEquals("Cannot read settings iris:vanilla-features under \"importedFeatures\" in dimension overworld:" + + " write the settings as an object, {\"