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);
}
}
}
}
@@ -0,0 +1,79 @@
package com.volmit.world;
import com.volmit.data.Codec;
import org.junit.jupiter.api.Test;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class SavedKeyTest {
private static final Codec<String> TEXT = new Codec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, String value) throws IOException {
output.writeUTF(value);
}
@Override
public String read(DataInput input, int storedVersion) throws IOException {
return input.readUTF();
}
};
@Test
void acceptsNamespacedIdsWithNestedPaths() {
assertDoesNotThrow(() -> key("example:spawn/cooldowns"));
assertDoesNotThrow(() -> key("example:book.v2"));
}
@Test
void rejectsIdsThatAreNotNamespaced() {
for (String id : new String[]{"", "book", ":book", "example:", "Example:book", "a:b:c"}) {
assertThrows(IllegalArgumentException.class, () -> key(id));
}
}
@Test
void rejectsIdsThatCouldLeaveTheWorldsStorage() {
for (String id : new String[]{"example:../book", "example:a/../b", "example:./book", "example:/book", "example:book/", "example:a//b"}) {
assertThrows(IllegalArgumentException.class, () -> key(id), id);
}
}
@Test
void rejectsMissingPartsAndPrimitiveTypes() {
assertThrows(NullPointerException.class, () -> new SavedKey<>(null, String.class, TEXT, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", null, TEXT, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", String.class, null, () -> ""));
assertThrows(NullPointerException.class, () -> new SavedKey<>("a:b", String.class, TEXT, null));
Codec<Integer> number = new Codec<>() {
@Override
public int version() {
return 1;
}
@Override
public void write(DataOutput output, Integer value) throws IOException {
output.writeInt(value);
}
@Override
public Integer read(DataInput input, int storedVersion) throws IOException {
return input.readInt();
}
};
assertThrows(IllegalArgumentException.class, () -> new SavedKey<>("a:b", int.class, number, () -> 0));
}
private static SavedKey<String> key(String id) {
return new SavedKey<>(id, String.class, TEXT, () -> "");
}
}