Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/events/EquipmentEventBridge.java
T
dmg 9737ed0ddb
Release / release (push) Successful in 15m47s
feat(equipment): audit native packing and recovery
2026-09-25 12:53:32 -04:00

165 lines
8.1 KiB
Java

package games.dmg.fantasystick.events;
import games.dmg.fantasystick.items.EquipmentStorage;
import games.dmg.fantasystick.persistence.CharacterRecord;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;
import org.bukkit.Location;
import org.bukkit.entity.Player;
/** Observes only proved native box custody and the owning acknowledged receipt boundary. */
public final class EquipmentEventBridge implements AutoCloseable {
private final Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink;
private final FeatureEventOutbox outbox;
private final Clock clock;
public EquipmentEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
Clock clock, Path dataFolder, Consumer<Throwable> failures) {
this.sink = Objects.requireNonNull(sink);
this.clock = Objects.requireNonNull(clock);
this.outbox = new FeatureEventOutbox(Objects.requireNonNull(dataFolder), "equipment-event-intents",
Set.of("equipment.packed", "equipment.restored", "equipment.recovery_resolved"), sink,
Objects.requireNonNull(failures));
}
public void replay() { outbox.replay(); }
@Override public void close() { outbox.close(); }
public CompletionStage<Void> drained() { return outbox.drained(); }
public record Capture(EventInput.Context context, EquipmentStorage.Snapshot box, Instant observedAt,
UUID session, UUID character) { }
public record Action(UUID id, Instant time, EventInput.Context context, UUID session, UUID character,
String kind) { }
public Action action(Player player, UUID session, UUID character, String kind) {
try {
// A receipt proves the character UUID, not its current display name. Keep that UUID
// in the payload rather than inventing an incomplete character envelope context.
var context = new EventInput.Context(player.getUniqueId(), player.getName(), null, null, null,
location(player.getLocation()));
var action = new Action(UUID.randomUUID(), clock.instant(), context, session, character, kind);
var values = actionFields(action);
values.put("outcome", "observed");
emitAction(action, "action.observed", "observed", values);
return action;
} catch (RuntimeException ignored) { return null; }
}
public void resolved(Action action, String outcome, String reason) {
if (action == null) return;
var values = actionFields(action);
values.put("outcome", outcome);
values.put("reason", reason);
emitAction(action, "action.resolved", "resolved", values);
}
public void recovery(Action action, UUID session, UUID character, String priorPhase, String currentPhase,
String quality, String outcome, String reason, BigInteger receiptRevision) {
if (action == null) return;
var values = actionFields(action);
values.put("recovery_operation_id", action.id().toString());
values.put("session_id", session.toString());
values.put("character_id", character.toString());
values.put("prior_phase", priorPhase);
values.put("current_phase", currentPhase);
values.put("custody_quality", quality);
values.put("outcome", outcome);
values.put("reason", reason);
if (receiptRevision != null) values.put("receipt_revision", receiptRevision.toString());
try { outbox.accepted(new EventInput(id(action.id(), "recovery_resolved"), action.id(), clock.instant(),
"equipment.recovery_resolved", action.context(), values)); }
catch (RuntimeException ignored) { }
}
private LinkedHashMap<String, Object> actionFields(Action action) {
var values = new LinkedHashMap<String, Object>();
values.put("action_kind", action.kind());
if (action.session() != null) values.put("session_id", action.session().toString());
if (action.character() != null) values.put("character_id", action.character().toString());
return values;
}
private void emitAction(Action action, String kind, String stage, Map<String, Object> values) {
try { sink.apply(new EventInput(id(action.id(), stage + ':' + values.get("outcome") + ':' + values.getOrDefault("reason", "")), action.id(),
stage.equals("observed") ? action.time() : clock.instant(), kind, action.context(), values)); }
catch (RuntimeException ignored) { }
}
public Capture capture(Player player, UUID session, CharacterRecord character, EquipmentStorage.Snapshot box) {
return capture(player, session, character.id(), character.name(), box);
}
/** Recovery can prove a receipt's character UUID without proving a current character name. */
public Capture capture(Player player, UUID session, UUID character, String characterName,
EquipmentStorage.Snapshot box) {
try {
return new Capture(new EventInput.Context(player.getUniqueId(), player.getName(),
characterName == null ? null : character, characterName,
characterName == null ? null : session, location(player.getLocation())),
box, clock.instant(), session, character);
} catch (RuntimeException ignored) { return null; }
}
public void packed(Capture captured, BigInteger revision) {
if (captured == null || revision == null) return;
var values = fields(captured, revision);
values.put("box_custody", "main_storage");
values.put("outcome", "acknowledged_packed");
emit(captured, "equipment.packed", "packed", values);
}
public void restored(Capture captured, BigInteger revision) {
if (captured == null || revision == null) return;
var values = fields(captured, revision);
values.put("displaced_head_handling", captured.box().displacedHead()
? captured.box().boxSource().equals("cursor") ? "cursor" : "freed_box_slot" : "none");
values.put("outcome", "acknowledged_restored");
emit(captured, "equipment.restored", "restored", values);
}
private LinkedHashMap<String, Object> fields(Capture captured, BigInteger revision) {
var box = captured.box();
var values = new LinkedHashMap<String, Object>();
values.put("session_id", captured.session().toString());
values.put("character_id", captured.character().toString());
values.put("equipment_operation_id", captured.session().toString());
values.put("receipt_revision", revision.toString());
values.put("slot_count", box.packedSlots());
values.put("slot_classes", box.slotClasses());
values.put("hotbar_slot_count", box.hotbarSlots());
values.put("armor_slot_count", box.armorSlots());
values.put("box_source", box.boxSource());
values.put("box_slot", box.boxSlot());
return values;
}
private void emit(Capture captured, String type, String phase, Map<String, Object> values) {
try { outbox.accepted(new EventInput(id(captured.session(), phase), captured.session(), captured.observedAt(),
type, captured.context(), values)); }
catch (RuntimeException ignored) { /* Audit failures cannot change native equipment custody. */ }
}
private EventInput.Location location(Location value) {
return value == null || value.getWorld() == null ? null
: new EventInput.Location(value.getWorld().getUID(), value.getWorld().getName(), value.getX(), value.getY(),
value.getZ(), clock.instant(), "exact");
}
private static UUID id(UUID session, String stage) {
return UUID.nameUUIDFromBytes(("fantasystick:equipment:" + session + ':' + stage)
.getBytes(StandardCharsets.UTF_8));
}
}