Add the warmUp lifecycle hook and LifecycleRunner.warmUp

This commit is contained in:
piexel committed 2026-10-09 20:22:29 +02:00
1 parent f6c078aade
commit 3c05cfea0a
6 files changed
+97 -5

No files matched your search

+13 -1
View File
@@ -9,7 +9,8 @@ import com.volmit.world.PackWorld;
* register itself.
*
* <p>Order: {@link #packLoaded} for a pack snapshot before the first world using it starts, then
* {@link #worldStarted} before that world generates anything, and {@link #worldStopped} when it stops. A hotload, or a
* {@link #worldStarted} before that world generates anything, then {@link #warmUp} (a slow worldStarted the host may
* run off the main thread), and {@link #worldStopped} when it stops. A hotload, or a
* reload of the pack's resources while worlds run, stops each world of the old snapshot, loads the new snapshot and
* starts its worlds again, with empty state (see {@link PackWorld}). The host calls the hooks of a pack's modules in
* module id order and stops them in reverse, one hook at a time, on the thread that starts or stops the world.
@@ -30,6 +31,17 @@ public interface ModuleLifecycle {
*/
default void worldStarted(PackWorld world) {}
/** {@code world} started: builds the caches that make its first chunks fast, which may be slow. Runs after
* {@link #worldStarted} of every module. The host may run it off the main thread: synchronously for a server's
* worlds, in the background for studio. It waits for every module's warm-up before the world's first generation, so
* a hook that is slow delays that, not the start of the world. A world that stops before its warm-up runs may skip it.
*
* <p>Only build caches in the module's world state ({@link PackWorld#state}): nothing here may change what the world
* generates, because the module also works when the warm-up never ran. A hook that throws does not stop the world;
* the host reports the failure, naming the module, and the cache is built when first needed.
*/
default void warmUp(PackWorld world) {}
/** {@code world} stops: it generates nothing more with this pack snapshot. Its state is still readable here and is
* closed afterwards. The host runs every module's hook even when one throws, then reports the failures.
*/
@@ -13,6 +13,7 @@ import java.util.Objects;
* var lifecycles = new LifecycleRunner(pack.catalog());
* lifecycles.packLoaded(resources, packSettings); // once per pack snapshot, before its first world starts
* lifecycles.worldStarted(world); // before the world generates anything
* lifecycles.warmUp(world); // then, possibly on another thread, before its first generation
* ...
* lifecycles.worldStopped(world); // then close the world's state
* }</pre>
@@ -60,6 +61,30 @@ public final class LifecycleRunner {
}
}
/** Runs every {@code warmUp} hook in order, each even when an earlier one threw; afterwards throws the first
* failure, naming its module, with the later ones suppressed. The caller picks the thread and waits for this
* before the world generates.
*/
public void warmUp(PackWorld world) {
Objects.requireNonNull(world, "world");
IllegalStateException failure = null;
for (var registration : lifecycles) {
try {
registration.lifecycle().warmUp(world);
} catch (RuntimeException warmFailure) {
var named = failed("warming up a world", registration, warmFailure);
if (failure == null) {
failure = named;
} else {
failure.addSuppressed(named);
}
}
}
if (failure != null) {
throw failure;
}
}
/** Runs every {@code worldStopped} hook in reverse order, each even when an earlier one threw; afterwards throws the
* first failure, naming its module, with the later ones suppressed.
*/