feat(events): audit Speed I owned native layer lifecycle
Release / release (push) Failing after 11m20s
Release / release (push) Failing after 11m20s
This commit is contained in:
1 parent
03c121287c
commit
d20c38a216
7 files changed
+390
-15
No files matched your search
@@ -161,6 +161,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
private games.dmg.fantasystick.passive.HealthSteal healthSteal;
|
||||
private games.dmg.fantasystick.events.HealthStealEventBridge healthStealEvents;
|
||||
private java.util.concurrent.CompletionStage<Void> healthStealAuditDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.events.PassivePotionEventBridge passivePotionEvents;
|
||||
private java.util.concurrent.CompletionStage<Void> passivePotionAuditDrain = java.util.concurrent.CompletableFuture.completedFuture(null);
|
||||
private games.dmg.fantasystick.passive.SpeedI speedI;
|
||||
private games.dmg.fantasystick.passive.LowLightVision lowLightVision;
|
||||
private games.dmg.fantasystick.passive.PassiveRegistry passiveRegistry;
|
||||
@@ -870,6 +872,10 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
eventLog::emit, Clock.systemUTC(), getDataFolder().toPath(),
|
||||
failure -> getLogger().log(Level.WARNING, "Health Steal event intent failed", failure));
|
||||
healthSteal.attachEvents(healthStealEvents);
|
||||
passivePotionEvents = new games.dmg.fantasystick.events.PassivePotionEventBridge(
|
||||
eventLog::emit, Clock.systemUTC(), getDataFolder().toPath(),
|
||||
failure -> getLogger().log(Level.WARNING, "Passive potion event intent failed", failure));
|
||||
speedI.attachEvents(passivePotionEvents);
|
||||
recoveryEvents = new games.dmg.fantasystick.events.RecoveryEventBridge(
|
||||
eventLog::emit, Clock.systemUTC(), getDataFolder().toPath(), sessions,
|
||||
failure -> getLogger().log(Level.WARNING, "Recovery event intent failed", failure));
|
||||
@@ -1055,6 +1061,11 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous admin experience intent drain failed", failure);
|
||||
if (replayGrantEvents != null) replayGrantEvents.replay();
|
||||
});
|
||||
var replayPassivePotionEvents = passivePotionEvents;
|
||||
passivePotionAuditDrain.whenComplete((ignored, failure) -> {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous passive potion event drain failed", failure);
|
||||
if (replayPassivePotionEvents != null) replayPassivePotionEvents.replay();
|
||||
});
|
||||
var replayHealthStealEvents = healthStealEvents;
|
||||
healthStealAuditDrain.whenComplete((ignored, failure) -> {
|
||||
if (failure != null) getLogger().log(Level.WARNING, "Previous Health Steal event drain failed", failure);
|
||||
@@ -1149,6 +1160,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
var closingSheetPackEvents = sheetResourcePackEvents; sheetResourcePackEvents = null;
|
||||
var closingGrantEvents = adminExperienceEvents; adminExperienceEvents = null;
|
||||
var closingHealthStealEvents = healthStealEvents; healthStealEvents = null;
|
||||
var closingPassivePotionEvents = passivePotionEvents; passivePotionEvents = null;
|
||||
var closingRecoveryEvents = recoveryEvents; recoveryEvents = null;
|
||||
var closingIdentityAuditEvents = identityAuditEvents; identityAuditEvents = null;
|
||||
var closingAdminRevivalAuditEvents = adminRevivalEvents; adminRevivalEvents = null;
|
||||
@@ -1511,6 +1523,10 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
return null;
|
||||
}).thenCompose(ignored -> closingGrantEvents.drained());
|
||||
});
|
||||
if (closingPassivePotionEvents != null) cleanup("passive potion event intent drain", () -> {
|
||||
closingPassivePotionEvents.close();
|
||||
passivePotionAuditDrain = closingPassivePotionEvents.drained();
|
||||
});
|
||||
if (closingHealthStealEvents != null) cleanup("Health Steal event intent drain", () -> {
|
||||
var storageDrained = closingCharacters == null
|
||||
? java.util.concurrent.CompletableFuture.<Void>completedFuture(null) : closingCharacters.drained();
|
||||
|
||||
@@ -898,7 +898,8 @@ public final class CharacterBookController implements Consumer<ControlInput>, Au
|
||||
|| event.getTag() == null) return;
|
||||
var player=connection.getPlayer();String raw=event.getTag().string();if(raw.length()>120)return;var match=ABILITY_PAYLOAD.matcher(raw);if(!match.matches())return;String passive=match.group(2);var definition=passives.find(passive).orElse(null);if(definition==null)return;var view=passiveViews.get(player.getUniqueId());boolean owned=games.dmg.fantasystick.passive.QuickerRegenerationI.ID.equals(passive)
|
||||
|| games.dmg.fantasystick.passive.QuickerRegenerationII.ID.equals(passive)
|
||||
|| games.dmg.fantasystick.passive.HealthSteal.ID.equals(passive);
|
||||
|| games.dmg.fantasystick.passive.HealthSteal.ID.equals(passive)
|
||||
|| games.dmg.fantasystick.passive.SpeedI.ID.equals(passive);
|
||||
if (view == null || view.input().player() != player || !view.nonce().equals(match.group(1)) || !view.ids().contains(passive)
|
||||
|| !passiveViews.remove(player.getUniqueId(), view) || !view.input().stillCurrent()) {if(owned)refusePassive(player,definition,unlock,"stale_forged_or_repeated_action");return;}
|
||||
var audit=owned&&passiveEvents!=null?passiveEvents.begin(view.input(),view.before(),definition,unlock):null;
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package games.dmg.fantasystick.events;
|
||||
|
||||
import games.dmg.fantasystick.session.EquipmentSessions;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
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.util.concurrent.CompletionStage;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
/** Records real owned potion layers, not periodic evaluation or a desired passive state. */
|
||||
public final class PassivePotionEventBridge implements AutoCloseable {
|
||||
public record Application(UUID id, String passive, EventInput.Context context, Instant startedAt,
|
||||
String effectType, int amplifier) { }
|
||||
private final Consumer<EventInput> sink;
|
||||
private final Clock clock;
|
||||
private final FeatureEventOutbox outbox;
|
||||
|
||||
public PassivePotionEventBridge(Consumer<EventInput> sink, Clock clock) {
|
||||
this.sink = Objects.requireNonNull(sink); this.clock = Objects.requireNonNull(clock); outbox = null;
|
||||
}
|
||||
public PassivePotionEventBridge(Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink,
|
||||
Clock clock, Path data, Consumer<Throwable> failures) {
|
||||
this.sink = event -> sink.apply(event); this.clock = Objects.requireNonNull(clock);
|
||||
outbox = new FeatureEventOutbox(data, "passive-potion-event-intents",
|
||||
java.util.Set.of("effect.applied", "effect.ended"), sink, failures);
|
||||
}
|
||||
|
||||
public Application applied(String passive, Player player, EquipmentSessions.Active active,
|
||||
String characterName, PotionEffect owned, PotionEffect previous, String reason) {
|
||||
try {
|
||||
var context = new EventInput.Context(player.getUniqueId(), player.getName(), active.character(),
|
||||
characterName, active.session(), location(player));
|
||||
var application = new Application(UUID.randomUUID(), passive, context, clock.instant(),
|
||||
owned.getType().getKey().toString(), owned.getAmplifier());
|
||||
emit("effect.applied", application, context, reason,
|
||||
previous == null ? "no_unrelated_layer_displaced" : "weaker_native_layer_retained", previous, false);
|
||||
return application;
|
||||
} catch (RuntimeException unavailable) { return null; }
|
||||
}
|
||||
public void ended(Application application, Player player, String reason, String disposition,
|
||||
PotionEffect current, boolean restored) {
|
||||
if (application == null) return;
|
||||
try {
|
||||
var before = application.context();
|
||||
var context = new EventInput.Context(before.playerId(), before.playerName(), before.characterId(),
|
||||
before.characterName(), before.sessionId(), location(player));
|
||||
emit("effect.ended", application, context, reason, disposition, current, restored);
|
||||
} catch (RuntimeException ignored) { /* Audit failure cannot retry or veto native cleanup. */ }
|
||||
}
|
||||
private void emit(String type, Application application, EventInput.Context context, String reason,
|
||||
String disposition, PotionEffect nativeEffect, boolean restored) {
|
||||
try {
|
||||
var data = new LinkedHashMap<String, Object>();
|
||||
data.put("passive_id", application.passive()); data.put("effect_id", application.id().toString());
|
||||
data.put("application_id", application.id().toString());
|
||||
data.put("owner_character_id", context.characterId().toString()); data.put("owner_session_id", context.sessionId().toString());
|
||||
data.put("target_player_id", context.playerId().toString()); data.put("effect_type", application.effectType());
|
||||
data.put("amplifier", application.amplifier()); data.put("started_at", application.startedAt().toString());
|
||||
data.put("reason", reason); data.put("native_disposition", disposition); data.put("unrelated_effect_restored", restored);
|
||||
data.put("native_publication", "acknowledged");
|
||||
data.put("location_capture_quality", context.location() == null ? "unavailable" : "exact");
|
||||
if (nativeEffect != null) data.put("unrelated_native_effect", Map.of(
|
||||
"effect_type", nativeEffect.getType().getKey().toString(), "amplifier", nativeEffect.getAmplifier(),
|
||||
"duration_ticks", nativeEffect.getDuration(), "infinite", nativeEffect.isInfinite()));
|
||||
var event = new EventInput(id(application.id(), type), application.id(), clock.instant(), type, context, Map.copyOf(data));
|
||||
if (outbox != null) outbox.accepted(event); else sink.accept(event);
|
||||
} catch (RuntimeException ignored) { /* No native effect operation is reachable from the event exporter. */ }
|
||||
}
|
||||
private EventInput.Location location(Player player) {
|
||||
try {
|
||||
var value = player.getLocation(); if (value == null || value.getWorld() == null) return null;
|
||||
return new EventInput.Location(value.getWorld().getUID(), value.getWorld().getName(), value.getX(), value.getY(),
|
||||
value.getZ(), clock.instant(), "exact");
|
||||
} catch (RuntimeException unavailable) { return null; }
|
||||
}
|
||||
private static UUID id(UUID application, String type) {
|
||||
return UUID.nameUUIDFromBytes(("fantasystick:passive-potion:" + application + ":" + type).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
public void replay() { if (outbox != null) outbox.replay(); }
|
||||
public CompletionStage<Void> drained() {
|
||||
return outbox == null ? java.util.concurrent.CompletableFuture.completedFuture(null) : outbox.drained();
|
||||
}
|
||||
@Override public void close() { if (outbox != null) outbox.close(); }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package games.dmg.fantasystick.passive;
|
||||
|
||||
import games.dmg.fantasystick.session.EquipmentSessions;
|
||||
import games.dmg.fantasystick.events.PassivePotionEventBridge;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -20,6 +21,8 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
PotionEffect displaced;
|
||||
long displacedUntil;
|
||||
long refreshedAt;
|
||||
long applications;
|
||||
PassivePotionEventBridge.Application application;
|
||||
State(EquipmentSessions.Active active, Player player) { this.active = active; this.player = player; }
|
||||
|
||||
void refresh() {
|
||||
@@ -28,8 +31,11 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
PotionEffect current;
|
||||
try { current = player.getPotionEffect(type); }
|
||||
catch (RuntimeException unavailable) { return; }
|
||||
if (owned != null && !same(current, owned)) { owned = null; displaced = null; }
|
||||
if (!eligible()) { remove(); return; }
|
||||
if (owned != null && !same(current, owned)) {
|
||||
endAudit("native_layer_replaced_or_removed", current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
||||
owned = null; displaced = null;
|
||||
}
|
||||
if (!eligible()) { remove(endReason()); return; }
|
||||
if (current != null && (!replaceWeaker || current.getAmplifier() >= amplifier)) return;
|
||||
var effect = new PotionEffect(type, PotionEffect.INFINITE_DURATION, amplifier, false, false, true);
|
||||
try {
|
||||
@@ -37,6 +43,14 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
owned = effect; displaced = current;
|
||||
displacedUntil = current == null ? 0 : current.isInfinite()
|
||||
? Long.MAX_VALUE : sweepGeneration + current.getDuration();
|
||||
if (events != null) {
|
||||
try {
|
||||
var record = sessions.currentRecord(active).orElse(null);
|
||||
if (record != null) application = events.applied(passive, player, active, record.name(), owned, current,
|
||||
applications == 0 ? "learned_current_session" : "owned_layer_reestablished");
|
||||
} catch (RuntimeException ignored) { /* Audit capture cannot discard native ownership. */ }
|
||||
}
|
||||
applications++;
|
||||
}
|
||||
} catch (RuntimeException unavailable) { owned = null; displaced = null; }
|
||||
}
|
||||
@@ -47,7 +61,21 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
return record != null && !record.dead() && record.passives().contains(passive);
|
||||
}
|
||||
|
||||
void remove() {
|
||||
String endReason() {
|
||||
if (events == null) return "owner_no_longer_eligible";
|
||||
try {
|
||||
if (closed) return "plugin_shutdown";
|
||||
if (!sessions.current(active)) return "session_no_longer_current";
|
||||
var record = sessions.currentRecord(active).orElse(null);
|
||||
if (record != null && record.dead()) return "character_defeated";
|
||||
return "passive_unlearned_or_character_unavailable";
|
||||
} catch (RuntimeException ignored) { return "owner_no_longer_eligible"; }
|
||||
}
|
||||
void endAudit(String reason, String disposition, PotionEffect current, boolean restored) {
|
||||
if (events != null && application != null) events.ended(application, player, reason, disposition, current, restored);
|
||||
application = null;
|
||||
}
|
||||
void remove(String reason) {
|
||||
if (owned != null) {
|
||||
PotionEffect current;
|
||||
try { current = player.getPotionEffect(type); }
|
||||
@@ -55,14 +83,21 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
if (same(current, owned)) {
|
||||
try {
|
||||
player.removePotionEffect(type);
|
||||
boolean restored = false;
|
||||
if (displaced != null && (displaced.isInfinite() || sweepGeneration < displacedUntil)) {
|
||||
int duration = displaced.isInfinite() ? PotionEffect.INFINITE_DURATION
|
||||
: Math.toIntExact(Math.min(Integer.MAX_VALUE, displacedUntil - sweepGeneration));
|
||||
player.addPotionEffect(new PotionEffect(displaced.getType(), duration, displaced.getAmplifier(),
|
||||
restored = player.addPotionEffect(new PotionEffect(displaced.getType(), duration, displaced.getAmplifier(),
|
||||
displaced.isAmbient(), displaced.hasParticles(), displaced.hasIcon()), true);
|
||||
}
|
||||
if (events != null) {
|
||||
try {
|
||||
var after = player.getPotionEffect(type);
|
||||
if (!same(after, owned)) endAudit(reason, "owned_layer_removed", after, restored);
|
||||
} catch (RuntimeException unavailable) { /* No native acknowledgement: do not invent an end record. */ }
|
||||
}
|
||||
} catch (RuntimeException unavailable) { return; }
|
||||
}
|
||||
} else endAudit(reason, current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
||||
owned = null; displaced = null;
|
||||
}
|
||||
states.remove(active.player(), this);
|
||||
@@ -80,6 +115,12 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
private long sweepGeneration;
|
||||
private org.bukkit.scheduler.BukkitTask sweepTask;
|
||||
private boolean closed;
|
||||
private PassivePotionEventBridge events;
|
||||
|
||||
void attachEvents(PassivePotionEventBridge bridge) {
|
||||
if (closed || events != null || !states.isEmpty()) throw new IllegalStateException("Passive potion events already attached or evaluation started");
|
||||
events = Objects.requireNonNull(bridge);
|
||||
}
|
||||
|
||||
PersistentPotionEffect(String passive, PotionEffectType type, int amplifier, boolean replaceWeaker,
|
||||
EquipmentSessions sessions) {
|
||||
@@ -119,7 +160,7 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
var state = states.get(active.player());
|
||||
boolean learned = !snapshot.record().dead() && snapshot.record().passives().contains(passive);
|
||||
if (!learned || !sessions.current(active)) { if (state != null) state.refresh(); return; }
|
||||
if (state != null && !state.active.equals(active)) { state.remove(); state = states.get(active.player()); }
|
||||
if (state != null && !state.active.equals(active)) { state.remove("owner_session_replaced"); state = states.get(active.player()); }
|
||||
if (state == null) {
|
||||
var player = sessions.currentPlayer(active).orElse(null);
|
||||
if (player == null) return;
|
||||
@@ -139,7 +180,7 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
if (sweepTask != null) sweepTask.cancel();
|
||||
for (var state : List.copyOf(states.values())) state.remove();
|
||||
for (var state : List.copyOf(states.values())) state.remove("plugin_shutdown");
|
||||
states.clear();
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ public final class SpeedI implements AutoCloseable {
|
||||
"Does not stack with equal Speed I and preserves stronger unrelated Speed effects."));
|
||||
}
|
||||
|
||||
public void attachEvents(games.dmg.fantasystick.events.PassivePotionEventBridge bridge) { effect.attachEvents(bridge); }
|
||||
public void install(org.bukkit.plugin.Plugin plugin) { effect.install(plugin); }
|
||||
public void sweep() { effect.sweep(); }
|
||||
@Override public void close() { effect.close(); }
|
||||
|
||||
Reference in new issue
Block a user