Stop startup only for packs a world uses

- Leave out a pack that cannot load when no world uses it, with a warning naming it
- Fail the compile naming the pack and its worlds when one does
- Leave such packs out of the pinned registry requirements
- Always create the dimension type folder
This commit is contained in:
piexel committed 2026-11-14 18:07:42 +01:00
1 parent ed037ea40b
commit 4a3ce335cf
4 files changed
+317 -23

No files matched your search

@@ -63,6 +63,7 @@ public final class IrisDatapackCompiler {
private static final int INPUT_FINGERPRINT_SCHEMA = 4;
private static final int INPUT_BUFFER_BYTES = 64 * 1024;
private static final int WORLD_PACK_SCAN_DEPTH = 8;
private static final String DIMENSION_TYPE_FOLDER = "data/iris/dimension_type";
private static final Pattern REGISTRY_KEY_PATTERN = Pattern.compile("[a-z0-9_.-]+:[a-z0-9/._-]+");
private static final List<String> INPUT_DIRECTORIES = List.of("dimensions", "biomes", "snippet");
private static final String FLAT_VOID_LEVEL_STEM = """
@@ -210,16 +211,7 @@ public final class IrisDatapackCompiler {
if (aliasContext.retainedContract() != null) {
data.bindGenerationRegistryContract(aliasContext.retainedContract());
}
ResourceLoader<IrisDimension> loader = data.getDimensionLoader();
String[] possibleKeys = loader.getPossibleKeys();
if (possibleKeys == null || possibleKeys.length == 0) {
throw new IOException("Iris pack has no dimension definitions: " + packRoot);
}
for (String possibleKey : possibleKeys) {
IrisDimension dimension = loader.load(possibleKey);
if (dimension == null) {
throw new IOException("Unable to load Iris dimension '" + possibleKey + "' from " + packRoot);
}
for (IrisDimension dimension : loadPack(data).dimensions()) {
collectRegistryRequirements(
requirements,
biomeTags,
@@ -455,6 +447,7 @@ public final class IrisDatapackCompiler {
Map<String, KSet<String>> biomes = new LinkedHashMap<>();
Map<String, List<DimensionCandidate>> dimensions = new LinkedHashMap<>();
Map<Path, GenerationHistory.PackInspection> histories = new LinkedHashMap<>();
PackUse packUse = new PackUse(packRoots, normalizedBindings);
int packCount = 0;
int dimensionCount = 0;
for (File packRoot : packRoots) {
@@ -468,18 +461,8 @@ public final class IrisDatapackCompiler {
if (aliasContext.retainedContract() != null) {
data.bindGenerationRegistryContract(aliasContext.retainedContract());
}
ResourceLoader<IrisDimension> loader = data.getDimensionLoader();
String[] possibleKeys = loader.getPossibleKeys();
if (possibleKeys == null || possibleKeys.length == 0) {
throw new IOException("Iris pack has no dimension definitions: " + packRoot);
}
int installedDimensions = 0;
for (String possibleKey : possibleKeys) {
IrisDimension dimension = loader.load(possibleKey);
if (dimension == null) {
throw new IOException("Unable to load Iris dimension '" + possibleKey + "' from " + packRoot);
}
for (IrisDimension dimension : dimensionsOfUsedPack(packRoot, loadPack(data), packUse)) {
IrisLogging.debug(" Compiling Dimension " + dimension.getLoadFile().getPath());
height.merge(dimension);
dimensions.computeIfAbsent(dimension.getLoadKey(), ignored -> new ArrayList<>())
@@ -514,12 +497,73 @@ public final class IrisDatapackCompiler {
}
IrisDimension.writeShared(datapackRoots, height, adjustVanillaHeight);
createDimensionTypeFolders(datapackRoots);
installRetainedRegistrySources(packRoots, datapackRoots, fixer, histories);
installLevelStemBindings(normalizedBindings, dimensions, datapackRoots);
validateOutputs(datapackRoots, dimensionCount, normalizedBindings.size());
return new CompilationResult(packCount, dimensionCount, countBiomes(biomes));
}
/**
* A pack that cannot load stops the compile when a world uses it, so the server never starts that world without
* its dimension; a pack no world uses is left out with a warning.
*/
private static List<IrisDimension> dimensionsOfUsedPack(File packRoot, LoadedPack pack, PackUse packUse)
throws IOException {
if (pack.loads()) {
return pack.dimensions();
}
List<String> users = packUse.worldsUsing(packRoot, pack.dimensionKeys());
if (!users.isEmpty()) {
throw new IOException("Iris pack " + packRoot + " cannot load (" + pack.refusal() + ") and world(s) "
+ String.join(", ", users) + " use it. Fix the pack, then start the server again.");
}
IrisLogging.warn("Iris pack %s cannot load (%s). No world uses it, so it is left out and the server starts.",
packRoot, pack.refusal());
return List.of();
}
/** Every dimension of the pack, or none with the reason when any of them cannot load; never part of a pack. */
private static LoadedPack loadPack(IrisData data) {
ResourceLoader<IrisDimension> loader = data.getDimensionLoader();
String[] possibleKeys = loader.getPossibleKeys();
if (possibleKeys == null || possibleKeys.length == 0) {
return LoadedPack.refused(List.of(), "it has no dimension definitions");
}
List<String> dimensionKeys = List.of(possibleKeys);
List<IrisDimension> loaded = new ArrayList<>();
for (String key : dimensionKeys) {
IrisDimension dimension;
try {
dimension = loader.load(key);
} catch (RuntimeException failure) {
return LoadedPack.refused(dimensionKeys, "its dimension '" + key + "' failed to load: " + failure.getMessage());
}
if (dimension == null) {
return LoadedPack.refused(dimensionKeys, "its dimension '" + key + "' failed to load; the error above says why");
}
loaded.add(dimension);
}
return new LoadedPack(dimensionKeys, List.copyOf(loaded), null);
}
private record LoadedPack(List<String> dimensionKeys, List<IrisDimension> dimensions, String refusal) {
static LoadedPack refused(List<String> dimensionKeys, String refusal) {
return new LoadedPack(dimensionKeys, List.of(), refusal);
}
boolean loads() {
return refusal == null;
}
}
/** The dimension type folder exists in every compiled datapack, so one built only from left-out packs reads as complete. */
private static void createDimensionTypeFolders(Collection<File> datapackRoots) throws IOException {
for (File datapackRoot : datapackRoots) {
Files.createDirectories(datapackRoot.toPath().resolve(DIMENSION_TYPE_FOLDER));
}
}
public static void installRetainedRegistrySources(List<File> packRoots, Collection<File> datapackRoots,
IDataFixer fixer) throws IOException {
installRetainedRegistrySources(packRoots, datapackRoots, fixer, new LinkedHashMap<>());
@@ -988,7 +1032,7 @@ public final class IrisDatapackCompiler {
if (!Files.isRegularFile(root.resolve("pack.mcmeta"))) {
throw new IOException("Iris datapack metadata was not generated at " + root);
}
if (dimensionCount > 0 && !Files.isDirectory(root.resolve("data/iris/dimension_type"))) {
if (dimensionCount > 0 && !Files.isDirectory(root.resolve(DIMENSION_TYPE_FOLDER))) {
throw new IOException("Iris dimension types were not generated at " + root);
}
if (levelStemCount > 0 && !Files.isDirectory(root.resolve("data/iris/dimension"))) {
@@ -0,0 +1,111 @@
package art.arcane.iris.pack.datapack;
import art.arcane.iris.world.WorldSlotKey;
import art.arcane.iris.world.lifecycle.BukkitWorldConfiguration.IrisGeneratorBinding;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Which worlds use a pack the startup datapack is compiled from. A pack copied into a world's folder
* ({@code <world>/iris/pack} or {@code <world>/iris/generation/epochs/<epoch>/pack}) belongs to that world, configured
* or not. A pack installed in Iris's own folder is used by every bound world that selects one of its dimensions and
* has no copy of its own.
*/
final class PackUse {
private static final String DIMENSIONS_FOLDER = "dimensions";
private static final String WORLD_IRIS_FOLDER = "iris";
private static final String WORLD_PACK_FOLDER = "pack";
private static final String GENERATION_FOLDER = "generation";
private static final String EPOCHS_FOLDER = "epochs";
private final Set<WorldSlotKey> worldsWithOwnPack;
private final List<IrisGeneratorBinding> bindings;
PackUse(Collection<File> packRoots, List<IrisGeneratorBinding> bindings) {
this.worldsWithOwnPack = packRoots.stream()
.map(root -> worldFolderOwning(root.toPath()))
.flatMap(Optional::stream)
.map(PackUse::slotKeyOf)
.flatMap(Optional::stream)
.collect(Collectors.toUnmodifiableSet());
this.bindings = List.copyOf(bindings);
}
/** The names of the worlds that use the pack at {@code packRoot}, which defines {@code dimensions}. */
List<String> worldsUsing(File packRoot, Collection<String> dimensions) {
Optional<Path> ownerFolder = worldFolderOwning(packRoot.toPath());
if (ownerFolder.isPresent()) {
return List.of(nameOfWorld(ownerFolder.get()));
}
return bindings.stream()
.filter(binding -> dimensions.contains(binding.dimension()))
.filter(binding -> !worldsWithOwnPack.contains(binding.worldKey()))
.map(IrisGeneratorBinding::configuredWorldName)
.toList();
}
private String nameOfWorld(Path worldFolder) {
Optional<WorldSlotKey> worldKey = slotKeyOf(worldFolder);
if (worldKey.isEmpty()) {
return nameOf(worldFolder);
}
return bindings.stream()
.filter(binding -> binding.worldKey().equals(worldKey.get()))
.map(IrisGeneratorBinding::configuredWorldName)
.findFirst()
.orElse(worldKey.get().toString());
}
/** The folder of the world whose copy the pack at {@code packRoot} is, or empty for a pack installed outside any world. */
static Optional<Path> worldFolderOwning(Path packRoot) {
Path pack = packRoot.toAbsolutePath().normalize();
if (!WORLD_PACK_FOLDER.equals(nameOf(pack))) {
return Optional.empty();
}
Path parent = pack.getParent();
if (WORLD_IRIS_FOLDER.equals(nameOf(parent))) {
return Optional.ofNullable(parent.getParent());
}
Path epochs = parentOf(parent);
Path generation = parentOf(epochs);
Path iris = parentOf(generation);
if (EPOCHS_FOLDER.equals(nameOf(epochs))
&& GENERATION_FOLDER.equals(nameOf(generation))
&& WORLD_IRIS_FOLDER.equals(nameOf(iris))) {
return Optional.ofNullable(iris.getParent());
}
return Optional.empty();
}
/** The key of a world stored at {@code <level>/dimensions/<namespace>/<key>}, or empty for the level's own folder. */
static Optional<WorldSlotKey> slotKeyOf(Path worldFolder) {
for (Path namespace = parentOf(worldFolder); namespace != null; namespace = namespace.getParent()) {
if (DIMENSIONS_FOLDER.equals(nameOf(namespace.getParent()))) {
return slotKey(nameOf(namespace), namespace.relativize(worldFolder));
}
}
return Optional.empty();
}
private static Optional<WorldSlotKey> slotKey(String namespace, Path world) {
try {
return Optional.of(new WorldSlotKey(namespace, world.toString().replace(File.separatorChar, '/')));
} catch (IllegalArgumentException notAWorldKey) {
return Optional.empty();
}
}
private static Path parentOf(Path path) {
return path == null ? null : path.getParent();
}
private static String nameOf(Path path) {
return path == null || path.getFileName() == null ? null : path.getFileName().toString();
}
}
@@ -588,7 +588,7 @@ public class DefaultPackBootstrapProvisionerTest {
byte[] metadata = Files.readAllBytes(first.datapackRoot().resolve("pack.mcmeta"));
byte[] originalOverworld = Files.readAllBytes(dataDirectory.resolve("packs/overworld/biomes/local.json"));
byte[] originalUnderworld = Files.readAllBytes(dataDirectory.resolve("packs/underworld/biomes/local.json"));
Path invalidPack = dataDirectory.resolve("packs/invalid");
Path invalidPack = root.resolve("dimensions/iris/broken_world/iris/pack");
Files.createDirectories(invalidPack.resolve("dimensions"));
Files.writeString(invalidPack.resolve("dimensions/broken.json"), "{", StandardCharsets.UTF_8);
updateServer = server(
@@ -35,6 +35,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
@@ -388,6 +389,144 @@ public class IrisDatapackCompilerTest {
assertTrue(failure.getMessage().contains("selects missing dimension \"missing\""));
}
@Test
public void leavesOutAPackThatCannotLoadWhenNoWorldUsesIt() throws Exception {
Path workingPack = temporaryFolder.newFolder("unused-refused-working").toPath();
Path refusedPack = temporaryFolder.newFolder("unused-refused-broken").toPath();
Path datapackRoot = temporaryFolder.newFolder("unused-refused-datapack").toPath();
createPack(workingPack, "working", "working_custom");
createRefusedPack(refusedPack, "broken");
IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile(
List.of(workingPack.toFile(), refusedPack.toFile()),
new KList<File>().qadd(datapackRoot.toFile()),
List.of(binding("moon", "working")),
new DataFixerV1217(),
false
);
assertEquals(1, result.packCount());
assertEquals(1, result.dimensionCount());
assertTrue(Files.isRegularFile(datapackRoot.resolve("data/iris/dimension/moon.json")));
}
@Test
public void leftOutPacksStillProduceACompleteDatapack() throws Exception {
Path refusedPack = temporaryFolder.newFolder("only-refused-broken").toPath();
Path datapackRoot = temporaryFolder.newFolder("only-refused-datapack").toPath();
createRefusedPack(refusedPack, "broken");
IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile(
List.of(refusedPack.toFile()),
new KList<File>().qadd(datapackRoot.toFile()),
List.of(),
new DataFixerV1217(),
false
);
assertEquals(0, result.dimensionCount());
assertTrue(Files.isRegularFile(datapackRoot.resolve("pack.mcmeta")));
assertTrue(Files.isDirectory(datapackRoot.resolve("data/iris/dimension_type")));
}
@Test
public void registryRequirementsLeaveOutAPackThatCannotLoad() throws Exception {
Path workingPack = temporaryFolder.newFolder("requirements-working").toPath();
Path refusedPack = temporaryFolder.newFolder("requirements-broken").toPath();
createPack(workingPack, "working", "working_custom");
createRefusedPack(refusedPack, "broken");
DataFixerV1217 fixer = new DataFixerV1217();
Map<String, String> withRefused = IrisDatapackCompiler.computeRegistryRequirements(
List.of(workingPack.toFile(), refusedPack.toFile()),
fixer);
Map<String, String> withoutRefused = IrisDatapackCompiler.computeRegistryRequirements(
List.of(workingPack.toFile()),
fixer);
assertEquals(withoutRefused, withRefused);
}
@Test
public void stopsTheCompileWhenABoundWorldSelectsAPackThatCannotLoad() throws Exception {
Path refusedPack = temporaryFolder.newFolder("bound-refused-broken").toPath();
Path datapackRoot = temporaryFolder.newFolder("bound-refused-datapack").toPath();
createRefusedPack(refusedPack, "broken");
IOException failure = assertThrows(
IOException.class,
() -> IrisDatapackCompiler.compile(
List.of(refusedPack.toFile()),
new KList<File>().qadd(datapackRoot.toFile()),
List.of(binding("moon", "broken")),
new DataFixerV1217(),
false
)
);
assertTrue(failure.getMessage(), failure.getMessage().contains(refusedPack.toString()));
assertTrue(failure.getMessage(), failure.getMessage().contains("world(s) world_iris_moon use it"));
}
@Test
public void stopsTheCompileWhenAWorldsOwnPackCannotLoad() throws Exception {
Path serverRoot = temporaryFolder.newFolder("own-refused-server").toPath();
Path ownPack = serverRoot.resolve("dimensions/iris/moon/iris/pack");
Path datapackRoot = temporaryFolder.newFolder("own-refused-datapack").toPath();
createRefusedPack(ownPack, "broken");
IOException failure = assertThrows(
IOException.class,
() -> IrisDatapackCompiler.compile(
List.of(ownPack.toFile()),
new KList<File>().qadd(datapackRoot.toFile()),
List.of(),
new DataFixerV1217(),
false
)
);
assertTrue(failure.getMessage(), failure.getMessage().contains("world(s) iris:moon use it"));
}
@Test
public void aWorldWithItsOwnPackDoesNotUseAnInstalledPackThatCannotLoad() throws Exception {
Path serverRoot = temporaryFolder.newFolder("own-working-server").toPath();
Path ownPack = serverRoot.resolve("dimensions/iris/moon/iris/pack");
Path installedPack = temporaryFolder.newFolder("own-working-installed").toPath();
Path datapackRoot = temporaryFolder.newFolder("own-working-datapack").toPath();
createPack(ownPack, "shared", "shared_custom");
createRefusedPack(installedPack, "shared");
IrisDatapackCompiler.CompilationResult result = IrisDatapackCompiler.compile(
List.of(installedPack.toFile(), ownPack.toFile()),
new KList<File>().qadd(datapackRoot.toFile()),
List.of(binding("moon", "shared")),
new DataFixerV1217(),
false
);
assertEquals(1, result.dimensionCount());
}
@Test
public void findsTheWorldOwningEachWorldPackLayout() {
Path level = Path.of("/server/world");
Path moon = level.resolve("dimensions/iris/moon");
assertEquals(Optional.of(moon), PackUse.worldFolderOwning(moon.resolve("iris/pack")));
assertEquals(Optional.of(moon), PackUse.worldFolderOwning(moon.resolve("iris/generation/epochs/0001/pack")));
assertEquals(Optional.of(level), PackUse.worldFolderOwning(level.resolve("iris/pack")));
assertEquals(Optional.empty(), PackUse.worldFolderOwning(Path.of("/server/plugins/Iris/packs/pack")));
assertEquals(Optional.of(new WorldSlotKey("iris", "moon")), PackUse.slotKeyOf(moon));
assertEquals(Optional.empty(), PackUse.slotKeyOf(level));
}
private static void createRefusedPack(Path root, String dimensionKey) throws IOException {
Files.createDirectories(root.resolve("dimensions"));
Files.writeString(root.resolve("dimensions").resolve(dimensionKey + ".json"), "{", StandardCharsets.UTF_8);
}
@Test
public void rejectsBindingToConflictingDuplicateDimension() throws Exception {
Path firstPack = temporaryFolder.newFolder("ambiguous-binding-first").toPath();