Add RunningWorld.saved, state a module keeps with the world across restarts

A world's state store is memory only, so Iris offered spawner cooldowns through a
facade to persist them. SavedKey names a value with its codec and initial value;
the host reads it on first ask and writes it when it saves and stops the world.
This commit is contained in:
piexel committed 2026-11-06 11:22:29 +01:00
1 parent 7cc3302c32
commit a0fcb74365
6 files changed
+166 -11

No files matched your search

@@ -42,6 +42,23 @@ public interface RunningWorld {
*/
Optional<PlacementRecord> placementAt(int worldX, int y, int worldZ);
/**
* The value the module keeps with this world across restarts under {@code key}: the host's one instance of it for
* the world, read from the world's storage the first time anyone asks (or {@code key.initial()} when none was
* stored), and written back when the host saves the world and when it stops. Keep what a spawner last did or a
* counter that must outlive the server here, not what can be rebuilt from the pack; per-world caches belong in
* {@link PackWorld#state}.
*
* <p>The module changes the value in place and owns its thread safety. The host writes it while module threads may
* still change it, so {@link com.volmit.data.Codec#write} must read the value safely. A stored payload the codec
* rejects is logged and replaced by {@code key.initial()}; the next save overwrites it. A pack reload or hotload
* stops the world and starts another over the same storage, so the new world reads what the old one wrote when it
* stopped.
*
* @throws IllegalArgumentException if another key with the same id but another type was used for this world
*/
<T> T saved(SavedKey<T> key);
/** The time and weather of the world; empty while the platform has not loaded the world. */
Optional<Sky> sky();
@@ -0,0 +1,35 @@
package com.volmit.world;
import com.volmit.data.Codec;
import java.util.Objects;
import java.util.function.Supplier;
/** The identity and form of one value a module keeps with a running world across restarts
* ({@link RunningWorld#saved}). The id is namespaced like a mantle key and starts with the owning module's id or its
* namespace; the host names its storage after it, so each {@code /} separated segment of the path must be a name, never
* empty, {@code .} or {@code ..}.
*
* @param id the stable storage identity, such as {@code example:moss/patches}
* @param type the value class
* @param codec how the value is stored; its version is written with the value
* @param initial the value for a world that has none stored, or whose stored one the codec rejects
*/
public record SavedKey<T>(String id, Class<T> type, Codec<T> codec, Supplier<T> initial) {
public SavedKey {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(type, "type");
Objects.requireNonNull(codec, "codec");
Objects.requireNonNull(initial, "initial");
if (!id.matches("[a-z0-9_.-]+:[a-z0-9_./-]+")) {
throw new IllegalArgumentException("Expected a namespaced saved ID: " + id);
}
if (type.isPrimitive()) {
throw new IllegalArgumentException("Saved values require a reference type");
}
for (String segment : id.substring(id.indexOf(':') + 1).split("/", -1)) {
if (segment.isEmpty() || segment.equals(".") || segment.equals("..")) {
throw new IllegalArgumentException("A saved ID names files, so its path has no empty, . or .. segment: " + id);
}
}
}
}