Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/items/TailRevivalDelivery.java
T
dmg 6dc706b6ff
CI / build (pull_request) Successful in 9m47s
CI / build (push) Failing after 7m18s
fix(revival): preserve Tail authority and native custody
2026-10-07 13:40:35 -04:00

158 lines
9.9 KiB
Java

package games.dmg.fantasystick.items;
import games.dmg.fantasystick.persistence.CharacterRecord;
import games.dmg.fantasystick.persistence.CharacterStorage;
import games.dmg.fantasystick.persistence.RevivalStore;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/** Feature-scoped native custody proof shared by the existing learned and admin Tail routes. */
public final class TailRevivalDelivery {
private static final CharacterSheets SHEETS = new CharacterSheets();
private static final TailBooks TAILS = new TailBooks();
private TailRevivalDelivery() { }
public static boolean currentRecipient(Player target, java.util.function.Function<java.util.UUID, Player> players) {
return target.isOnline() && players.apply(target.getUniqueId()) == target;
}
public static boolean destinationAvailable(Player target, RevivalStore.Reservation reservation) {
if (reservation.sourceAmount() == 1) return reservation.destinationSlot() == -1;
var item = target.getInventory().getItem(reservation.destinationSlot());
return item == null || item.isEmpty();
}
public static boolean matchesRecord(CharacterRecord record, RevivalStore.Reservation reservation) {
return reservation.sheetGeneration() != null && record.id().equals(reservation.character())
&& record.sheetGeneration().equals(reservation.sheetGeneration())
&& reservation.death().equals(record.deathId());
}
public static ItemStack provisional(CharacterRecord dead, RevivalStore.Reservation reservation) {
if (!dead.dead() || !matchesRecord(dead, reservation)
|| reservation.phase() != RevivalStore.DeliveryPhase.DELIVERY_STARTED)
throw new IllegalArgumentException("Unbound Tail revival");
var living = new CharacterRecord(dead.id(), dead.name(), dead.revision(), dead.stats(), dead.level(),
dead.experience(), dead.unspentPoints(), dead.maximumHealth(), dead.learned(), dead.loadout(),
dead.passives(), dead.cooldowns(), dead.restrictedKills(), false, dead.deathId(), dead.diedAt(),
// Death-only escrow remains in the saved dead record until restore commits; a living
// reference projection must satisfy the same zero-escrow invariant as saved restoration.
dead.coins(), dead.coinsInitialized(), dead.rewardReceipts(), java.math.BigInteger.ZERO, dead.gear(),
dead.sheetGeneration());
var sheet = SHEETS.create(living); SHEETS.markRevival(sheet, reservation.id()); return sheet;
}
public static boolean proves(ItemStack item, RevivalStore.Reservation reservation) {
return reservation.phase() == RevivalStore.DeliveryPhase.DELIVERY_STARTED
&& SHEETS.identity(item).filter(value -> !value.legacy()
&& value.character().equals(reservation.character())
&& value.generation().equals(reservation.sheetGeneration())).isPresent()
&& SHEETS.hasRevival(item, reservation.id());
}
/** Count marked units, not UUID references. Revoked/unrelated same-character sheets stay untouched. */
public static int uniqueProofSlot(Player target, RevivalStore.Reservation reservation) {
int found = -1;
for (int slot = 0; slot <= 40; slot++) {
if (slot > 35 && slot != 40) continue;
var item = target.getInventory().getItem(slot);
if (!proves(item, reservation)) continue;
if (found >= 0 || item.getAmount() != 1) return -2;
found = slot;
}
return found;
}
public static boolean unchangedTail(Player target, RevivalStore.Reservation reservation) {
var source = target.getInventory().getItem(reservation.sourceSlot());
return source != null && source.getAmount() == reservation.sourceAmount() && matchingTail(source, reservation);
}
public static boolean consumedTail(Player target, RevivalStore.Reservation reservation) {
var source = target.getInventory().getItem(reservation.sourceSlot());
if (reservation.sourceAmount() == 1) return !matchingTail(source, reservation);
return source != null && source.getAmount() == reservation.sourceAmount() - 1 && matchingTail(source, reservation);
}
private static boolean matchingTail(ItemStack item, RevivalStore.Reservation reservation) {
return TAILS.identify(item).filter(value -> value.characterId().equals(reservation.character())
&& value.deathId().equals(reservation.death())).isPresent();
}
/** A RESERVED journal proves no writes began; item absence alone never proves non-delivery. */
public static boolean provablyUnperformed(Player target, RevivalStore.Reservation reservation) {
if (reservation.phase() != RevivalStore.DeliveryPhase.RESERVED || !unchangedTail(target, reservation)) return false;
for (int slot = 0; slot <= 40; slot++) {
if (slot > 35 && slot != 40) continue;
if (SHEETS.hasRevival(target.getInventory().getItem(slot), reservation.id())) return false;
}
return true;
}
/** All native access runs on the caller's server thread, including reentrant setter postconditions. */
public static boolean deliver(Player target, ItemStack captured, CharacterRecord dead,
RevivalStore.Reservation reservation, java.util.function.Function<java.util.UUID, Player> players, BooleanSupplier current) {
if (!current.getAsBoolean() || !currentRecipient(target, players)
|| !captured.equals(target.getInventory().getItem(reservation.sourceSlot()))
|| !destinationAvailable(target, reservation)) return false;
var sheet = provisional(dead, reservation);
if (reservation.sourceAmount() == 1) target.getInventory().setItem(reservation.sourceSlot(), sheet);
else {
target.getInventory().setItem(reservation.destinationSlot(), sheet);
// A setter may reenter plugin/native code. Never overwrite a changed source after it returns.
if (!current.getAsBoolean() || !currentRecipient(target, players)
|| !captured.equals(target.getInventory().getItem(reservation.sourceSlot()))
|| !sheet.equals(target.getInventory().getItem(reservation.destinationSlot()))) return false;
var remaining = captured.clone(); remaining.setAmount(reservation.sourceAmount() - 1);
target.getInventory().setItem(reservation.sourceSlot(), remaining);
}
target.updateInventory();
return current.getAsBoolean() && currentRecipient(target, players) && consumedTail(target, reservation)
&& uniqueProofSlot(target, reservation) >= 0;
}
/**
* Reload both durable authorities before acknowledgment. The provisional reference already is the
* current-generation sheet: finalization does not mint/upgrade/replace another item. Only the
* conditional-clear winner may report success. The issued marker remains harmless final provenance.
* Later ordinary inventory movement is not an atomic part of the saved completion acknowledgment.
*/
public static void complete(CharacterStorage characters, RevivalStore reservations, Consumer<Runnable> nextTick,
Consumer<Throwable> failures, Player target, RevivalStore.Reservation reservation, java.util.function.Function<java.util.UUID, Player> players,
BooleanSupplier current, java.util.function.Function<CharacterRecord, Runnable> acknowledgment, Runnable uncertain) {
reservations.find(reservation.character()).whenComplete((saved, saveFailure) -> {
if (saveFailure != null || saved.isEmpty() || !saved.orElseThrow().equals(reservation)) {
schedule(nextTick, failures, () -> { if (saveFailure != null) failures.accept(saveFailure); uncertain.run(); });
return;
}
characters.find(reservation.character()).whenComplete((found, loadFailure) -> schedule(nextTick, failures, () -> {
if (loadFailure != null) { failures.accept(loadFailure); uncertain.run(); return; }
if (!current.getAsBoolean() || !currentRecipient(target, players) || found.isEmpty()) { uncertain.run(); return; }
var record = found.orElseThrow();
int slot = uniqueProofSlot(target, reservation);
if (record.dead() || !matchesRecord(record, reservation) || slot < 0
|| !consumedTail(target, reservation)
|| !SHEETS.authorizes(target.getInventory().getItem(slot), record)) { uncertain.run(); return; }
final Runnable acknowledged;
try { acknowledged = java.util.Objects.requireNonNull(acknowledgment.apply(record)); }
catch (RuntimeException captureFailure) { failures.accept(captureFailure); uncertain.run(); return; }
reservations.clear(reservation).whenComplete((cleared, clearFailure) ->
schedule(nextTick, failures, () -> {
if (clearFailure != null) { failures.accept(clearFailure); uncertain.run(); return; }
// Successful clearance commits already proved historical completion. Later quit/expiry
// cannot turn it into unresolved custody after deleting its proof. Feedback is gated
// by each route; audit facts were captured above, while the native context was valid.
if (Boolean.TRUE.equals(cleared)) acknowledged.run();
else uncertain.run();
}));
}));
});
}
private static void schedule(Consumer<Runnable> nextTick, Consumer<Throwable> failures, Runnable action) {
try { nextTick.accept(action); } catch (RuntimeException rejected) { failures.accept(rejected); }
}
}