feat(stone-skin): audit owned damage mitigation
Release / release (push) Failing after 16m11s

This commit is contained in:
dmg committed 2026-09-25 23:13:37 -04:00
1 parent 4616ee80e3
commit ee7d9036a2
9 files changed
+262 -23

No files matched your search

@@ -763,6 +763,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
abilityEngine.enableCastEventsFor("longbow");
abilityEngine.enableCastEventsFor("quickstep");
abilityEngine.enableCastEventsFor("second_wind");
abilityEngine.enableCastEventsFor("stone_skin");
abilityEngine.enableCastEventsFor("mending_light");
abilityEngine.enableCastEventsFor("purify");
abilityEngine.enableCastEventsFor("guardian_bond");
@@ -444,6 +444,7 @@ public final class AbilityEngine implements Consumer<ControlInput>, AutoCloseabl
try { feedback.report(definition, input, type, target, report.amount()); }
catch (RuntimeException failure) { AbilityEngine.this.report(key.character(), key.ability(), failure); }
}
@Override public java.time.Instant now() { return clock.instant(); }
@Override public UUID effectApplied(String effectId, Entity target, Map<String, Object> evidence) {
if (!stillCurrent()) throw new IllegalStateException("Ability execution ended");
Objects.requireNonNull(effectId); Objects.requireNonNull(evidence);
@@ -17,6 +17,8 @@ public interface AbilityExecution extends AutoCloseable {
ControlInput input();
games.dmg.fantasystick.persistence.CharacterRecord character();
boolean stillCurrent();
/** Native-thread clock shared with cast and effect audit timestamps. */
default java.time.Instant now() { return java.time.Instant.now(); }
List<Entity> eligible(Collection<? extends Entity> candidates, int maximum);
AbilityScheduler.Task later(Duration delay, Runnable action);
void own(AutoCloseable resource);
@@ -19,9 +19,9 @@ import org.bukkit.event.entity.EntityDamageEvent;
public final class CombatMitigation implements Listener, AutoCloseable {
private final class Grant implements AutoCloseable {
final AbilityExecution execution; final Player player; final BigDecimal percent; final BooleanSupplier current;
final java.util.function.BiConsumer<EntityDamageEvent, Double> acceptedObservation; boolean closed;
final java.util.function.Consumer<Accepted> acceptedObservation; boolean closed;
Grant(AbilityExecution execution, Player player, BigDecimal percent, BooleanSupplier current,
java.util.function.BiConsumer<EntityDamageEvent, Double> acceptedObservation) {
java.util.function.Consumer<Accepted> acceptedObservation) {
this.execution = execution; this.player = player; this.percent = percent; this.current = current;
this.acceptedObservation = acceptedObservation;
}
@@ -35,7 +35,8 @@ public final class CombatMitigation implements Listener, AutoCloseable {
if (owned != null) { owned.remove(this); if (owned.isEmpty()) grants.remove(player.getUniqueId()); }
}
}
private record Prevention(Grant grant, BigDecimal amount) { }
public record Accepted(EntityDamageByEntityEvent event, double incoming, double remaining, double prevented) { }
private record Prevention(Grant grant, double incoming, double reduced) { }
private final Map<UUID, List<Grant>> grants = new java.util.HashMap<>();
private final Map<EntityDamageEvent, Prevention> pending = new IdentityHashMap<>();
private boolean closed;
@@ -48,9 +49,16 @@ public final class CombatMitigation implements Listener, AutoCloseable {
return grant(execution, player, percent, current, null);
}
/** An owner may observe only the selected, positive native prevention after final cancellation. */
/** Legacy owner observation; keep existing prevention consumers unchanged. */
public AutoCloseable grant(AbilityExecution execution, Player player, BigDecimal percent, BooleanSupplier current,
java.util.function.BiConsumer<EntityDamageEvent, Double> acceptedObservation) {
java.util.function.BiConsumer<EntityDamageEvent, Double> observation) {
return grantAccepted(execution, player, percent, current,
observation == null ? null : accepted -> observation.accept(accepted.event(), accepted.prevented()));
}
/** An owner may observe the selected, positive native prevention and its original hit value. */
public AutoCloseable grantAccepted(AbilityExecution execution, Player player, BigDecimal percent, BooleanSupplier current,
java.util.function.Consumer<Accepted> acceptedObservation) {
Objects.requireNonNull(execution); Objects.requireNonNull(player); Objects.requireNonNull(percent); Objects.requireNonNull(current);
if (closed || percent.signum() < 0 || percent.compareTo(BigDecimal.valueOf(100)) > 0)
throw new IllegalArgumentException("Invalid combat mitigation grant");
@@ -75,8 +83,7 @@ public final class CombatMitigation implements Listener, AutoCloseable {
.multiply(BigDecimal.ONE.subtract(strongest.percent.movePointLeft(2))).doubleValue();
if (!Double.isFinite(reduced) || reduced < 0) return;
event.setDamage(reduced);
BigDecimal prevented = BigDecimal.valueOf(original - reduced);
if (prevented.signum() > 0) pending.put(event, new Prevention(strongest, prevented));
if (original > reduced) pending.put(event, new Prevention(strongest, original, reduced));
}
@EventHandler(priority = EventPriority.MONITOR)
@@ -84,9 +91,15 @@ public final class CombatMitigation implements Listener, AutoCloseable {
var prevention = pending.remove(event);
if (prevention == null || event.isCancelled() || event.getFinalDamage() <= 0
|| !prevention.grant.valid()) return;
prevention.grant.execution.report(AbilityReport.Type.PREVENTION, event.getEntity(), prevention.amount);
double remaining = event.getDamage();
double actual = Math.min(prevention.incoming - prevention.reduced, prevention.incoming - remaining);
if (!Double.isFinite(remaining) || !Double.isFinite(actual) || actual <= 0) return;
try { prevention.grant.execution.report(AbilityReport.Type.PREVENTION, event.getEntity(),
BigDecimal.valueOf(actual)); }
catch (RuntimeException ignored) { /* Feedback cannot interrupt an accepted native hit or its audit. */ }
if (prevention.grant.acceptedObservation != null) try {
prevention.grant.acceptedObservation.accept(event, prevention.amount.doubleValue());
prevention.grant.acceptedObservation.accept(new Accepted((EntityDamageByEntityEvent) event,
prevention.incoming, remaining, actual));
} catch (RuntimeException ignored) { /* Audit cannot alter the accepted native hit. */ }
}
@@ -62,11 +62,24 @@ public final class StoneSkin implements AbilityEffect, AutoCloseable {
@Override public void activate(AbilityExecution execution) {
if (closed || !execution.stillCurrent()) return;
var current = from(execution.definition()); var player = execution.input().player();
var grant = mitigation.grant(execution, player, current.reductionPercent());
var grant = mitigation.grantAccepted(execution, player, current.reductionPercent(), () -> true,
accepted -> {
try {
execution.acceptedPrevention(execution.input().session(), player, accepted.event().getDamager(),
accepted.incoming(), accepted.prevented(), accepted.remaining(), null);
} catch (RuntimeException ignored) { /* Auditing cannot change an accepted native hit. */ }
});
var state = new State(execution, player, grant);
var previous = protections.put(player.getUniqueId(), state);
if (previous != null) previous.execution.finish();
execution.own(state); execution.later(current.duration(), execution::finish);
if (previous != null) { previous.execution.resolve("replaced"); previous.execution.finish(); }
execution.own(state);
var evidence = new java.util.LinkedHashMap<String, Object>();
evidence.put("reduction_percent", current.reductionPercent().stripTrailingZeros().toPlainString());
evidence.put("duration_seconds", BigDecimal.valueOf(current.duration().toNanos()).movePointLeft(9).stripTrailingZeros().toPlainString());
var activatedAt = execution.now();
if (activatedAt != null) evidence.put("effect_until", activatedAt.plus(current.duration()).toString());
execution.effectApplied("stone_skin", player, evidence);
execution.later(current.duration(), () -> { execution.resolve("expired"); execution.finish(); });
}
private static Settings from(AbilityDefinition definition) {
@@ -78,7 +91,9 @@ public final class StoneSkin implements AbilityEffect, AutoCloseable {
@Override public void close() {
if (closed) return; closed = true;
for (var state : List.copyOf(protections.values())) state.execution.finish();
for (var state : List.copyOf(protections.values())) {
state.execution.resolve("interrupted"); state.execution.finish();
}
protections.clear();
}
}
@@ -221,6 +221,10 @@ public final class AbilityCastEventBridge {
data.put("target_location", locationData(ally.getLocation()));
data.put("attacker_entity_id", attacker.getUniqueId().toString());
data.put("attacker_entity_type", entityType(attacker));
if (cast.abilityId().equals("stone_skin")) {
var attackLocation = attacker.getLocation();
if (attackLocation != null) data.put("attacker_location", locationData(attackLocation));
}
data.put("incoming_amount", decimal(incoming));
data.put("actual_amount", decimal(actual));
data.put("remaining_amount", decimal(remaining));
@@ -71,11 +71,12 @@ public final class StoneSkinSettings implements AutoCloseable {
public StoneSkin.Settings settings() { return parse(effectiveValues); }
/** Publish only after the caller returns the durable acknowledgment to the server thread. */
public synchronized void publish(Update update) {
public synchronized boolean publish(Update update) {
Objects.requireNonNull(update);
if (update.sequence() <= publishedSequence) return;
if (update.sequence() <= publishedSequence) return false;
parse(update.values());
effectiveValues = update.values(); publishedSequence = update.sequence();
return true;
}
public CompletionStage<Update> set(String key, String value) {
@@ -1677,8 +1677,11 @@ public final class EquipmentAdminCommand implements TabExecutor, AutoCloseable {
private void stoneSkinConfiguration(CommandSender sender, String[] args) {
String key = args[1];
var actor = abilityConfigEvents == null ? null : abilityConfigEvents.capture(sender);
String before = stoneSkinSettings.value(key);
if (args.length == 2) {
sender.sendMessage(key + "=" + stoneSkinSettings.value(key) + " (applies to future Stone Skin casts only)"); return;
if (abilityConfigEvents != null) abilityConfigEvents.read(actor, "stone_skin", key, before);
sender.sendMessage(key + "=" + before + " (applies to future Stone Skin casts only)"); return;
}
java.util.concurrent.CompletionStage<games.dmg.fantasystick.persistence.StoneSkinSettings.Update> operation;
try { operation = stoneSkinSettings.set(key, args[2]); }
@@ -1688,14 +1691,18 @@ public final class EquipmentAdminCommand implements TabExecutor, AutoCloseable {
if (closed) return;
try { nextTick.accept(() -> {
if (closed) return;
if (failure == null) {
stoneSkin.configure(saved.settings()); abilities.replace(stoneSkin.definition()); stoneSkinSettings.publish(saved);
String effectiveBefore = failure == null ? stoneSkinSettings.value(key) : null;
boolean applied = failure == null && stoneSkinSettings.publish(saved);
if (applied) {
stoneSkin.configure(stoneSkinSettings.settings()); abilities.replace(stoneSkin.definition());
if (abilityConfigEvents != null) abilityConfigEvents.changed(actor, "stone_skin", key, effectiveBefore, saved.value());
}
if (!sender.hasPermission(PERMISSION)
|| sender instanceof org.bukkit.entity.Player player && !player.isOnline()) return;
sender.sendMessage(failure == null
? "Saved " + key + "=" + saved.value() + " for future Stone Skin casts; active effects and deadlines are unchanged."
: "Stone Skin configuration save failed; the effective setting is unchanged.");
sender.sendMessage(failure != null
? "Stone Skin configuration save failed; the effective setting is unchanged."
: applied ? "Saved " + key + "=" + saved.value() + " for future Stone Skin casts; active effects and deadlines are unchanged."
: "Saved request was superseded; effective " + key + "=" + stoneSkinSettings.value(key) + ".");
}); } catch (RuntimeException rejected) { report(null, "config-response", rejected); }
});
}