feat(ignis): establish durable progression foundations
CI / build (push) Successful in 3m10s
Release / release (push) Failing after 3m50s

This commit is contained in:
dmg committed 2026-09-10 20:15:11 -04:00
commit 7a481f366c
27 files changed
+1213

No files matched your search

@@ -0,0 +1,125 @@
package games.dmg.spigotignis;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/** Versioned data, never Java object deserialization. Call from the persistence worker only. */
public final class FileStateRepository implements StateRepository {
private static final int MAGIC = 0x49474e49;
private static final int VERSION = 1;
private static final int MAX_RECORDS = 100_000;
private final Path path;
public FileStateRepository(Path path) { this.path = path.toAbsolutePath(); }
@Override
public IgnisState load() throws IOException {
if (Files.notExists(path)) return IgnisState.empty();
try (var input = new DataInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
if (input.readInt() != MAGIC || input.readInt() != VERSION) {
throw new IOException("Unsupported Ignis state header or version.");
}
var limits = new ProgressLimits(input.readLong(), input.readLong(), input.readLong());
Map<UUID, PlayerState> players = new HashMap<>();
int count = count(input, MAX_RECORDS);
for (int i = 0; i < count; i++) {
UUID id = uuid(input);
Map<Track, TrackState> tracks = new HashMap<>();
int trackCount = count(input, Track.values().length);
for (int j = 0; j < trackCount; j++) {
Track track = Track.valueOf(input.readUTF());
var state = new TrackState(input.readLong(), flag(input),
flag(input), input.readLong());
if (tracks.putIfAbsent(track, state) != null) throw new IOException("Duplicate track.");
}
Map<UUID, Track> pending = new HashMap<>();
int rewards = count(input, MAX_RECORDS);
for (int j = 0; j < rewards; j++) {
if (pending.putIfAbsent(uuid(input), Track.valueOf(input.readUTF())) != null) {
throw new IOException("Duplicate pending reward.");
}
}
if (players.putIfAbsent(id, new PlayerState(tracks, pending)) != null) {
throw new IOException("Duplicate player.");
}
}
if (input.read() != -1) throw new IOException("Trailing Ignis state data.");
return new IgnisState(limits, players);
} catch (IllegalArgumentException exception) {
throw new IOException("Invalid Ignis state; original file retained.", exception);
}
}
@Override
public void save(IgnisState state) throws IOException {
if (state.players().size() > MAX_RECORDS
|| state.players().values().stream().anyMatch(p -> p.pendingRewards().size() > MAX_RECORDS)) {
throw new IOException("Ignis state exceeds supported record count.");
}
Files.createDirectories(path.getParent());
Path temporary = Files.createTempFile(path.getParent(), ".ignis-", ".tmp");
try {
try (var output = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(temporary)))) {
output.writeInt(MAGIC);
output.writeInt(VERSION);
output.writeLong(state.limits().invisibilityMillis());
output.writeLong(state.limits().pearls());
output.writeLong(state.limits().darknessMillis());
output.writeInt(state.players().size());
for (var entry : state.players().entrySet()) {
uuid(output, entry.getKey());
output.writeInt(entry.getValue().tracks().size());
for (var track : entry.getValue().tracks().entrySet()) {
output.writeUTF(track.getKey().name());
output.writeLong(track.getValue().progress());
output.writeBoolean(track.getValue().unlocked());
output.writeBoolean(track.getValue().activated());
output.writeLong(track.getValue().generation());
}
output.writeInt(entry.getValue().pendingRewards().size());
for (var reward : entry.getValue().pendingRewards().entrySet()) {
uuid(output, reward.getKey());
output.writeUTF(reward.getValue().name());
}
}
}
try (var file = FileChannel.open(temporary, StandardOpenOption.WRITE)) { file.force(true); }
// Deliberately fail rather than fall back to a partial non-atomic replacement.
Files.move(temporary, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} finally {
Files.deleteIfExists(temporary);
}
}
private static boolean flag(DataInputStream input) throws IOException {
int value = input.readUnsignedByte();
if (value > 1) throw new IOException("Invalid state flag.");
return value == 1;
}
private static int count(DataInputStream input, int maximum) throws IOException {
int result = input.readInt();
if (result < 0 || result > maximum) throw new IOException("Invalid record count.");
return result;
}
private static UUID uuid(DataInputStream input) throws IOException {
return new UUID(input.readLong(), input.readLong());
}
private static void uuid(DataOutputStream output, UUID id) throws IOException {
output.writeLong(id.getMostSignificantBits());
output.writeLong(id.getLeastSignificantBits());
}
}
@@ -0,0 +1,39 @@
package games.dmg.spigotignis;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
public record IgnisState(ProgressLimits limits, Map<UUID, PlayerState> players) {
public IgnisState {
Objects.requireNonNull(limits);
players = Map.copyOf(players);
}
public static IgnisState empty() { return new IgnisState(ProgressLimits.defaults(), Map.of()); }
public IgnisState evaluate(ProgressLimits nextLimits) {
Objects.requireNonNull(nextLimits);
Map<UUID, PlayerState> evaluated = new HashMap<>();
players.forEach((id, player) -> {
Map<Track, TrackState> tracks = new HashMap<>(player.tracks());
for (Track track : Track.values()) {
if (track == Track.VOID) continue;
TrackState previous = player.track(track);
long threshold = switch (track) {
case MASK -> nextLimits.invisibilityMillis();
case DAGGER -> nextLimits.pearls();
case HEART -> nextLimits.darknessMillis();
case VOID -> throw new IllegalStateException("Void is objective-based.");
};
if (previous.progress() >= threshold && !previous.unlocked()) {
tracks.put(track, new TrackState(previous.progress(), true,
previous.activated(), previous.generation()));
}
}
evaluated.put(id, new PlayerState(tracks, player.pendingRewards()));
});
return new IgnisState(nextLimits, evaluated);
}
}
@@ -0,0 +1,15 @@
package games.dmg.spigotignis;
import java.util.Map;
import java.util.UUID;
/** Pending reward identifiers are durable; gameplay delivery is a later adapter. */
public record PlayerState(Map<Track, TrackState> tracks, Map<UUID, Track> pendingRewards) {
public PlayerState {
tracks = Map.copyOf(tracks);
pendingRewards = Map.copyOf(pendingRewards);
}
public static PlayerState empty() { return new PlayerState(Map.of(), Map.of()); }
public TrackState track(Track track) { return tracks.getOrDefault(track, TrackState.empty()); }
}
@@ -0,0 +1,14 @@
package games.dmg.spigotignis;
/** Thresholds use milliseconds for online time and units for consumed pearls. */
public record ProgressLimits(long invisibilityMillis, long pearls, long darknessMillis) {
public ProgressLimits {
if (invisibilityMillis <= 0 || pearls <= 0 || darknessMillis <= 0) {
throw new IllegalArgumentException("All progression thresholds must be positive.");
}
}
public static ProgressLimits defaults() {
return new ProgressLimits(12 * 60 * 60 * 1000L, 313, 6 * 60 * 60 * 1000L);
}
}
@@ -0,0 +1,26 @@
package games.dmg.spigotignis;
import java.util.concurrent.CompletableFuture;
import org.bukkit.plugin.java.JavaPlugin;
/** Foundation bootstrap only: no gameplay listeners or player/admin commands are registered. */
public final class SpigotIgnisPlugin extends JavaPlugin {
private CompletableFuture<StateService> initialization;
@Override
public void onEnable() {
initialization = StateService.open(new FileStateRepository(getDataFolder().toPath().resolve("state.bin")));
initialization.whenComplete((service, error) -> {
if (error != null) {
getLogger().severe("Ignis state unavailable; original data retained. Restore a verified backup before retrying.");
} else {
getLogger().info("Ignis foundation loaded. No gameplay abilities or commands are enabled.");
}
});
}
@Override
public void onDisable() {
if (initialization != null) initialization.thenAccept(StateService::close);
}
}
@@ -0,0 +1,8 @@
package games.dmg.spigotignis;
import java.io.IOException;
public interface StateRepository {
IgnisState load() throws IOException;
void save(IgnisState state) throws IOException;
}
@@ -0,0 +1,64 @@
package games.dmg.spigotignis;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.UnaryOperator;
/** Serialized persistence: acknowledge and publish only after a successful atomic save. */
public final class StateService implements AutoCloseable {
private final StateRepository repository;
private final ExecutorService worker;
private volatile IgnisState current;
private StateService(StateRepository repository, ExecutorService worker, IgnisState current) {
this.repository = repository;
this.worker = worker;
this.current = current;
}
public static CompletableFuture<StateService> open(StateRepository repository) {
Objects.requireNonNull(repository);
ExecutorService worker = Executors.newSingleThreadExecutor(task -> new Thread(task, "ignis-persistence"));
return CompletableFuture.supplyAsync(() -> {
try {
return new StateService(repository, worker, repository.load());
} catch (IOException | RuntimeException exception) {
worker.shutdown();
throw new java.util.concurrent.CompletionException(exception);
}
}, worker);
}
public IgnisState snapshot() { return current; }
public CompletableFuture<IgnisState> configure(long invisibilityMillis, long pearls, long darknessMillis) {
return update(state -> state.evaluate(new ProgressLimits(invisibilityMillis, pearls, darknessMillis)));
}
/** Changes must be pure and must not access Bukkit or cause gameplay side effects. */
public CompletableFuture<IgnisState> update(UnaryOperator<IgnisState> change) {
Objects.requireNonNull(change);
try {
return CompletableFuture.supplyAsync(() -> {
IgnisState next = Objects.requireNonNull(change.apply(current));
try {
repository.save(next);
} catch (IOException exception) {
throw new UncheckedIOException(exception);
}
current = next;
return next;
}, worker);
} catch (RejectedExecutionException exception) {
return CompletableFuture.failedFuture(exception);
}
}
/** Non-blocking; already queued mutations finish, and further mutations are rejected. */
@Override public void close() { worker.shutdown(); }
}
@@ -0,0 +1,3 @@
package games.dmg.spigotignis;
public enum Track { VOID, MASK, DAGGER, HEART }
@@ -0,0 +1,12 @@
package games.dmg.spigotignis;
/** Immutable durable foundation; time counters are online milliseconds. */
public record TrackState(long progress, boolean unlocked, boolean activated, long generation) {
public TrackState {
if (progress < 0 || generation < 0 || activated && !unlocked) {
throw new IllegalArgumentException("Invalid progression state.");
}
}
public static TrackState empty() { return new TrackState(0, false, false, 0); }
}
+5
View File
@@ -0,0 +1,5 @@
name: SpigotIgnis
version: '${version}'
main: games.dmg.spigotignis.SpigotIgnisPlugin
api-version: '1.21'
description: Ignis progression foundation; gameplay is not implemented yet.