feat(parties): audit native protection decisions
Release / release (push) Successful in 15m37s

This commit is contained in:
dmg committed 2026-09-25 14:11:48 -04:00
1 parent 39881ad6fc
commit af7c75b6ed
4 files changed
+704 -11

No files matched your search

@@ -752,6 +752,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
recoveryEvents = new games.dmg.fantasystick.events.RecoveryEventBridge(eventLog::emit, Clock.systemUTC());outOfCombatRecovery.attachEvents(recoveryEvents,record->record.passives().contains(games.dmg.fantasystick.passive.QuickerRegenerationI.ID)?quickerRegeneration.bonus():java.math.BigInteger.ZERO);
reviveEvents = new games.dmg.fantasystick.events.ReviveEventBridge(eventLog::emit, Clock.systemUTC());revive.attachEvents(reviveEvents);
partyAuditEvents = new games.dmg.fantasystick.events.PartyEventBridge(eventLog::emit, Clock.systemUTC());partyManagement.attachEvents(partyAuditEvents);admin.attachPartyEvents(partyAuditEvents);
partyCombat.attachProtectionEvents(new games.dmg.fantasystick.events.PartyProtectionEventBridge(
eventLog::emit, Clock.systemUTC()));
combatRewardEvents = new games.dmg.fantasystick.events.CombatRewardEventBridge(eventLog::emit,Clock.systemUTC());combatExperience.attachEvents(combatRewardEvents);
coinEvents = new games.dmg.fantasystick.events.CoinEventBridge(eventLog::emit,Clock.systemUTC());characters.attachCoinEvents(coinEvents);admin.attachCoinEvents(coinEvents);
sessionAuditEvents = new games.dmg.fantasystick.events.SessionEventBridge(eventLog::emit,Clock.systemUTC());sessions.attachSessionEvents(sessionAuditEvents);
@@ -0,0 +1,150 @@
package games.dmg.fantasystick.events;
import games.dmg.fantasystick.party.CombatSources;
import games.dmg.fantasystick.party.PartyRoster;
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.util.function.Consumer;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
/** Captures native protection decisions without changing the native damage/event authority. */
public final class PartyProtectionEventBridge {
public record Action(UUID id, Instant observedAt, EventInput.Context actor, Map<String, Object> data) { }
private final Consumer<EventInput> sink;
private final Clock clock;
public PartyProtectionEventBridge(Consumer<EventInput> sink, Clock clock) {
this.sink = Objects.requireNonNull(sink);
this.clock = Objects.requireNonNull(clock);
}
public Action damage(EntityDamageEvent event, CombatSources.Origin origin,
CombatSources.Origin recipient, Player target,
PartyRoster.Party sourceParty, PartyRoster.Party targetParty, String reason) {
try {
var id = UUID.randomUUID();
var time = clock.instant();
Entity causing = event.getDamageSource().getCausingEntity();
Entity direct = event.getDamageSource().getDirectEntity();
var source = causing instanceof Player player && player.getUniqueId().equals(origin.player()) ? player : null;
var context = source == null ? null : new EventInput.Context(source.getUniqueId(), source.getName(),
null, null, null, location(source.getLocation()));
var data = new LinkedHashMap<String, Object>();
data.put("action_kind", "party_damage_protection");
if (context == null) data.put("actor_kind", "system");
data.put("damage_cause", event.getCause().name().toLowerCase(java.util.Locale.ROOT));
data.put("source_kind", causing == null ? "provenance" : causing instanceof Player ? "player"
: causing.getType() == null ? "unavailable" : causing.getType().name().toLowerCase(java.util.Locale.ROOT));
data.put("source_player_id", origin.player().toString());
if (causing != null && causing.getUniqueId() != null)
data.put("causing_entity_id", causing.getUniqueId().toString());
if (direct != null && direct.getUniqueId() != null)
data.put("direct_entity_id", direct.getUniqueId().toString());
if (origin.character() != null) data.put("source_character_id", origin.character().toString());
if (origin.session() != null) data.put("source_session_id", origin.session().toString());
data.put("target_player_id", target.getUniqueId().toString());
if (recipient.character() != null) data.put("target_character_id", recipient.character().toString());
if (recipient.session() != null) data.put("target_session_id", recipient.session().toString());
if (sourceParty != null) data.put("source_party_id", sourceParty.id().toString());
if (targetParty != null) data.put("target_party_id", targetParty.id().toString());
data.put("provenance_quality", origin.session() == null ? "ordinary_or_unproved"
: reason.equals("stale_source") ? "claimed_session_unverified" : "matching_session_id");
data.put("provenance_age_status", "unavailable");
data.put("requested_amount", event.getDamage());
data.put("prior_cancelled", event.isCancelled());
data.put("proposed_reason", reason);
var sourceLocation = causing == null ? null : location(causing.getLocation());
var targetLocation = location(target.getLocation());
if (sourceLocation != null) data.put("source_location", coordinates(sourceLocation));
else data.put("source_location_status", "unavailable");
if (targetLocation != null) data.put("target_location", coordinates(targetLocation));
else data.put("target_location_status", "unavailable");
var action = new Action(id, time, context, Map.copyOf(data));
var observed = new LinkedHashMap<String, Object>(data);
observed.put("outcome", "observed");
emit(action, "action.observed", "observed", observed);
return action;
} catch (RuntimeException ignored) { return null; }
}
public Action nativeAttempt(String kind, Entity causing, Player target, CombatSources.Origin origin,
CombatSources.Origin recipient, PartyRoster.Party sourceParty,
PartyRoster.Party targetParty, String reason, String nativeKind) {
try {
var id = UUID.randomUUID();
var now = clock.instant();
var source = causing instanceof Player player && player.getUniqueId().equals(origin.player()) ? player : null;
var context = source == null ? null : new EventInput.Context(source.getUniqueId(), source.getName(),
null, null, null, location(source.getLocation()));
var data = new LinkedHashMap<String, Object>();
data.put("action_kind", kind);
if (context == null) data.put("actor_kind", "system");
data.put("native_kind", nativeKind);
data.put("source_player_id", origin.player().toString());
if (causing != null && causing.getUniqueId() != null)
data.put("direct_entity_id", causing.getUniqueId().toString());
if (origin.character() != null) data.put("source_character_id", origin.character().toString());
if (origin.session() != null) data.put("source_session_id", origin.session().toString());
if (recipient != null) {
data.put("target_player_id", recipient.player().toString());
if (recipient.character() != null) data.put("target_character_id", recipient.character().toString());
if (recipient.session() != null) data.put("target_session_id", recipient.session().toString());
} else data.put("target_status", "not_single_proved_player");
if (sourceParty != null) data.put("source_party_id", sourceParty.id().toString());
if (targetParty != null) data.put("target_party_id", targetParty.id().toString());
data.put("provenance_quality", origin.session() == null ? "ordinary_or_unproved"
: reason.equals("stale_source") ? "claimed_session_unverified" : "matching_session_id");
data.put("provenance_age_status", "unavailable");
data.put("requested_amount_status", "not_applicable");
data.put("prior_cancelled", false);
var sourceLocation = causing == null ? null : location(causing.getLocation());
var targetLocation = target == null ? null : location(target.getLocation());
if (sourceLocation != null) data.put("source_location", coordinates(sourceLocation));
else data.put("source_location_status", "unavailable");
if (targetLocation != null) data.put("target_location", coordinates(targetLocation));
else data.put("target_location_status", "unavailable");
var action = new Action(id, now, context, Map.copyOf(data));
var observed = new LinkedHashMap<String, Object>(data);
observed.put("outcome", "observed");
emit(action, "action.observed", "observed", observed);
return action;
} catch (RuntimeException ignored) { return null; }
}
public void resolved(Action action, String outcome, String reason, boolean cancelled, Double finalAmount) {
if (action == null) return;
var values = new LinkedHashMap<String, Object>(action.data());
values.put("outcome", outcome);
values.put("reason", reason);
values.put("final_cancelled", cancelled);
if (finalAmount != null) values.put("final_amount", finalAmount);
else values.put("final_amount_status", "not_applicable");
emit(action, "action.resolved", "resolved", values);
}
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 Map<String, Object> coordinates(EventInput.Location value) {
return Map.of("world_id", value.worldId().toString(), "world_name", value.worldName(),
"x", value.x(), "y", value.y(), "z", value.z(), "captured_at", value.capturedAt().toString());
}
private void emit(Action action, String type, String stage, Map<String, Object> data) {
try { sink.accept(new EventInput(UUID.nameUUIDFromBytes(("fantasystick:party-protection:" + action.id()
+ ':' + stage).getBytes(StandardCharsets.UTF_8)), action.id(),
stage.equals("observed") ? action.observedAt() : clock.instant(), type, action.actor(), data)); }
catch (RuntimeException ignored) { }
}
}
@@ -5,6 +5,7 @@ import java.util.Objects;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import java.util.WeakHashMap;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
@@ -17,12 +18,24 @@ public final class PartyCombat implements Listener, AutoCloseable {
private boolean closed;
private final PartyRoster parties;
private final CombatSources sources;
private games.dmg.fantasystick.events.PartyProtectionEventBridge protectionEvents;
private final WeakHashMap<EntityDamageEvent, DamageAudit> damageAudits = new WeakHashMap<>();
private final WeakHashMap<org.bukkit.event.Event, NativeAudit> nativeAudits = new WeakHashMap<>();
private record NativeAudit(games.dmg.fantasystick.events.PartyProtectionEventBridge.Action action,
String reason) { }
private record DamageAudit(games.dmg.fantasystick.events.PartyProtectionEventBridge.Action action,
String decision) { }
public PartyCombat(PartyRoster parties, EquipmentSessions sessions) {
this.parties = Objects.requireNonNull(parties);
this.sources = new CombatSources(sessions);
}
public void attachProtectionEvents(games.dmg.fantasystick.events.PartyProtectionEventBridge events) {
if (protectionEvents != null || closed) throw new IllegalStateException("Party protection events already attached");
protectionEvents = Objects.requireNonNull(events);
}
public void install(org.bukkit.plugin.Plugin plugin) {
cleanup = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
if (!closed) for (var player : plugin.getServer().getOnlinePlayers()) OngoingDamage.sweep(player);
@@ -31,6 +44,8 @@ public final class PartyCombat implements Listener, AutoCloseable {
@Override public void close() {
closed = true;
damageAudits.clear();
nativeAudits.clear();
if (cleanup != null) cleanup.cancel();
}
@@ -40,8 +55,21 @@ public final class PartyCombat implements Listener, AutoCloseable {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void impact(org.bukkit.event.entity.ProjectileHitEvent event) {
var origin = sources.read(event.getEntity());
if (origin != null && (!sources.current(origin) || !(event.getEntity() instanceof org.bukkit.entity.ThrownPotion)
&& event.getHitEntity() instanceof Player target && blocked(origin, target))) event.setCancelled(true);
if (origin == null) return;
var target = event.getHitEntity() instanceof Player player ? player : null;
var reason = !sources.current(origin) ? "stale_source"
: target != null && !(event.getEntity() instanceof org.bukkit.entity.ThrownPotion)
? protectionReason(origin, target) : null;
if (reason != null) {
captureNative(event, "party_projectile_impact_protection", event.getEntity(), target, origin,
reason, "projectile_impact");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedImpact(org.bukkit.event.entity.ProjectileHitEvent event) {
resolveNative(event, event);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -50,13 +78,33 @@ public final class PartyCombat implements Listener, AutoCloseable {
// Revoke stale continuation here. Valid healing and unrelated recipients are untouched;
// actual harmful applications and instantaneous damage have their own guards below.
var origin = sources.read(event.getEntity());
if (origin != null && !sources.current(origin)) event.setCancelled(true);
if (origin != null && !sources.current(origin)) {
var recipients = event.getAffectedEntities().stream().filter(Player.class::isInstance)
.map(Player.class::cast).toList();
captureNative(event, "party_splash_protection", event.getEntity(),
recipients.size() == 1 ? recipients.getFirst() : null, origin, "stale_source", "potion_splash");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedSplash(org.bukkit.event.entity.PotionSplashEvent event) {
resolveNative(event, event);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void staleLingering(LingeringPotionSplashEvent event) {
var origin = sources.read(event.getEntity());
if (origin != null && !sources.current(origin)) event.setCancelled(true);
if (origin != null && !sources.current(origin)) {
captureNative(event, "party_lingering_protection", event.getEntity(), null,
origin, "stale_source", "lingering_splash");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedLingering(LingeringPotionSplashEvent event) {
resolveNative(event, event);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -64,12 +112,49 @@ public final class PartyCombat implements Listener, AutoCloseable {
sources.store(event.getAreaEffectCloud(), sources.read(event.getEntity()));
}
@EventHandler(priority = EventPriority.LOWEST)
public void observeDamage(EntityDamageEvent event) {
if (protectionEvents == null || !(event.getEntity() instanceof Player target)) return;
// OngoingDamage.read reconciles native PDC. Only the original HIGHEST guard may do so;
// the LOWEST audit must not change its timing or mutate an earlier protection's state.
if (event.getCause() == EntityDamageEvent.DamageCause.POISON
|| event.getCause() == EntityDamageEvent.DamageCause.WITHER
|| event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK) return;
var origin = sources.source(event);
if (origin == null) return;
var ongoing = OngoingDamage.read(target, event.getCause());
String reason = ongoing != null && !sources.current(ongoing.target()) ? "stale_target"
: protectionReason(origin, target);
if (reason == null) return;
captureDamage(event, target, origin, reason);
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedDamage(EntityDamageEvent event) {
if (protectionEvents == null) return;
var audit = damageAudits.remove(event);
if (audit == null) return;
boolean prior = Boolean.TRUE.equals(audit.action().data().get("prior_cancelled"));
String reason = prior && event.isCancelled() ? "prior_cancelled"
: !event.isCancelled() && audit.decision() != null ? "protection_overridden"
: event.isCancelled() && audit.decision() != null ? audit.decision()
: event.isCancelled() ? "external_cancelled" : "protection_not_applied";
String outcome = !event.isCancelled() ? "accepted" : prior || audit.decision() == null
? "cancelled" : "refused";
protectionEvents.resolved(audit.action(), outcome, reason, event.isCancelled(),
event.isCancelled() ? 0 : event.getFinalDamage());
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void damage(EntityDamageEvent event) {
if (games.dmg.fantasystick.ability.GuardianBond.redirectedDamage()) return;
if (event.getEntity() instanceof Player target) {
var ongoing = OngoingDamage.read(target, event.getCause());
if (ongoing != null && (!sources.current(ongoing.target()) || blocked(ongoing.source(), target))) {
var reason = !sources.current(ongoing.target()) ? "stale_target"
: protectionReason(ongoing.source(), target);
captureDamage(event, target, ongoing.source(), reason);
markDamageDecision(event, reason);
event.setCancelled(true); return;
}
}
@@ -77,7 +162,14 @@ public final class PartyCombat implements Listener, AutoCloseable {
if (origin == null && event instanceof EntityDamageByEntityEvent entity) origin = sources.read(entity.getDamager());
if (origin == null) origin = sources.read(event.getDamageSource().getCausingEntity());
if (origin != null && (!sources.current(origin)
|| event.getEntity() instanceof Player target && blocked(origin, target))) event.setCancelled(true);
|| event.getEntity() instanceof Player target && blocked(origin, target))) {
if (event.getEntity() instanceof Player target) {
var reason = protectionReason(origin, target);
captureDamage(event, target, origin, reason);
markDamageDecision(event, reason);
}
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -85,7 +177,16 @@ public final class PartyCombat implements Listener, AutoCloseable {
if (!(event.getEntity() instanceof Player target) || event.getNewEffect() == null
|| OngoingDamage.cause(event.getModifiedType()) == null) return;
var origin = sources.read(event.getSource());
if (origin != null && blocked(origin, target)) event.setCancelled(true);
if (origin != null && blocked(origin, target)) {
captureNative(event, "party_effect_application_protection", event.getSource(), target,
origin, protectionReason(origin, target), event.getModifiedType().getKey().getKey());
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedPotion(org.bukkit.event.entity.EntityPotionEffectEvent event) {
resolveNative(event, event);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -105,7 +206,16 @@ public final class PartyCombat implements Listener, AutoCloseable {
public void combust(org.bukkit.event.entity.EntityCombustByEntityEvent event) {
if (!(event.getEntity() instanceof Player target)) return;
var origin = sources.read(event.getCombuster());
if (origin != null && blocked(origin, target)) event.setCancelled(true);
if (origin != null && blocked(origin, target)) {
captureNative(event, "party_ignite_protection", event.getCombuster(), target,
origin, protectionReason(origin, target), "combust");
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void resolvedCombust(org.bukkit.event.entity.EntityCombustByEntityEvent event) {
resolveNative(event, event);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -118,11 +228,49 @@ public final class PartyCombat implements Listener, AutoCloseable {
: new OngoingDamage.Application(origin, sources.player(target.getUniqueId())));
}
private boolean blocked(CombatSources.Origin origin, Player target) {
private void captureNative(org.bukkit.event.Event event, String kind, org.bukkit.entity.Entity causing,
Player target, CombatSources.Origin origin, String reason, String nativeKind) {
if (protectionEvents == null || reason == null) return;
var recipient = target == null ? null : sources.player(target.getUniqueId());
var action = protectionEvents.nativeAttempt(kind, causing, target, origin, recipient,
parties.partyOf(origin.player()).orElse(null),
recipient == null ? null : parties.partyOf(recipient.player()).orElse(null),
reason, nativeKind);
if (action != null) nativeAudits.put(event, new NativeAudit(action, reason));
}
private void resolveNative(org.bukkit.event.Event event, org.bukkit.event.Cancellable cancellable) {
if (protectionEvents == null) return;
var audit = nativeAudits.remove(event);
if (audit != null) protectionEvents.resolved(audit.action(), cancellable.isCancelled() ? "refused" : "accepted",
cancellable.isCancelled() ? audit.reason() : "external_uncancelled", cancellable.isCancelled(), null);
}
private void captureDamage(EntityDamageEvent event, Player target, CombatSources.Origin origin, String reason) {
if (protectionEvents == null || damageAudits.containsKey(event) || reason == null) return;
var recipient = sources.player(target.getUniqueId());
var action = protectionEvents.damage(event, origin, recipient, target,
parties.partyOf(origin.player()).orElse(null), parties.partyOf(recipient.player()).orElse(null), reason);
if (action != null) damageAudits.put(event, new DamageAudit(action, null));
}
private void markDamageDecision(EntityDamageEvent event, String reason) {
var audit = damageAudits.get(event);
if (audit != null && reason != null) damageAudits.put(event, new DamageAudit(audit.action(), reason));
}
private String protectionReason(CombatSources.Origin origin, Player target) {
if (!sources.current(origin)) return "stale_source";
var recipient = sources.player(target.getUniqueId());
boolean fantasy = origin.session() != null;
return !sources.current(origin) || fantasy != (recipient.session() != null) || fantasy
&& !origin.player().equals(recipient.player())
&& parties.partyOf(origin.player()).filter(group -> group.members().contains(recipient.player())).isPresent();
if (fantasy != (recipient.session() != null)) return "mixed_mode";
if (fantasy && !origin.player().equals(recipient.player())
&& parties.partyOf(origin.player()).filter(group -> group.members().contains(recipient.player())).isPresent())
return "same_party";
return null;
}
private boolean blocked(CombatSources.Origin origin, Player target) {
return protectionReason(origin, target) != null;
}
}