Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/session/AdminCharacterIssuer.java
T
dmg 26ccc95cdd
Release / release (push) Successful in 14m24s
feat(events): record character coin outcomes
2026-09-24 23:57:29 -04:00

122 lines
6.0 KiB
Java

package games.dmg.fantasystick.session;
import games.dmg.fantasystick.items.CharacterSheets;
import games.dmg.fantasystick.persistence.CharacterRecord;
import games.dmg.fantasystick.persistence.CharacterStorage;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.bukkit.entity.Player;
/** Persists one fresh character before handing its authentic sheet to a still-valid native inventory slot. */
public final class AdminCharacterIssuer implements AutoCloseable {
public enum Outcome { DELIVERED, FULL, BUSY, STALE, SAVE_FAILED, RETAINED }
public record Failure(UUID player, UUID character, String operation, Throwable cause) { }
private final CharacterStorage storage;
private final Supplier<CharacterRecord> allocate;
private final Consumer<Runnable> nextTick;
private final Consumer<Failure> failures;
private final ConcurrentHashMap<UUID, Attempt> pending = new ConcurrentHashMap<>();
private volatile boolean closed;
public AdminCharacterIssuer(CharacterStorage storage, Supplier<CharacterRecord> allocate,
Consumer<Runnable> nextTick, Consumer<Failure> failures) {
this.storage = Objects.requireNonNull(storage);
this.allocate = Objects.requireNonNull(allocate);
this.nextTick = Objects.requireNonNull(nextTick);
this.failures = Objects.requireNonNull(failures);
}
/** Must be entered on the server thread. Completion is delivered on that thread. */
public void issue(Player player, Consumer<Outcome> response) {
Objects.requireNonNull(player); Objects.requireNonNull(response);
if (closed || !player.isOnline()) { response.accept(Outcome.STALE); return; }
int slot = player.getInventory().firstEmpty();
if (slot < 0) { response.accept(Outcome.FULL); return; }
var attempt = new Attempt(player, slot, response);
if (pending.putIfAbsent(attempt.playerId, attempt) != null) { response.accept(Outcome.BUSY); return; }
try {
attempt.record = allocate.get();
attempt.preparation = storage.prepareCraft(List.of(attempt.record),"admin_issue");
attempt.preparation.ready().whenComplete((batch, failure) -> schedule(attempt, batch, failure));
} catch (RuntimeException failure) {
pending.remove(attempt.playerId, attempt);
abandon(attempt);
report(attempt, "prepare", failure);
response.accept(Outcome.SAVE_FAILED);
}
}
private void schedule(Attempt attempt, CharacterStorage.CraftBatch batch, Throwable failure) {
if (closed || pending.get(attempt.playerId) != attempt) { abandon(attempt); return; }
try { nextTick.accept(() -> complete(attempt, batch, failure)); }
catch (RuntimeException rejected) {
pending.remove(attempt.playerId, attempt); abandon(attempt); report(attempt, "schedule", rejected);
}
}
private void complete(Attempt attempt, CharacterStorage.CraftBatch batch, Throwable failure) {
if (closed || !pending.remove(attempt.playerId, attempt)) { abandon(attempt); return; }
if (failure != null) {
abandon(attempt); report(attempt, "save", failure); respond(attempt, Outcome.SAVE_FAILED); return;
}
var occupying = attempt.player.getInventory().getItem(attempt.slot);
if (!attempt.player.isOnline() || attempt.slot < 0 || attempt.slot >= 36
|| occupying != null && !occupying.getType().isAir()) {
abandon(attempt); respond(attempt, Outcome.STALE); return;
}
var claimed = batch.claimForDelivery();
if (claimed.isEmpty() || claimed.orElseThrow().size() != 1
|| !claimed.orElseThrow().getFirst().id().equals(attempt.record.id())) {
abandon(attempt); respond(attempt, Outcome.RETAINED); return;
}
try {
var sheet = new CharacterSheets().create(attempt.record);
attempt.player.getInventory().setItem(attempt.slot, sheet);
attempt.delivered = true;
} catch (RuntimeException uncertain) {
report(attempt, "delivery", uncertain);
respond(attempt, Outcome.RETAINED);
return;
}
try { attempt.player.updateInventory(); }
catch (RuntimeException syncFailure) { report(attempt, "sync", syncFailure); }
respond(attempt, Outcome.DELIVERED);
}
private void respond(Attempt attempt, Outcome outcome) {
try { attempt.response.accept(outcome); }
catch (RuntimeException failure) { report(attempt, "response", failure); }
}
private void abandon(Attempt attempt) {
if (attempt.preparation == null || attempt.delivered || !attempt.abandoned.compareAndSet(false, true)) return;
attempt.preparation.abandon().thenAccept(cleanup -> {
if (!cleanup.retained().isEmpty()) report(attempt, "cleanup-retained", null);
});
}
private void report(Attempt attempt, String operation, Throwable cause) {
try { failures.accept(new Failure(attempt.playerId, attempt.record == null ? null : attempt.record.id(), operation, cause)); }
catch (RuntimeException ignored) { }
}
@Override public void close() {
closed = true;
for (var attempt : List.copyOf(pending.values())) {
if (pending.remove(attempt.playerId, attempt)) abandon(attempt);
}
}
private static final class Attempt {
final Player player; final UUID playerId; final int slot; final Consumer<Outcome> response;
final java.util.concurrent.atomic.AtomicBoolean abandoned = new java.util.concurrent.atomic.AtomicBoolean();
CharacterRecord record; CharacterStorage.CraftPreparation preparation; volatile boolean delivered;
Attempt(Player player, int slot, Consumer<Outcome> response) { this.player=player; this.playerId=player.getUniqueId(); this.slot=slot; this.response=response; }
}
}