feat(sessions): complete native health and fantasy defeat
CI / build (push) Failing after 4m43s
Release / release (push) Failing after 4m45s

This commit is contained in:
dmg committed 2026-09-19 12:37:20 -04:00
1 parent 6f42082383
commit 6ac4c4bfde
21 files changed
+1010 -46

No files matched your search

@@ -21,7 +21,7 @@ import java.time.Clock;
import java.util.logging.Level;
import org.bukkit.plugin.java.JavaPlugin;
/** Crafting, equipment sessions and standalone names; health, nutrition and spells remain inactive. */
/** Crafting, native fantasy sessions, books, parties and names; progression actions and spells remain separate. */
public final class FantasyStickPlugin extends JavaPlugin {
private CharacterStorage characters;
private ManualCraftingController crafting;
@@ -42,6 +42,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
private CharacterChatNames chatNames;
private games.dmg.fantasystick.persistence.AdventureSettings adventureSettings;
private games.dmg.fantasystick.session.AdventureModeLease gameModes;
private games.dmg.fantasystick.session.SessionVitals vitals;
private games.dmg.fantasystick.session.SessionVisuals sessionVisuals;
private java.util.concurrent.CompletionStage<Void> configurationDrain = java.util.concurrent.CompletableFuture.completedStage(null);
@Override public void onEnable() {
@@ -106,6 +108,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
gameModes = new games.dmg.fantasystick.session.AdventureModeLease();
sessions = new EquipmentSessions(characters, receipts, switchDelay, Clock.systemUTC(), nextTick,
notice -> notifyPlayer(generation, notice), diagnostics, presentation, adventureSettings::enabled, gameModes);
vitals = new games.dmg.fantasystick.session.SessionVitals();
sessions.attachVitals(vitals);
parties = new games.dmg.fantasystick.party.PartyRoster(Clock.systemUTC(), id -> {
var player = getServer().getPlayer(id);
return player != null && player.isOnline();
@@ -136,6 +140,9 @@ public final class FantasyStickPlugin extends JavaPlugin {
partyCombat.install(this);
getServer().getPluginManager().registerEvents(books, this);
getServer().getPluginManager().registerEvents(gameModes, this);
sessionVisuals = new games.dmg.fantasystick.session.SessionVisuals(sessions,
failure -> getLogger().log(Level.WARNING, "Fantasy session particle delivery failed", failure));
sessionVisuals.install(this);
recipeRegistered = getServer().addRecipe(new CharacterSheets().recipe());
if (!recipeRegistered) throw new IllegalStateException("Character-sheet recipe key is already registered");
for (var player : getServer().getOnlinePlayers()) sessions.arrive(player);
@@ -166,6 +173,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
var closingCharacters = characters; characters = null;
var closingSettings = adventureSettings; adventureSettings = null;
var closingModes = gameModes; gameModes = null;
var closingVitals = vitals; vitals = null;
var closingVisuals = sessionVisuals; sessionVisuals = null;
// Retire client resources independently of successful ordinary-equipment restoration.
if (closingPresentation != null) cleanup("presentation", closingPresentation::close);
if (closingProtocol != null) cleanup("protocol presentation", closingProtocol::close);
@@ -175,11 +184,13 @@ public final class FantasyStickPlugin extends JavaPlugin {
cleanup("configuration", closingSettings::close);
}
if (closingBooks != null) cleanup("character book", closingBooks::close);
if (closingVisuals != null) cleanup("session particles", closingVisuals::close);
if (closingParties != null) cleanup("parties", closingParties::close);
if (closingPartyCombat != null) cleanup("party combat", closingPartyCombat::close);
if (closingEvents != null) cleanup("session events", closingEvents::close);
if (closingCrafting != null) cleanup("craft attempts", closingCrafting::close);
if (closingSessions != null) cleanup("equipment sessions", closingSessions::close);
if (closingVitals != null) cleanup("native health hooks", closingVitals::close);
unregister(closingModes);
unregister(closingChat);
unregister(closingPartyEvents);
@@ -243,7 +254,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
default -> notice.message();
};
player.sendMessage(message);
player.spawnParticle(org.bukkit.Particle.ANGRY_VILLAGER, player.getLocation().add(0, 1, 0), 8, 0.3, 0.4, 0.3, 0);
if (notice.code().equals("DEFEAT")) games.dmg.fantasystick.session.SessionVisuals.burst(player, org.bukkit.Color.RED, 12);
else player.spawnParticle(org.bukkit.Particle.ANGRY_VILLAGER, player.getLocation().add(0, 1, 0), 8, 0.3, 0.4, 0.3, 0);
};
if (lifetime != generation) return;
if (getServer().isPrimaryThread()) delivery.run();
@@ -85,7 +85,8 @@ public final class CharacterBookController implements Consumer<ControlInput>, Au
: record.experience().remainder(rules.xpPerLevel()) + "/" + rules.xpPerLevel();
String learned = record.learned().isEmpty() ? "none" : String.join(", ", record.learned().stream().sorted().toList());
String second = "Level: " + record.level() + "\nXP: " + xp + "\nUnspent points: " + record.unspentPoints()
+ "\nHealth: " + record.health().min(maximum).stripTrailingZeros().toPlainString() + "/" + maximum.stripTrailingZeros().toPlainString()
+ "\nHealth: " + java.math.BigDecimal.valueOf(input.player().getHealth()).min(maximum).stripTrailingZeros().toPlainString()
+ "/" + maximum.stripTrailingZeros().toPlainString()
+ "\n\nAbilities: " + learned;
var item = new ItemStack(Material.WRITTEN_BOOK);
var meta = (BookMeta) item.getItemMeta();
@@ -35,14 +35,14 @@ public final class TailBooks {
meta.setGeneration(BookMeta.Generation.ORIGINAL);
meta.displayName(Component.text(fullTitle));
List<Component> pages = new ArrayList<>();
pages.add(Component.text(fullTitle));
pages.add(Component.text("Level: " + character.level()));
pages.add(Component.text("XP: " + character.experience()));
pages.add(Component.text("Unspent points: " + character.unspentPoints()));
for (Stat stat : Stat.values()) pages.add(Component.text(stat + ": " + character.stats().get(stat)));
pages.add(Component.text("Abilities: " + (character.learned().isEmpty() ? "None" : "")));
character.learned().stream().sorted().forEach(ability -> pages.add(Component.text(ability)));
pages.add(Component.text("Fell: " + character.diedAt()));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages(fullTitle));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages("Level: " + character.level()));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages("XP: " + character.experience()));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages("Unspent points: " + character.unspentPoints()));
for (Stat stat : Stat.values()) pages.addAll(games.dmg.fantasystick.book.BookPagination.pages(stat + ": " + character.stats().get(stat)));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages("Abilities: " + (character.learned().isEmpty() ? "None" : "")));
character.learned().stream().sorted().forEach(ability -> pages.addAll(games.dmg.fantasystick.book.BookPagination.pages(ability)));
pages.addAll(games.dmg.fantasystick.book.BookPagination.pages("Fell: " + character.diedAt()));
meta.pages(pages);
var data = meta.getPersistentDataContainer();
data.set(VERSION, PersistentDataType.INTEGER, 1);
@@ -210,6 +210,25 @@ public final class CharacterStorage implements AutoCloseable {
});
}
/** Session-owned remaining health; retain all other current fields and reject concurrent revision changes. */
public CompletableFuture<Optional<CharacterRecord>> saveHealth(UUID characterId, java.math.BigDecimal health,
java.math.BigDecimal nativeLimit) {
Objects.requireNonNull(characterId);
if (health.signum() < 0 || nativeLimit.signum() <= 0) throw new IllegalArgumentException("Invalid health bound");
return submit(() -> {
var found = repository.load(characterId);
if (found.isEmpty() || found.orElseThrow().dead()) return Optional.empty();
var old = found.orElseThrow();
var bounded = health.min(old.maximumHealth()).min(nativeLimit);
if (old.health().compareTo(bounded) == 0) return Optional.of(old);
var saved = new CharacterRecord(old.id(), old.name(), old.revision().add(BigInteger.ONE), old.stats(), old.level(),
old.experience(), old.unspentPoints(), bounded, old.learned(), old.loadout(), old.cooldowns(),
false, old.deathId(), old.diedAt());
repository.replace(saved, old.revision());
return Optional.of(saved);
});
}
/**
* One accepted defeat against the latest live record on this worker, including queued names.
* The repository's locked revision compare-and-replace prevents overwriting a concurrent
@@ -48,8 +48,11 @@ public final class EquipmentSessionStore implements AutoCloseable {
private final ExecutorService worker = Executors.newSingleThreadExecutor(
task -> new Thread(task, "fantasy-equipment-session-persistence"));
public record Receipt(UUID sessionId, UUID characterId, Instant preparedAt, String previousGameMode) {
public Receipt(UUID sessionId, UUID characterId, Instant preparedAt) { this(sessionId, characterId, preparedAt, null); }
public record Receipt(UUID sessionId, UUID characterId, Instant preparedAt, String previousGameMode, OrdinaryVitals ordinaryVitals) {
public Receipt(UUID sessionId, UUID characterId, Instant preparedAt) { this(sessionId, characterId, preparedAt, null, null); }
public Receipt(UUID sessionId, UUID characterId, Instant preparedAt, String previousGameMode) {
this(sessionId, characterId, preparedAt, previousGameMode, null);
}
public Receipt {
Objects.requireNonNull(sessionId);
Objects.requireNonNull(characterId);
@@ -116,15 +119,20 @@ public final class EquipmentSessionStore implements AutoCloseable {
/** A receipt acknowledgment precedes mode/gear mutation; it is not proof either happened. */
public CompletionStage<State> prepare(UUID player, UUID session, UUID character, String previousGameMode) {
return prepare(player, session, character, previousGameMode, null);
}
public CompletionStage<State> prepare(UUID player, UUID session, UUID character, String previousGameMode, OrdinaryVitals ordinaryVitals) {
Objects.requireNonNull(player);
var receipt = new Receipt(session, character, clock.instant(), previousGameMode);
var receipt = new Receipt(session, character, clock.instant(), previousGameMode, ordinaryVitals);
return submit(() -> withLock(() -> {
var found = load(player);
if (found.isPresent()) {
var old = found.orElseThrow();
if (old.pending() != null) {
if (session.equals(old.pending().sessionId()) && character.equals(old.pending().characterId())
&& Objects.equals(previousGameMode, old.pending().previousGameMode())) {
&& Objects.equals(previousGameMode, old.pending().previousGameMode())
&& Objects.equals(ordinaryVitals, old.pending().ordinaryVitals())) {
return old;
}
throw new IOException("Conflicting pending equipment session");
@@ -296,6 +304,7 @@ public final class EquipmentSessionStore implements AutoCloseable {
pending.put("character-id", receipt.characterId().toString());
pending.put("prepared-at", receipt.preparedAt().toString());
if (receipt.previousGameMode() != null) pending.put("previous-game-mode", receipt.previousGameMode());
if (receipt.ordinaryVitals() != null) pending.put("ordinary-vitals", receipt.ordinaryVitals().encode());
root.put("pending", pending);
}
if (state.lastExit() != null) {
@@ -317,13 +326,14 @@ public final class EquipmentSessionStore implements AutoCloseable {
Exit exit = null;
if (root.containsKey("pending")) {
var keys = Set.of("session-id", "character-id", "prepared-at");
var row = mapping(root.get("pending"), keys, Set.of("session-id", "character-id", "prepared-at", "previous-game-mode"));
var row = mapping(root.get("pending"), keys, Set.of("session-id", "character-id", "prepared-at", "previous-game-mode", "ordinary-vitals"));
String previousMode = null;
if (row.containsKey("previous-game-mode")) {
if (!(row.get("previous-game-mode") instanceof String value)) throw new IllegalArgumentException("Invalid previous mode");
previousMode = value;
}
pending = new Receipt(id(row.get("session-id")), id(row.get("character-id")), instant(row.get("prepared-at")), previousMode);
pending = new Receipt(id(row.get("session-id")), id(row.get("character-id")), instant(row.get("prepared-at")), previousMode,
row.containsKey("ordinary-vitals") ? OrdinaryVitals.decode(text(row.get("ordinary-vitals"))) : null);
}
if (root.containsKey("last-exit")) {
var keys = Set.of("session-id", "character-id", "exited-at", "deadline");
@@ -0,0 +1,27 @@
package games.dmg.fantasystick.persistence;
/** Holder-owned ordinary state, never transferable character progress or an attribute snapshot. */
public record OrdinaryVitals(double health, int food, float saturation, float exhaustion,
boolean scaled, double scale) {
public OrdinaryVitals {
if (!Double.isFinite(health) || health < 0 || food < 0 || food > 20
|| !Float.isFinite(saturation) || saturation < 0 || !Float.isFinite(exhaustion) || exhaustion < 0
|| !Double.isFinite(scale) || scale <= 0) throw new IllegalArgumentException("Invalid ordinary vitals");
}
public String encode() {
return health + ";" + food + ";" + saturation + ";" + exhaustion + ";" + scaled + ";" + scale;
}
public static OrdinaryVitals decode(String encoded) {
if (encoded.length() > 256) throw new IllegalArgumentException("Oversized ordinary vitals");
String[] fields = encoded.split(";", -1);
if (fields.length != 6 || !fields[4].equals("true") && !fields[4].equals("false")) {
throw new IllegalArgumentException("Invalid ordinary vitals fields");
}
var value = new OrdinaryVitals(Double.parseDouble(fields[0]), Integer.parseInt(fields[1]),
Float.parseFloat(fields[2]), Float.parseFloat(fields[3]), Boolean.parseBoolean(fields[4]), Double.parseDouble(fields[5]));
if (!encoded.equals(value.encode())) throw new IllegalArgumentException("Noncanonical ordinary vitals");
return value;
}
}
@@ -24,8 +24,8 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* Server-thread equipment custody and minimal defeat handling. Fantasy health attributes,
* nutrition and rewards are not applied here. Presentation is an independently cleaned output port. No inventory/file transaction,
* Server-thread equipment custody, optional native vitals overlay and intercepted defeat.
* Rewards and active food overrides remain separate. Presentation is independently cleaned. No inventory/file transaction,
* automatic retry or snapshot rollback is promised. The scheduler must defer onto
* the server thread; diagnostics must be thread-safe and must not use Player APIs.
*/
@@ -57,6 +57,7 @@ public final class EquipmentSessions implements AutoCloseable {
private boolean closed;
private java.util.function.BooleanSupplier adventureMode = () -> false;
private AdventureModeLease gameModes = new AdventureModeLease();
private SessionVitals vitals;
private boolean closeSweepFinished;
private int persistenceOperations;
private final CompletableFuture<Void> drained = new CompletableFuture<>();
@@ -77,6 +78,11 @@ public final class EquipmentSessions implements AutoCloseable {
boolean effectsStarted;
String previousGameMode;
boolean modeStarted;
games.dmg.fantasystick.persistence.OrdinaryVitals ordinaryVitals;
boolean vitalsStarted;
boolean healthSaveRequested;
boolean healthAcknowledged = true;
boolean nativeRestored;
boolean gearRestoredForModeRecovery;
boolean presentationInstalled;
boolean cleaning;
@@ -85,6 +91,7 @@ public final class EquipmentSessions implements AutoCloseable {
boolean nativeUncertain;
boolean exitRequested;
boolean arrival;
Attempt recoveryOwner;
org.bukkit.Location entryLocation;
boolean defeated;
boolean deathResolved;
@@ -134,6 +141,12 @@ public final class EquipmentSessions implements AutoCloseable {
this.gameModes = Objects.requireNonNull(gameModes);
}
/** Install before exposing entry; equipment-only adapter tests can omit the health overlay. */
public synchronized void attachVitals(SessionVitals value) {
if (vitals != null || !owned.isEmpty()) throw new IllegalStateException("Vitals already attached or sessions started");
vitals = Objects.requireNonNull(value);
}
/** One server-thread custody bridge per coordinator; never called by persistence workers. */
synchronized void attachCursorCustody(Consumer<UUID> invalidate) {
if (cursorCustodyAttached) throw new IllegalStateException("Cursor custody already attached");
@@ -239,7 +252,8 @@ public final class EquipmentSessions implements AutoCloseable {
private record Loaded(Optional<CharacterRecord> character, Optional<EquipmentSessionStore.State> state) { }
private boolean entryNativeSafe(Attempt attempt, EquipmentSessionStore.State state) {
if (hasRemnants(attempt.player) || !gameModes.available(attempt.player)) return false;
if (hasRemnants(attempt.player) || !gameModes.available(attempt.player)
|| vitals != null && !vitals.available(attempt.player)) return false;
var marker = EquipmentSessionMarker.read(attempt.player).orElse(null);
if (EquipmentSessionMarker.present(attempt.player)) {
// A terminal marker is resolved only by its matching completed file acknowledgment.
@@ -303,8 +317,9 @@ public final class EquipmentSessions implements AutoCloseable {
attempt.session = attempt.claim.sessionId();
owned.put(attempt.owner, attempt);
controls.create(record, attempt.owner, attempt.session); // Validate all nine controls before preparation/packing.
if (vitals != null) attempt.ordinaryVitals = vitals.capture(attempt.player);
attempt.saving = true;
persist(() -> receipts.prepare(attempt.owner, attempt.session, attempt.character, attempt.previousGameMode), (saved, error) -> {
persist(() -> receipts.prepare(attempt.owner, attempt.session, attempt.character, attempt.previousGameMode, attempt.ordinaryVitals), (saved, error) -> {
attempt.saving = false;
if (error != null) { attempt.uncertainSave = true; fail(attempt, error); return; }
attempt.acknowledged = true;
@@ -349,6 +364,12 @@ public final class EquipmentSessions implements AutoCloseable {
if (interrupted(attempt)) return;
}
mark(attempt, EquipmentSessionMarker.Phase.PACKED);
if (vitals != null) {
attempt.vitalsStarted = true;
attempt.healthAcknowledged = false;
vitals.enter(attempt.player, attempt.session, attempt.ordinaryVitals, record);
if (attempt.exitRequested || !current(attempt)) return;
}
pending.remove(attempt.owner, attempt);
if (!attempt.exitRequested) {
attempt.record = record;
@@ -443,8 +464,8 @@ public final class EquipmentSessions implements AutoCloseable {
attempt.deathReference = copy(attempt.player.getInventory().getItemInOffHand());
attempt.deathView = attempt.player.getOpenInventory();
restoreExit(attempt);
// Equipment-only sessions have not changed attributes. Read the current ordinary
// maximum, including unrelated modifiers; never set a base value or modifiers.
// Read the current maximum after owned overlay cleanup, including legitimate
// ordinary modifiers. Never reconstruct attributes from an entry snapshot.
double maximum = Objects.requireNonNull(attempt.player.getAttribute(org.bukkit.attribute.Attribute.MAX_HEALTH)).getValue();
event.setReviveHealth(maximum);
attempt.player.setHealth(maximum); // Native cancellation only fills health when <= 0.
@@ -548,19 +569,24 @@ public final class EquipmentSessions implements AutoCloseable {
boolean reserved = transitioning.putIfAbsent(attempt.owner, attempt) == null;
try {
invalidateCursor(attempt.owner);
saveRemainingHealth(attempt);
boolean modeRestored = restoreGameMode(attempt);
EquipmentStorage.RestoreResult result;
try {
result = restoreMarked(attempt);
} catch (RuntimeException failure) {
attempt.nativeUncertain = true;
restoreVitals(attempt); // Proven health ownership is independent of uncertain equipment writes.
persistExit(attempt, false);
diagnostic(attempt, failure);
notice(attempt.owner, "NATIVE_UNCERTAIN", "Restoration failed; items and receipt require inspection.");
return EquipmentStorage.RestoreResult.INVALID_BOX;
}
if (result == EquipmentStorage.RestoreResult.RESTORED && !modeRestored) attempt.gearRestoredForModeRecovery = true;
persistExit(attempt, result == EquipmentStorage.RestoreResult.RESTORED && modeRestored);
boolean vitalsRestored = restoreVitals(attempt);
if (result == EquipmentStorage.RestoreResult.RESTORED && (!modeRestored || !vitalsRestored || !attempt.healthAcknowledged)) {
attempt.gearRestoredForModeRecovery = true;
}
persistExit(attempt, result == EquipmentStorage.RestoreResult.RESTORED && modeRestored && vitalsRestored);
if (result != EquipmentStorage.RestoreResult.RESTORED) {
notice(attempt.owner, result.name(), "Equipment recovery refused: " + result);
} else sync(attempt);
@@ -608,14 +634,42 @@ public final class EquipmentSessions implements AutoCloseable {
catch (RuntimeException failure) { diagnostic(attempt, failure); }
}
private void saveRemainingHealth(Attempt attempt) {
if (!attempt.vitalsStarted || attempt.defeated || attempt.healthSaveRequested) return;
attempt.healthSaveRequested = true; // Failed/uncertain writes are not authority for an automatic replay.
try {
var remaining = vitals.remaining(attempt.player, attempt.session, attempt.ordinaryVitals);
var limit = NativeHealthLimit.read();
persist(() -> characters.saveHealth(attempt.character, remaining, limit), (saved, failure) -> {
if (failure != null || saved.isEmpty()) {
diagnostic(attempt, failure != null ? failure : new IllegalStateException("Character unavailable for health save"));
return;
}
attempt.healthAcknowledged = true;
if (attempt.nativeRestored) persistExit(attempt, true);
});
} catch (RuntimeException failure) { diagnostic(attempt, failure); }
}
private boolean restoreVitals(Attempt attempt) {
if (vitals == null || attempt.ordinaryVitals == null) return true;
try {
if (vitals.restore(attempt.player, attempt.session, attempt.ordinaryVitals)) return true;
} catch (RuntimeException failure) { diagnostic(attempt, failure); }
notice(attempt.owner, "VITALS_RECOVERY", "Health/nutrition restoration was refused or ambiguous; retained receipt requires inspection.");
return false;
}
private void persistExit(Attempt attempt, boolean restored) {
attempt.nativeRestored = restored;
boolean complete = restored && (attempt.healthAcknowledged || attempt.defeated);
attempt.saving = true;
persist(() -> restored
persist(() -> complete
? receipts.completeExit(attempt.owner, attempt.session, switchDelay)
: receipts.recordExit(attempt.owner, attempt.session, switchDelay), (state, failure) -> {
attempt.saving = false;
if (failure != null) diagnostic(attempt, failure);
else if (restored) { attempt.gearAcknowledged = true; release(attempt); }
else if (complete) { attempt.gearAcknowledged = true; release(attempt); }
});
}
@@ -647,6 +701,7 @@ public final class EquipmentSessions implements AutoCloseable {
if (!attempt.player.isOnline()
|| attempt.previousGameMode != null && !attempt.modeStarted
&& !attempt.player.getGameMode().name().equals(attempt.previousGameMode)
|| attempt.ordinaryVitals != null && !attempt.vitalsStarted && !vitals.unchanged(attempt.player, attempt.ordinaryVitals)
|| attempt.markerValidated && (EquipmentSessionMarker.present(attempt.player) != (attempt.entryMarker != null)
|| !Objects.equals(attempt.entryMarker, EquipmentSessionMarker.read(attempt.player).orElse(null)))
|| !Arrays.equals(attempt.snapshot, snapshot(attempt.player))
@@ -667,6 +722,12 @@ public final class EquipmentSessions implements AutoCloseable {
pending.remove(attempt.owner, attempt);
active.remove(attempt.owner, attempt);
withdrawPresentation(attempt);
if (attempt.vitalsStarted) {
// Only native callbacks can reach this phase. Preserve observed character damage
// before independent vitals teardown; the uncertain equipment receipt stays owned.
saveRemainingHealth(attempt);
restoreVitals(attempt);
}
abandon(attempt);
diagnostic(attempt, failure);
}
@@ -723,6 +784,7 @@ public final class EquipmentSessions implements AutoCloseable {
}
cancelEntry(owner);
var attempt = new Attempt(player, owner, null);
attempt.recoveryOwner = previous;
// A recovered receipt has no process-local never-packed proof.
attempt.effectsStarted = true;
attempt.arrival = arrival;
@@ -735,6 +797,10 @@ public final class EquipmentSessions implements AutoCloseable {
if (!current(attempt)) return;
pending.remove(attempt.owner, attempt);
if (!attempt.player.isOnline()) return;
if (owned.get(attempt.owner) != attempt.recoveryOwner) {
notice(attempt.owner, "STALE_RECOVERY", "Session ownership changed after the recovery read; no old receipt was replayed.");
return;
}
if (failure != null) { diagnostic(attempt, failure); return; }
var receipt = state.map(EquipmentSessionStore.State::pending).orElse(null);
if (receipt == null) {
@@ -750,12 +816,15 @@ public final class EquipmentSessions implements AutoCloseable {
attempt.character = receipt.characterId();
attempt.session = receipt.sessionId();
attempt.previousGameMode = receipt.previousGameMode();
attempt.ordinaryVitals = receipt.ordinaryVitals();
attempt.vitalsStarted = attempt.ordinaryVitals != null;
attempt.healthAcknowledged = !attempt.vitalsStarted;
attempt.modeStarted = attempt.previousGameMode != null;
var previous = owned.get(attempt.owner);
if (previous != null && !attempt.session.equals(previous.session)) {
notice(attempt.owner, "SESSION_MISMATCH", "Saved and process-local sessions disagree; items were left unchanged."); return;
}
if (previous != null && previous.defeated) {
if (previous != null && (previous.defeated || previous.vitalsStarted)) {
// Explicit same-lifetime equipment recovery still uses matching receipt/PACKED
// proof. Keep the original death authority and its pending worker completion;
// do not replace it with a recovery Attempt that could release its claim early.
@@ -770,11 +839,13 @@ public final class EquipmentSessions implements AutoCloseable {
owned.put(attempt.owner, attempt);
transitioning.put(attempt.owner, attempt);
try {
saveRemainingHealth(attempt);
boolean modeRestored = restoreGameMode(attempt);
var result = restoreMarked(attempt);
boolean vitalsRestored = restoreVitals(attempt);
if (result == EquipmentStorage.RestoreResult.RESTORED) {
if (!modeRestored) attempt.gearRestoredForModeRecovery = true;
persistExit(attempt, modeRestored);
if (!modeRestored || !vitalsRestored || !attempt.healthAcknowledged) attempt.gearRestoredForModeRecovery = true;
persistExit(attempt, modeRestored && vitalsRestored);
sync(attempt);
} else notice(attempt.owner, result.name(), "Equipment recovery refused: " + result + "; receipt and items retained.");
} catch (RuntimeException error) {
@@ -0,0 +1,88 @@
package games.dmg.fantasystick.session;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.Consumer;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
/**
* Purpur 2618 instance-local effective-value lease. Base/modifiers remain in the native
* instance, so additions, removals, potion expiry and serialization keep their normal owners.
* The pinned dirty callback is synchronous: no polling interval can expose an external maximum.
* No global attribute/range is changed. Native compatibility tests must gate runtime upgrades.
*/
public final class NativeMaximumHealth implements AutoCloseable {
private static final Field DIRTY = field("dirty");
private static final Field VALUE = field("cachedValue");
private static final Field CALLBACK = field("onDirty");
private final AttributeInstance attribute;
private final Consumer<AttributeInstance> previous;
private final Consumer<AttributeInstance> callback = this::changed;
private final double maximum;
private boolean closed;
/** Exercise the pinned hook on a detached instance before exposing any player entry. */
public static void prepare() {
var probe = new AttributeInstance(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH, ignored -> {});
double bound = Math.min(10, NativeHealthLimit.read().doubleValue());
try (var lease = new NativeMaximumHealth(probe, bound)) {
Objects.requireNonNull(lease);
if (probe.getValue() != bound) throw new IllegalStateException("Native maximum hook did not apply");
probe.setBaseValue(probe.getBaseValue() + 1);
if (probe.getValue() != bound) throw new IllegalStateException("Native dirty hook did not retain the maximum");
}
if (probe.getValue() != probe.getAttribute().value().sanitizeValue(probe.getBaseValue())) {
throw new IllegalStateException("Native maximum hook did not restore ordinary calculation");
}
}
@SuppressWarnings("unchecked")
public NativeMaximumHealth(AttributeInstance attribute, double maximum) {
this.attribute = Objects.requireNonNull(attribute);
if (!Double.isFinite(maximum) || maximum <= 0) throw new IllegalArgumentException("Invalid fantasy maximum");
this.maximum = maximum;
try { previous = (Consumer<AttributeInstance>) CALLBACK.get(attribute); }
catch (ReflectiveOperationException failure) { throw incompatible(failure); }
try {
CALLBACK.set(attribute, callback);
changed(attribute);
} catch (ReflectiveOperationException | RuntimeException failure) {
try { close(); }
catch (RuntimeException cleanup) { failure.addSuppressed(cleanup); }
throw incompatible(failure);
}
}
private void changed(AttributeInstance changed) {
if (!closed) {
try {
VALUE.setDouble(attribute, maximum);
DIRTY.setBoolean(attribute, false);
} catch (ReflectiveOperationException failure) { throw incompatible(failure); }
}
previous.accept(changed);
}
@Override public void close() {
if (closed) return;
closed = true;
try {
// Never overwrite a later callback installed by someone else. If it wraps ours,
// our now-closed callback simply passes through and no longer overrides values.
if (CALLBACK.get(attribute) == callback) CALLBACK.set(attribute, previous);
DIRTY.setBoolean(attribute, true);
previous.accept(attribute);
} catch (ReflectiveOperationException failure) { throw incompatible(failure); }
}
private static Field field(String name) {
try {
var field = AttributeInstance.class.getDeclaredField(name);
field.setAccessible(true);
return field;
} catch (ReflectiveOperationException failure) { throw incompatible(failure); }
}
private static IllegalStateException incompatible(Exception failure) {
return new IllegalStateException("Unsupported native maximum-health internals; no fallback health policy", failure);
}
}
@@ -0,0 +1,41 @@
package games.dmg.fantasystick.session;
import java.util.Objects;
import java.util.function.Consumer;
import org.bukkit.Color;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
/** Public particles only; never grants gameplay state, protection, health or persistence authority. */
public final class SessionVisuals implements AutoCloseable {
private final EquipmentSessions sessions;
private final Consumer<Throwable> failures;
private BukkitTask task;
private boolean closed;
public SessionVisuals(EquipmentSessions sessions, Consumer<Throwable> failures) {
this.sessions = Objects.requireNonNull(sessions); this.failures = Objects.requireNonNull(failures);
}
public void install(Plugin plugin) {
if (closed || task != null) throw new IllegalStateException("Visual lifetime already used");
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
if (closed) return;
for (var player : plugin.getServer().getOnlinePlayers()) {
try {
var active = sessions.active(player.getUniqueId());
if (active.isPresent() && sessions.current(active.orElseThrow())) burst(player, Color.BLUE, 3);
} catch (RuntimeException failure) { failures.accept(failure); }
}
}, 1L, 10L);
}
public static void burst(Player player, Color color, int count) {
player.getWorld().spawnParticle(Particle.DUST, player.getLocation().add(0, 1, 0), count,
0.3, 0.4, 0.3, 0, new Particle.DustOptions(color, 1));
}
@Override public void close() {
closed = true;
if (task != null) { task.cancel(); task = null; }
}
}
@@ -0,0 +1,151 @@
package games.dmg.fantasystick.session;
import games.dmg.fantasystick.persistence.CharacterRecord;
import games.dmg.fantasystick.persistence.OrdinaryVitals;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
/** Server-thread native overlay; native attributes themselves are never snapshotted/replayed. */
public final class SessionVitals implements AutoCloseable {
private static final NamespacedKey KEY = new NamespacedKey("fantasystick", "vitals_session");
private enum Phase { APPLYING, APPLIED, RESTORING, RELEASED }
private record Mark(UUID session, Phase phase, double maximum, OrdinaryVitals ordinary) { }
private record Lease(UUID session, NativeMaximumHealth maximum) { }
private final Map<UUID, Lease> leases = new HashMap<>();
private boolean closed;
public SessionVitals() { NativeMaximumHealth.prepare(); }
public OrdinaryVitals capture(Player player) {
return new OrdinaryVitals(player.getHealth(), player.getFoodLevel(), player.getSaturation(), player.getExhaustion(),
player.isHealthScaled(), player.getHealthScale());
}
public boolean available(Player player) {
var mark = read(player);
return !closed && (!player.getPersistentDataContainer().has(KEY) || mark != null && mark.phase() == Phase.RELEASED);
}
public boolean unchanged(Player player, OrdinaryVitals ordinary) {
return ordinary != null && ordinary.health() > 0 && ordinary.equals(capture(player));
}
/** Caller already acknowledged this ordinary snapshot in its player/session receipt. */
public void enter(Player player, UUID session, OrdinaryVitals ordinary, CharacterRecord record) {
if (!available(player) || !unchanged(player, ordinary) || leases.containsKey(player.getUniqueId())) {
throw new IllegalStateException("Stale native vitals entry");
}
double maximum = record.maximumHealth().min(NativeHealthLimit.read()).doubleValue();
write(player, new Mark(session, Phase.APPLYING, maximum, ordinary));
var nativeAttribute = Objects.requireNonNull(((CraftPlayer) player).getHandle()
.getAttribute(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH));
try {
var lease = new NativeMaximumHealth(nativeAttribute, maximum);
leases.put(player.getUniqueId(), new Lease(session, lease));
// Equal to the actual native maximum, not virtual health. Native attribute packets
// use CraftPlayer's scaled snapshot, without exposing ordinary modifier arithmetic.
// Initialize once BEFORE display delivery can reenter damage/healing handlers.
player.setHealth(Math.min(record.health().doubleValue(), maximum));
if (closed) throw new IllegalStateException("Vitals lifetime ended during entry");
player.setHealthScale(maximum);
if (closed) throw new IllegalStateException("Vitals lifetime ended during entry");
write(player, new Mark(session, Phase.APPLIED, maximum, ordinary));
} catch (RuntimeException | LinkageError failure) {
try { releaseMaximum(player, session); }
catch (RuntimeException | LinkageError cleanup) { failure.addSuppressed(cleanup); }
try { restoreScale(player, maximum, ordinary); }
catch (RuntimeException | LinkageError cleanup) { failure.addSuppressed(cleanup); }
// Leave APPLYING proof and the acknowledged receipt: an uncertain health setter
// is not permission to heal from a stale snapshot or replay the character save.
throw failure;
}
}
public BigDecimal remaining(Player player, UUID session, OrdinaryVitals ordinary) {
var mark = read(player);
if (mark == null || !mark.session().equals(session) || !mark.ordinary().equals(ordinary) || mark.phase() != Phase.APPLIED) {
throw new IllegalStateException("No matching applied fantasy vitals proof");
}
return BigDecimal.valueOf(Math.max(0, Math.min(player.getHealth(), mark.maximum())));
}
public boolean restore(Player player, UUID session, OrdinaryVitals ordinary) {
if (ordinary == null) return true; // Legacy equipment-only receipt, not health restoration authority.
var mark = read(player);
if (mark == null || !mark.session().equals(session) || !mark.ordinary().equals(ordinary)
|| mark.phase() != Phase.APPLIED) {
// Process-local hook ownership is sufficient to retire our maximum override,
// never to replay ordinary health/nutrition when native setter proof is missing.
releaseMaximum(player, session);
return mark != null && mark.session().equals(session) && mark.ordinary().equals(ordinary) && mark.phase() == Phase.RELEASED;
}
write(player, new Mark(session, Phase.RESTORING, mark.maximum(), ordinary));
// Reconcile restored equipment's native modifiers before clamping ordinary health.
try { ((CraftPlayer) player).getHandle().detectEquipmentUpdates(); }
finally { releaseMaximum(player, session); }
// On restart there is no hook: native attribute serialization retained ordinary
// base/permanent modifiers, and the native effect lifecycle owns potion expiry/removal.
restoreScale(player, mark.maximum(), ordinary);
player.setFoodLevel(ordinary.food()); player.setSaturation(ordinary.saturation()); player.setExhaustion(ordinary.exhaustion());
player.setHealth(Math.min(ordinary.health(), Objects.requireNonNull(player.getAttribute(Attribute.MAX_HEALTH)).getValue()));
write(player, new Mark(session, Phase.RELEASED, mark.maximum(), ordinary));
return true;
}
private void releaseMaximum(Player player, UUID session) {
var lease = leases.get(player.getUniqueId());
if (lease != null && lease.session().equals(session)) {
leases.remove(player.getUniqueId(), lease);
lease.maximum().close();
}
}
private static void restoreScale(Player player, double maximum, OrdinaryVitals ordinary) {
if (player.isHealthScaled() && player.getHealthScale() == maximum) {
player.setHealthScale(ordinary.scale());
player.setHealthScaled(ordinary.scaled());
}
}
/** Only removes this process's effective-value hooks; never replays unknown ordinary snapshots. */
@Override public void close() {
closed = true;
RuntimeException errors = null;
for (var lease : java.util.List.copyOf(leases.values())) {
try { lease.maximum().close(); }
catch (RuntimeException | LinkageError failure) {
if (errors == null) errors = new IllegalStateException("Native maximum cleanup failed");
errors.addSuppressed(failure);
}
}
leases.clear();
if (errors != null) throw errors;
}
private static Mark read(Player player) {
var data = player.getPersistentDataContainer();
if (!data.has(KEY, PersistentDataType.STRING)) return null;
try {
String encoded = data.get(KEY, PersistentDataType.STRING);
if (encoded.length() > 400) return null;
String[] fields = encoded.split("\\|", -1);
if (fields.length != 5 || !fields[0].equals("1")) return null;
UUID session = UUID.fromString(fields[1]);
double maximum = Double.parseDouble(fields[3]);
if (!session.toString().equals(fields[1]) || !Double.isFinite(maximum) || maximum <= 0) return null;
return new Mark(session, Phase.valueOf(fields[2]), maximum, OrdinaryVitals.decode(fields[4]));
} catch (IllegalArgumentException invalid) { return null; }
}
private static void write(Player player, Mark mark) {
player.getPersistentDataContainer().set(KEY, PersistentDataType.STRING,
"1|" + mark.session() + "|" + mark.phase() + "|" + mark.maximum() + "|" + mark.ordinary().encode());
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ version: ${version}
main: games.dmg.fantasystick.FantasyStickPlugin
api-version: "26.2"
depend: [ProtocolLib]
description: Transferable character sheets and recoverable equipment sessions; other RPG gameplay is not activated.
description: Transferable characters, native fantasy health, recoverable equipment, character books and parties.
author: dmg.games
commands:
fantasystick:
@@ -101,7 +101,8 @@ class CharacterAnvilNamesNativeTest {
assertEquals("Tail of Mira of Dawn — 雪", book.getTitle());
var text = net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer.plainText();
assertTrue(text.serialize(book.customName()).contains("Mira of Dawn — 雪"));
assertTrue(book.pages().stream().map(text::serialize).anyMatch(page -> page.contains("Mira of Dawn — 雪")));
assertTrue(book.pages().stream().map(text::serialize).collect(java.util.stream.Collectors.joining())
.replace("\n", "").contains("Mira of Dawn — 雪"), "Readable wrapping must retain the full acknowledged name");
assertTrue(problems.isEmpty());
// Actor rejects all XP/cursor/inventory mutation calls and all worker-side player access.
}
@@ -111,7 +111,7 @@ class CharacterBookNativeTest {
}
}
@Test void valuesBeyondOneNativeBookRemainCompleteThroughBoundedSingleUseNavigation() throws Exception {
@Test void largeRecordsRemainCompleteThroughBoundedSingleUseNavigation() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
@@ -131,7 +131,7 @@ class CharacterBookNativeTest {
while (true) {
var content = org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(a.books.getLast())
.get(net.minecraft.core.component.DataComponents.WRITTEN_BOOK_CONTENT);
assertNotNull(content); assertTrue(content.pages().size() <= 100, "Never exceed the native book page limit");
assertNotNull(content); assertTrue(content.pages().size() <= 100, "Keep the chosen per-view payload bound (not a signed-book codec page limit)");
for (var page : content.pages()) if (!hasClick(page.raw())) all.append(page.raw().getString().replace("\n", ""));
if (!text(a.books.getLast()).contains("[Next pages]")) break;
assertTrue(++volumes < 10, "Navigation must terminate, not repeat a truncated volume");
@@ -1685,6 +1685,8 @@ class EquipmentSessionsNativeTest {
org.bukkit.GameMode gameMode = org.bukkit.GameMode.SURVIVAL;
Runnable beforeMode = () -> {};
double health = 20;
Runnable beforeHealth = () -> {};
Runnable healthMutation = () -> {};
float attackStrength = 1;
ItemStack activeItem = new ItemStack(Material.AIR);
int useStarts;
@@ -1729,7 +1731,7 @@ class EquipmentSessionsNativeTest {
useStarts++; activeItem = inventory.getItemInMainHand().clone(); startUse.run(); yield null;
}
case "clearActiveItem" -> { useClears++; activeItem = new ItemStack(Material.AIR); clearUse.run(); yield null; }
case "setHealth" -> { health = (Double) args[0]; yield null; }
case "setHealth" -> { beforeHealth.run(); health = (Double) args[0]; healthMutation.run(); yield null; }
case "getAttribute" -> { assertEquals(org.bukkit.attribute.Attribute.MAX_HEALTH, args[0]); yield maximum; }
case "teleport" -> {
teleports++; teleported = ((org.bukkit.Location) args[0]).clone(); teleport.run();
@@ -0,0 +1,384 @@
package games.dmg.fantasystick;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.persistence.CharacterStorage;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.bukkit.attribute.Attribute;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class FantasyHealthNativeTest {
@TempDir Path data;
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
@Test void nativeMaximumIgnoresBonusesPenaltiesAndHealthBoostWhileTheirOrdinaryOwnershipAndExpiryRemainIntact() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(a.core.id, a.player);
var bonus = modifier("bonus", 5, net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation.ADD_VALUE);
var penalty = modifier("penalty", -0.5, net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL);
a.maximumHealth.addTransientModifier(bonus); a.maximumHealth.addTransientModifier(penalty);
var boost = net.minecraft.world.effect.MobEffects.HEALTH_BOOST.value();
boost.addAttributeModifiers(a.healthAttributes, 1);
a.player.setHealth(12); a.player.setFoodLevel(7); a.player.setSaturation(2); a.player.setExhaustion(3);
f.activate(a);
assertEquals(10, a.maximumHealth.getValue());
a.player.setHealth(6);
a.maximumHealth.setBaseValue(30); // A later legitimate ordinary base change must survive exit.
var later = modifier("later", 4, net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation.ADD_VALUE);
a.maximumHealth.addPermanentModifier(later);
boost.addAttributeModifiers(a.healthAttributes, 2);
assertEquals(10, a.maximumHealth.getValue(), "New native potion modifiers cannot change fantasy maximum");
boost.removeAttributeModifiers(a.healthAttributes);
a.maximumHealth.removeModifier(bonus); a.maximumHealth.removeModifier(penalty);
var zero = modifier("zero", -1, net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL);
a.maximumHealth.addTransientModifier(zero);
assertEquals(10, a.maximumHealth.getValue(), "Even a zero multiplier cannot bypass the native effective-value lease");
a.maximumHealth.removeModifier(zero);
assertEquals(6, a.player.getHealth());
var serialized = a.maximumHealth.pack();
assertEquals(30, serialized.baseValue()); assertEquals(java.util.List.of(later), serialized.modifiers());
var loaded = new net.minecraft.world.entity.ai.attributes.AttributeInstance(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH, ignored -> {});
loaded.apply(serialized); assertEquals(34, loaded.getValue(), "Player saves retain ordinary attribute ownership, not the fantasy maximum");
a.player.setFoodLevel(2); a.player.setSaturation(0); a.player.setExhaustion(1);
a.core.inventory.setItemInOffHand(null); f.changed(a);
assertEquals(34, a.maximumHealth.getValue()); assertEquals(12, a.player.getHealth());
assertEquals(java.util.Set.of(later), a.maximumHealth.getModifiers(), "Expired/removed modifiers are not replayed from a snapshot");
assertEquals(7, a.player.getFoodLevel()); assertEquals(2, a.player.getSaturation()); assertEquals(3, a.player.getExhaustion());
assertFalse(a.player.isHealthScaled()); assertEquals(20, a.player.getHealthScale());
}
}
@Test void activeCharacterBookShowsCurrentNativeDamageRatherThanTheLastSavedHealth() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(a.core.id, a.player); f.activate(a);
a.player.setHealth(4);
PartyBookNativeTest.open(f, a);
assertTrue(PartyBookNativeTest.text(a).contains("Health: 4/10"));
}
}
@Test void activeBlueMistAndOneRedDefeatBurstUseWorldLocationsAndStopWithTheSession() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
var ordinary = new FantasyStickLifecycleNativeTest.Actor(data); ordinary.core.inventory.setItemInOffHand(null);
f.players.put(a.core.id, a.player); f.players.put(ordinary.core.id, ordinary.player);
var entry = a.player.getLocation(); f.activate(a);
f.pulses.forEach(Runnable::run);
assertTrue(dust(a, org.bukkit.Color.BLUE) > 0, "Active fantasy mode must have visible public blue particles");
assertEquals(0, dust(ordinary, org.bukkit.Color.BLUE));
a.core.location.add(20, 0, 20);
var defeatLocation = a.player.getLocation().add(0, 1, 0);
var source = new org.bukkit.craftbukkit.damage.CraftDamageSource(new net.minecraft.world.damagesource.DamageSource(
org.bukkit.craftbukkit.damage.CraftDamageType.bukkitToMinecraftHolder(org.bukkit.damage.DamageType.OUT_OF_WORLD)));
var death = new org.bukkit.event.entity.PlayerDeathEvent(a.player, source, new java.util.ArrayList<>(), 0,
net.kyori.adventure.text.Component.empty(), true);
f.dispatcher.accept(death); assertTrue(death.isCancelled());
PartyBookNativeTest.drain(f);
assertEquals(1, dust(a, org.bukkit.Color.RED));
assertTrue(org.mockito.Mockito.mockingDetails(a.world).getInvocations().stream().anyMatch(call ->
call.getMethod().getName().equals("spawnParticle") && java.util.Arrays.asList(call.getArguments()).contains(defeatLocation)));
assertEquals(entry, a.player.getLocation()); assertEquals(28, a.player.getHealth());
var tail = a.core.inventory.getItemInOffHand().clone();
assertEquals(org.bukkit.Material.WRITTEN_BOOK, tail.getType()); assertEquals(1, tail.getAmount());
long blue = dust(a, org.bukkit.Color.BLUE);
f.pulses.forEach(Runnable::run); f.dispatcher.accept(death);
assertEquals(blue, dust(a, org.bukkit.Color.BLUE)); assertEquals(1, dust(a, org.bukkit.Color.RED));
assertEquals(tail, a.core.inventory.getItemInOffHand());
f.plugin.setEnabled(false); f.awaitWorkers();
f.pulses.forEach(Runnable::run);
assertEquals(blue, dust(a, org.bukkit.Color.BLUE), "Retired timers cannot emit fantasy effects");
}
}
@Test void uncappedConstitutionEntersAtTheNativeCeilingAndPersistsOnlyBoundedRemainingHealth() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(a.core.id, a.player);
var old = a.core.record;
var con = java.math.BigInteger.TEN.pow(1000);
var stats = new java.util.EnumMap<games.dmg.fantasystick.progression.Stat, java.math.BigInteger>(games.dmg.fantasystick.progression.Stat.class);
stats.putAll(old.stats()); stats.put(games.dmg.fantasystick.progression.Stat.CON, con);
var saved = new games.dmg.fantasystick.persistence.CharacterRecord(old.id(), old.name(), old.revision().add(java.math.BigInteger.ONE),
stats, old.level(), old.experience(), old.unspentPoints(), new java.math.BigDecimal(con.add(java.math.BigInteger.TEN)),
old.learned(), old.loadout(), old.cooldowns(), false, null, null);
new games.dmg.fantasystick.persistence.YamlCharacterRepository(data).replace(saved, old.revision());
var ceiling = games.dmg.fantasystick.session.NativeHealthLimit.read();
f.activate(a);
assertEquals(ceiling.doubleValue(), a.maximumHealth.getValue()); assertEquals(ceiling.doubleValue(), a.player.getHealth());
a.player.setHealth(3.25);
a.core.inventory.setItemInOffHand(null); f.changed(a);
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
var suspended = characters.find(old.id()).get(5, TimeUnit.SECONDS).orElseThrow();
assertEquals(con, suspended.stats().get(games.dmg.fantasystick.progression.Stat.CON));
assertEquals(0, suspended.health().compareTo(new java.math.BigDecimal("3.25")));
assertEquals(ceiling, games.dmg.fantasystick.session.NativeHealthLimit.read(), "Never mutate global attribute limits");
}
}
@Test void serializedInterruptedSessionRecoversOrdinaryVitalsAndRemainingHealthWithoutReplayingExpiredModifiers() throws Exception {
FantasyStickLifecycleNativeTest.Actor a;
byte[] nativeProof, characterFile, receiptFile;
byte[][] items;
net.minecraft.world.entity.ai.attributes.AttributeInstance.Packed ordinaryAttributes;
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
a = new FantasyStickLifecycleNativeTest.Actor(data); f.players.put(a.core.id, a.player);
a.player.setHealth(12); a.player.setFoodLevel(7); a.player.setSaturation(2); a.player.setExhaustion(3);
net.minecraft.world.effect.MobEffects.HEALTH_BOOST.value().addAttributeModifiers(a.healthAttributes, 1);
f.activate(a); a.player.setHealth(4); a.player.setFoodLevel(2);
nativeProof = a.core.pdc.serializeToBytes();
items = java.util.Arrays.stream(a.core.snapshot()).map(item -> item == null ? null : item.serializeAsBytes()).toArray(byte[][]::new);
ordinaryAttributes = a.maximumHealth.pack();
characterFile = java.nio.file.Files.readAllBytes(data.resolve("characters").resolve(a.core.record.id() + ".yml"));
receiptFile = java.nio.file.Files.readAllBytes(data.resolve("sessions").resolve(a.core.id + ".yml"));
}
// Recreate one concrete crash window from native item/PDC/attribute and YAML saves.
// Orderly fixture disposal above is not mistaken for an actual server crash.
java.nio.file.Files.write(data.resolve("characters").resolve(a.core.record.id() + ".yml"), characterFile);
java.nio.file.Files.write(data.resolve("sessions").resolve(a.core.id + ".yml"), receiptFile);
a.core.inventory.setContents(java.util.Arrays.stream(items).map(bytes -> bytes == null ? null : org.bukkit.inventory.ItemStack.deserializeBytes(bytes))
.toArray(org.bukkit.inventory.ItemStack[]::new));
a.core.pdc.readFromBytes(nativeProof, true);
a.maximumHealth.removeModifiers(); a.maximumHealth.apply(ordinaryAttributes);
// Native potion attributes use permanent storage. Exercise the real expiry/removal
// hook after load; plugin recovery must not reintroduce that removed modifier.
net.minecraft.world.effect.MobEffects.HEALTH_BOOST.value().removeAttributeModifiers(a.healthAttributes);
a.core.health = 4; a.food.setFoodLevel(2); a.healthScaled = true; a.healthScale = 10;
a.core.gameMode = org.bukkit.GameMode.ADVENTURE;
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.players.put(a.core.id, a.player); f.plugin.setEnabled(true); f.tick();
PartyBookNativeTest.drain(f);
var receipt = f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS).orElseThrow();
assertNull(receipt.pending());
assertEquals(org.bukkit.Material.DIAMOND_SWORD, a.core.inventory.getItem(0).getType());
assertEquals(28, a.maximumHealth.getValue()); assertTrue(a.maximumHealth.getModifiers().isEmpty());
assertEquals(12, a.player.getHealth()); assertEquals(7, a.player.getFoodLevel());
assertEquals(2, a.player.getSaturation()); assertEquals(3, a.player.getExhaustion());
assertFalse(a.player.isHealthScaled()); assertEquals(0, a.core.teleports);
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
assertEquals(0, characters.find(a.core.record.id()).get(5, TimeUnit.SECONDS).orElseThrow().health().compareTo(java.math.BigDecimal.valueOf(4)));
// An unrelated later ordinary change cannot be overwritten by a repeated recovery.
a.player.setHealth(5); a.player.setFoodLevel(9);
((games.dmg.fantasystick.session.EquipmentSessions) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "sessions")).recover(a.player);
f.tick(); assertEquals(5, a.player.getHealth()); assertEquals(9, a.player.getFoodLevel());
}
}
@Test void failedEntrySettersAndMissingExitProofNeverLeaveTheNativeMaximumOverridden() throws Exception {
for (int route = 0; route < 6; route++) try (var f = new FantasyStickLifecycleNativeTest.Fixture(data.resolve("failure-" + route))) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data.resolve("failure-" + route));
f.players.put(a.core.id, a.player);
Runnable fail = () -> { a.core.beforeHealth = () -> {}; a.core.healthMutation = () -> {}; throw new IllegalStateException("Native health setter failed"); };
if (route == 0) a.core.beforeHealth = fail;
if (route == 1) a.core.healthMutation = fail;
if (route == 5) a.core.sync = () -> {
a.core.sync = () -> {}; a.player.setHealth(3);
throw new IllegalStateException("Activation client synchronization failed after native damage");
};
f.activate(a);
if (route >= 2 && route < 5) {
a.player.setHealth(4); a.player.setFoodLevel(2);
if (route == 2) a.core.pdc.remove(new org.bukkit.NamespacedKey("fantasystick", "vitals_session"));
else if (route == 3) org.mockito.Mockito.doThrow(new IllegalStateException("Native equipment update failed")).when(a.nativePlayer).detectEquipmentUpdates();
else a.core.beforeMutation = () -> { throw new IllegalStateException("Equipment restoration write failed"); };
a.core.inventory.setItemInOffHand(null); f.changed(a);
if (route < 4) {
assertEquals(4, a.player.getHealth(), "Missing/uncertain proof cannot authorize a stale ordinary-health replay");
assertEquals(2, a.player.getFoodLevel());
}
}
var sessions = (games.dmg.fantasystick.session.EquipmentSessions) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "sessions");
assertTrue(sessions.active(a.core.id).isEmpty());
assertEquals(28, a.maximumHealth.getValue(), "Only the owned maximum hook is safe to remove despite an uncertain setter/proof");
assertNotNull(f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS).orElseThrow().pending());
if (route < 2) {
assertEquals(route == 0 ? 20 : 10, a.player.getHealth(), "Do not blindly heal an uncertain native write");
assertFalse(a.player.isHealthScaled()); assertEquals(20, a.player.getHealthScale());
}
if (route >= 4) {
assertEquals(20, a.player.getHealth(), "Proven vitals cleanup is independent of uncertain equipment/client state");
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
assertEquals(0, characters.find(a.core.record.id()).get(5, TimeUnit.SECONDS).orElseThrow().health()
.compareTo(java.math.BigDecimal.valueOf(route == 4 ? 4 : 3)));
}
}
}
@Test void failedHealthWriteRestoresOrdinaryGearButRetainsTheReceiptAndCannotResumeStaleHealth() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data); f.players.put(a.core.id, a.player); f.activate(a);
var sheet = a.core.inventory.getItemInOffHand().clone(); a.player.setHealth(4);
var path = data.resolve("characters").resolve(a.core.record.id() + ".yml");
byte[] acknowledged = java.nio.file.Files.readAllBytes(path);
try (var channel = java.nio.channels.FileChannel.open(data.resolve("characters/.records.lock"), java.nio.file.StandardOpenOption.WRITE);
var lock = channel.lock()) {
assertTrue(lock.isValid());
a.core.inventory.setItemInOffHand(null); f.changed(a); PartyBookNativeTest.drain(f);
assertNotNull(f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS).orElseThrow().pending());
assertArrayEquals(acknowledged, java.nio.file.Files.readAllBytes(path));
}
assertEquals(org.bukkit.Material.DIAMOND_SWORD, a.core.inventory.getItem(0).getType());
assertEquals(28, a.maximumHealth.getValue()); assertEquals(20, a.player.getHealth());
a.core.inventory.setItemInOffHand(sheet); f.changed(a); f.tick();
var sessions = (games.dmg.fantasystick.session.EquipmentSessions) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "sessions");
assertTrue(sessions.active(a.core.id).isEmpty(), "An unacknowledged health save cannot become a free heal on reentry");
assertArrayEquals(acknowledged, java.nio.file.Files.readAllBytes(path));
assertTrue(a.messages.stream().anyMatch(message -> message.contains("persistence or recovery is pending")));
}
}
@Test void delayedRecoveryReadCannotRecreateOwnershipAfterTheHealthSaveAndExitAlreadyCompleted() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data); f.players.put(a.core.id, a.player); f.activate(a);
var sheet = a.core.inventory.getItemInOffHand().clone(); a.player.setHealth(4);
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
var worker = (java.util.concurrent.ExecutorService) FantasyStickLifecycleNativeTest.Fixture.field(characters, "worker");
var entered = new java.util.concurrent.CountDownLatch(1); var release = new java.util.concurrent.CountDownLatch(1);
var gate = worker.submit(() -> {
entered.countDown();
try { assertTrue(release.await(5, TimeUnit.SECONDS)); }
catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new AssertionError(interrupted); }
});
try {
assertTrue(entered.await(5, TimeUnit.SECONDS));
a.core.inventory.setItemInOffHand(null); f.changed(a);
assertNotNull(f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS).orElseThrow().pending());
var sessions = (games.dmg.fantasystick.session.EquipmentSessions) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "sessions");
sessions.recover(a.player);
var stale = f.tasks.poll(5, TimeUnit.SECONDS); assertNotNull(stale);
release.countDown(); gate.get(5, TimeUnit.SECONDS);
characters.find(a.core.record.id()).get(5, TimeUnit.SECONDS);
assertNull(f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS).orElseThrow().pending());
stale.run();
a.core.inventory.setItemInOffHand(sheet); f.changed(a); f.tick();
assertFalse(a.messages.stream().anyMatch(message -> message.contains("persistence or recovery is pending")),
"A delayed old receipt must not manufacture a new ownership blocker after acknowledged exit");
f.tick(); f.tick(); assertTrue(sessions.active(a.core.id).isPresent()); assertEquals(4, a.player.getHealth());
} finally { release.countDown(); }
}
}
@Test void nativeHealthChangesDuringClientScaleDeliveryAreNotOverwrittenByInitialActivation() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data); f.players.put(a.core.id, a.player);
a.scaleMutation = () -> {
a.scaleMutation = () -> {};
a.player.setHealth(Math.min(a.player.getHealth(), a.maximumHealth.getValue()) - 1);
};
f.activate(a);
assertEquals(9, a.player.getHealth(), "Native damage during a reentrant client update must not be replaced with another full-health initialization");
}
}
@Test void quitAndDisablePersistDamageAndRestoreOnlyOwnedOrdinaryVitalsAndDisplay() throws Exception {
for (boolean quit : new boolean[]{true, false}) try (var f = new FantasyStickLifecycleNativeTest.Fixture(data.resolve("lifecycle-" + quit))) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data.resolve("lifecycle-" + quit)); f.players.put(a.core.id, a.player);
a.player.setHealth(12); a.player.setFoodLevel(7); a.player.setSaturation(2); a.player.setExhaustion(3);
f.activate(a); a.player.setHealth(4); a.player.setFoodLevel(1);
a.maximumHealth.setBaseValue(8); // A legitimate later ordinary maximum, below the pre-entry health.
a.player.setHealthScale(44); // A later display owner's value is not ours to undo.
if (quit) {
a.core.online = false;
f.dispatcher.accept(new org.bukkit.event.player.PlayerQuitEvent(a.player, net.kyori.adventure.text.Component.empty()));
PartyBookNativeTest.drain(f);
f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS);
} else { f.plugin.setEnabled(false); f.awaitWorkers(); }
assertEquals(8, a.maximumHealth.getValue()); assertEquals(8, a.player.getHealth());
assertEquals(7, a.player.getFoodLevel()); assertEquals(2, a.player.getSaturation()); assertEquals(3, a.player.getExhaustion());
assertEquals(44, a.player.getHealthScale()); assertTrue(a.player.isHealthScaled());
assertEquals(org.bukkit.Material.DIAMOND_SWORD, a.core.inventory.getItem(0).getType());
var saved = new games.dmg.fantasystick.persistence.YamlCharacterRepository(data.resolve("lifecycle-" + quit)).load(a.core.record.id()).orElseThrow();
assertFalse(saved.dead()); assertEquals(0, saved.health().compareTo(java.math.BigDecimal.valueOf(4))); assertEquals(0, a.core.teleports);
}
}
@Test void aDamagedTransferredCharacterNeverTransfersItsFormerHoldersOrdinaryVitals() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data); var b = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(a.core.id, a.player); f.players.put(b.core.id, b.player);
a.player.setHealth(12); a.player.setFoodLevel(7);
b.player.setHealth(19); b.player.setFoodLevel(3);
var sheet = a.core.inventory.getItemInOffHand().clone(); f.activate(a); a.player.setHealth(3);
a.core.inventory.setItemInOffHand(null); f.changed(a); PartyBookNativeTest.drain(f);
f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS);
b.core.inventory.setItemInOffHand(sheet); f.activate(b);
assertEquals(3, b.player.getHealth()); assertEquals(12, a.player.getHealth()); assertEquals(7, a.player.getFoodLevel());
b.player.setHealth(2); b.core.inventory.setItemInOffHand(null); f.changed(b); PartyBookNativeTest.drain(f);
assertEquals(19, b.player.getHealth()); assertEquals(3, b.player.getFoodLevel());
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
assertEquals(0, characters.find(a.core.record.id()).get(5, TimeUnit.SECONDS).orElseThrow().health().compareTo(java.math.BigDecimal.valueOf(2)));
assertEquals(b.core.record, characters.find(b.core.record.id()).get(5, TimeUnit.SECONDS).orElseThrow());
}
}
@Test void shuttingDownMaximumHooksContinuesAfterAnOriginalNativeObserverThrows() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var throwNow = new java.util.concurrent.atomic.AtomicBoolean();
var attributes = new java.util.ArrayList<net.minecraft.world.entity.ai.attributes.AttributeInstance>();
for (int i = 0; i < 2; i++) {
var a = new FantasyStickLifecycleNativeTest.Actor(data); f.players.put(a.core.id, a.player);
var attribute = new net.minecraft.world.entity.ai.attributes.AttributeInstance(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH,
ignored -> { if (throwNow.get()) throw new IllegalStateException("Native dirty observer failed during cleanup"); });
attributes.add(attribute);
org.mockito.Mockito.when(a.nativePlayer.getAttribute(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH)).thenReturn(attribute);
f.activate(a); assertEquals(10, attribute.getValue());
}
throwNow.set(true);
var vitals = (games.dmg.fantasystick.session.SessionVitals) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "vitals");
var failure = assertThrows(IllegalStateException.class, vitals::close);
assertEquals(2, failure.getSuppressed().length);
for (var attribute : attributes) assertEquals(20, attribute.getValue(), "Every player's hook must be retired despite another cleanup failure");
}
}
private static long dust(FantasyStickLifecycleNativeTest.Actor a, org.bukkit.Color color) {
return org.mockito.Mockito.mockingDetails(a.world).getInvocations().stream().filter(call ->
call.getMethod().getName().equals("spawnParticle") && java.util.Arrays.stream(call.getArguments())
.anyMatch(argument -> argument instanceof org.bukkit.Particle.DustOptions dust && dust.getColor().equals(color))).count();
}
private static net.minecraft.world.entity.ai.attributes.AttributeModifier modifier(String name, double amount,
net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation operation) {
return new net.minecraft.world.entity.ai.attributes.AttributeModifier(net.minecraft.resources.Identifier.fromNamespaceAndPath("test", name), amount, operation);
}
@Test void actualOffhandEntryAppliesFantasyHealthAndExitRestoresOrdinaryHealthWithoutHealingTheCharacter() throws Exception {
try (var f = new FantasyStickLifecycleNativeTest.Fixture(data)) {
f.plugin.setEnabled(true);
var a = new FantasyStickLifecycleNativeTest.Actor(data);
f.players.put(a.core.id, a.player);
double ordinaryHealth = a.player.getHealth();
double ordinaryMaximum = a.player.getAttribute(Attribute.MAX_HEALTH).getValue();
var sheet = a.core.inventory.getItemInOffHand().clone();
f.activate(a);
assertEquals(10, a.player.getAttribute(Attribute.MAX_HEALTH).getValue());
assertEquals(10, a.player.getHealth(), "A fresh character starts full, independently of ordinary health");
a.player.setHealth(4);
a.core.inventory.setItemInOffHand(null); f.changed(a);
assertEquals(ordinaryMaximum, a.player.getAttribute(Attribute.MAX_HEALTH).getValue());
assertEquals(ordinaryHealth, a.player.getHealth());
var characters = (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(f.plugin, "characters");
var saved = characters.find(a.core.record.id()).get(5, TimeUnit.SECONDS).orElseThrow();
assertEquals(0, saved.health().compareTo(java.math.BigDecimal.valueOf(4)));
f.receipts().find(a.core.id).toCompletableFuture().get(5, TimeUnit.SECONDS);
a.core.inventory.setItemInOffHand(sheet); f.activate(a);
assertEquals(4, a.player.getHealth(), "Resuming does not replay a first-activation heal");
}
}
}
@@ -60,7 +60,7 @@ class FantasyStickLifecycleNativeTest {
assertTrue(fixture.order.indexOf("CharacterChatNames") >= 0);
assertTrue(fixture.order.indexOf("CharacterChatNames") < recipe);
assertNotNull(fixture.packetListener);
assertEquals(2, fixture.timers.size(), "Presentation and bounded combat-provenance cleanup");
assertEquals(3, fixture.timers.size(), "Presentation, combat-provenance cleanup and session particles");
assertTrue(fixture.order.indexOf("PartyCombat") >= 0);
assertTrue(fixture.order.indexOf("PartyCombat") < recipe);
assertNotNull(fixture.command);
@@ -874,6 +874,14 @@ class FantasyStickLifecycleNativeTest {
var oldSessions = (games.dmg.fantasystick.session.EquipmentSessions) Fixture.field(f.plugin, "sessions");
var worker = (java.util.concurrent.ExecutorService) Fixture.field(oldCharacters, "worker");
var receiptWorker = (java.util.concurrent.ExecutorService) Fixture.field(f.receipts(), "worker");
// Finish owned health/receipt durability before deliberately overlapping ONLY a
// rename with a new plugin lifetime. A pending health save now correctly retains
// the character claim; it is not safe authority to start a replacement session.
var transferredSheet = a.core.inventory.getItemInOffHand().clone();
a.core.inventory.setItemInOffHand(null); f.changed(a);
oldCharacters.find(a.core.record.id()).get(5, java.util.concurrent.TimeUnit.SECONDS);
f.receipts().find(a.core.id).toCompletableFuture().get(5, java.util.concurrent.TimeUnit.SECONDS);
a.core.inventory.setItemInOffHand(transferredSheet);
var entered = new java.util.concurrent.CountDownLatch(1); var release = new java.util.concurrent.CountDownLatch(1);
var gate = worker.submit(() -> {
entered.countDown();
@@ -1178,6 +1186,14 @@ class FantasyStickLifecycleNativeTest {
final List<String> messages = new ArrayList<>();
final List<org.bukkit.inventory.ItemStack> books = new ArrayList<>();
final List<Integer> particles = new ArrayList<>();
final net.minecraft.world.entity.ai.attributes.AttributeMap healthAttributes = new net.minecraft.world.entity.ai.attributes.AttributeMap(
net.minecraft.world.entity.ai.attributes.AttributeSupplier.builder().add(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH).build());
final net.minecraft.world.entity.ai.attributes.AttributeInstance maximumHealth = java.util.Objects.requireNonNull(
healthAttributes.getInstance(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH));
final net.minecraft.world.food.FoodData food = new net.minecraft.world.food.FoodData();
boolean healthScaled;
double healthScale = 20;
Runnable scaleMutation = () -> {};
java.util.function.Consumer<org.bukkit.event.player.PlayerGameModeChangeEvent> modeEvents = ignored -> { };
final org.bukkit.craftbukkit.entity.CraftPlayer player;
final net.minecraft.server.level.ServerPlayer nativePlayer = mock(net.minecraft.server.level.ServerPlayer.class);
@@ -1205,6 +1221,21 @@ class FantasyStickLifecycleNativeTest {
case "getHeight" -> 1.8;
case "getTrackedBy" -> tracked;
case "canSee" -> true;
case "getAttribute" -> {
assertEquals(org.bukkit.attribute.Attribute.MAX_HEALTH, args[0]);
yield new org.bukkit.craftbukkit.attribute.CraftAttributeInstance(maximumHealth, org.bukkit.attribute.Attribute.MAX_HEALTH);
}
case "getMaxHealth" -> maximumHealth.getValue();
case "getFoodLevel" -> food.getFoodLevel();
case "setFoodLevel" -> { food.setFoodLevel((Integer) args[0]); yield null; }
case "getSaturation" -> food.getSaturationLevel();
case "setSaturation" -> { food.setSaturation((Float) args[0]); yield null; }
case "getExhaustion" -> food.exhaustionLevel;
case "setExhaustion" -> { food.exhaustionLevel = (Float) args[0]; yield null; }
case "isHealthScaled" -> healthScaled;
case "getHealthScale" -> healthScale;
case "setHealthScale" -> { healthScale = (Double) args[0]; healthScaled = true; scaleMutation.run(); yield null; }
case "setHealthScaled" -> { healthScaled = (Boolean) args[0]; yield null; }
case "getGameMode" -> core.gameMode;
case "setGameMode" -> {
var event = new org.bukkit.event.player.PlayerGameModeChangeEvent((org.bukkit.entity.Player) call.getMock(), (org.bukkit.GameMode) args[0]);
@@ -1224,6 +1255,9 @@ class FantasyStickLifecycleNativeTest {
when(nativePlayer.getId()).thenReturn(42);
when(nativePlayer.getUUID()).thenReturn(core.id);
when(nativePlayer.getTabListDisplayName()).thenReturn(net.minecraft.network.chat.Component.literal("Ordinary tab"));
maximumHealth.setBaseValue(core.normalMaximum);
when(nativePlayer.getAttributes()).thenReturn(healthAttributes);
when(nativePlayer.getAttribute(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH)).thenReturn(maximumHealth);
distance.setBaseValue(64);
when(nativePlayer.getAttribute(net.minecraft.world.entity.ai.attributes.Attributes.NAME_TAG_DISTANCE)).thenReturn(distance);
var level = mock(net.minecraft.server.level.ServerLevel.class);
@@ -0,0 +1,57 @@
package games.dmg.fantasystick;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.session.NativeMaximumHealth;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class NativeMaximumHealthNativeTest {
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
@Test void startupProbeExercisesPinnedInternalsWithoutChangingTheGlobalRangeOrOrdinaryInstances() {
var maximum = games.dmg.fantasystick.session.NativeHealthLimit.read();
var ordinary = new AttributeInstance(Attributes.MAX_HEALTH, ignored -> {});
double before = ordinary.getValue();
NativeMaximumHealth.prepare();
assertEquals(maximum, games.dmg.fantasystick.session.NativeHealthLimit.read());
assertEquals(before, ordinary.getValue()); assertTrue(ordinary.getModifiers().isEmpty());
}
@Test void nativePlayerHealthClampingAndClientAttributeSnapshotsUseTheLeasedMaximum() {
var attributes = new net.minecraft.world.entity.ai.attributes.AttributeMap(
net.minecraft.world.entity.ai.attributes.AttributeSupplier.builder().add(Attributes.MAX_HEALTH).build());
var maximum = java.util.Objects.requireNonNull(attributes.getInstance(Attributes.MAX_HEALTH));
var player = org.mockito.Mockito.mock(org.bukkit.craftbukkit.entity.CraftPlayer.class, org.mockito.Mockito.CALLS_REAL_METHODS);
var nativePlayer = org.mockito.Mockito.mock(net.minecraft.server.level.ServerPlayer.class, org.mockito.Mockito.CALLS_REAL_METHODS);
org.mockito.Mockito.doReturn(attributes).when(nativePlayer).getAttributes();
org.mockito.Mockito.doReturn(player).when(nativePlayer).getBukkitEntity();
org.mockito.Mockito.doReturn(nativePlayer).when(player).getHandle();
// Network/entity-data delivery is the boundary; run the actual native health methods.
org.mockito.Mockito.doNothing().when(player).updateScaledHealth();
org.mockito.Mockito.doNothing().when(player).updateScaledHealth(org.mockito.ArgumentMatchers.anyBoolean());
try (var lease = new NativeMaximumHealth(maximum, 10)) {
assertNotNull(lease);
player.setHealthScale(10);
nativePlayer.setHealth(100);
assertEquals(10, nativePlayer.getMaxHealth()); assertEquals(10, nativePlayer.getHealth());
nativePlayer.setHealth(6.25f); assertEquals(6.25, player.getHealth());
var packet = new net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket(42, java.util.List.of(player.getScaledMaxHealth()));
assertEquals(10, packet.getValues().getFirst().base()); assertTrue(packet.getValues().getFirst().modifiers().isEmpty());
assertEquals(20, maximum.getBaseValue(), "Ordinary attribute data is not replaced by the client presentation snapshot");
}
assertEquals(20, nativePlayer.getMaxHealth());
assertEquals(6.25, nativePlayer.getHealth(), "Removing the maximum lease does not itself heal");
}
@Test void failingOriginalNativeDirtyCallbackCannotLeaveAnUnownedMaximumOverride() {
var attribute = new AttributeInstance(Attributes.MAX_HEALTH, ignored -> { throw new IllegalStateException("Native observer failed"); });
assertEquals(20, attribute.getValue());
assertThrows(IllegalStateException.class, () -> new NativeMaximumHealth(attribute, 10));
assertEquals(20, attribute.getValue(), "Failed construction must not leave an unreachable effective-value lease");
assertThrows(IllegalStateException.class, () -> attribute.setBaseValue(24));
assertEquals(24, attribute.getValue(), "Subsequent legitimate native writes must not call a leaked lease");
}
}
@@ -390,8 +390,8 @@ class PartyCombatNativeTest {
static void exit(FantasyStickLifecycleNativeTest.Fixture f, FantasyStickLifecycleNativeTest.Actor a) throws Exception {
a.core.inventory.setItemInOffHand(null); f.changed(a);
PartyBookNativeTest.drain(f); // Health acknowledgment precedes clearing the equipment receipt.
f.receipts().find(a.core.id).toCompletableFuture().get(5, java.util.concurrent.TimeUnit.SECONDS);
PartyBookNativeTest.drain(f);
}
private FantasyStickLifecycleNativeTest.Actor actor(FantasyStickLifecycleNativeTest.Fixture f) throws Exception {
@@ -54,7 +54,34 @@ class TailBooksNativeTest {
assertTrue(meta.getTitle().length() <= 32);
assertFalse(Character.isHighSurrogate(meta.getTitle().charAt(meta.getTitle().length() - 1)));
assertEquals(Component.text("Tail of " + name), meta.displayName());
assertTrue(meta.pages().contains(Component.text("Tail of " + name)));
String text = meta.pages().stream().map(net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer.plainText()::serialize)
.collect(java.util.stream.Collectors.joining()).replace("\n", "");
assertTrue(text.contains("Tail of " + name), "Readable wrapping must retain every character of the full name");
}
@Test void giantStatsRemainReadableInOneSignedKeepsakeThroughTheActualNativeCodec() {
var old = fallen();
var stats = new EnumMap<Stat, BigInteger>(Stat.class); stats.putAll(old.stats());
String digits = "1234567890".repeat(4000);
stats.put(Stat.STR, new BigInteger(digits));
var saved = new CharacterRecord(old.id(), "雪🐈".repeat(80), old.revision(), stats, old.level(), old.experience(),
old.unspentPoints(), old.health(), old.learned(), old.loadout(), old.cooldowns(), true, old.deathId(), old.diedAt());
var book = new TailBooks().create(saved);
var ops = RegistryOps.create(NbtOps.INSTANCE, CraftRegistry.getMinecraftRegistry());
var encoded = net.minecraft.world.item.ItemStack.CODEC.encodeStart(ops, CraftItemStack.asNMSCopy(book)).getOrThrow();
var decoded = net.minecraft.world.item.ItemStack.CODEC.parse(ops, encoded).getOrThrow();
var content = decoded.get(DataComponents.WRITTEN_BOOK_CONTENT); assertNotNull(content);
assertTrue(content.pages().size() > 100, "Signed books are not subject to the writable-book 100-page cap");
var text = new StringBuilder();
for (var page : content.pages()) {
String value = page.raw().getString();
assertTrue(value.split("\\n", -1).length <= 13);
for (String line : value.split("\\n", -1)) assertTrue(line.codePoints().map(cp -> cp < 128 ? 6 : 9).sum() <= 108);
text.append(value.replace("\n", ""));
}
assertTrue(text.toString().contains(saved.name())); assertTrue(text.toString().contains(digits));
assertEquals(1, book.getAmount());
assertEquals(new TailBooks.Identity(saved.id(), saved.deathId()), new TailBooks().identify(CraftItemStack.asBukkitCopy(decoded)).orElseThrow());
}
@Test
@@ -0,0 +1,25 @@
package games.dmg.fantasystick.persistence;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.Test;
class OrdinaryVitalsTest {
@Test void canonicalOrdinarySnapshotRetainsExactHealthNutritionAndDisplayValues() {
var value = new OrdinaryVitals(12.25, 7, 2.5f, 3.75f, false, 20);
assertEquals(value, OrdinaryVitals.decode(value.encode()));
var scaled = new OrdinaryVitals(1, 0, 0, 0, true, 44);
assertEquals(scaled, OrdinaryVitals.decode(scaled.encode()));
}
@Test void malformedNonfiniteNegativeAndNoncanonicalSnapshotsCannotAuthorizeRestoration() {
for (String input : List.of("", "garbage", "x".repeat(300), "NaN;7;2.0;3.0;false;20.0",
"Infinity;7;2.0;3.0;false;20.0", "-1.0;7;2.0;3.0;false;20.0", "12.0;-1;2.0;3.0;false;20.0",
"12.0;21;2.0;3.0;false;20.0", "12.0;7;NaN;3.0;false;20.0", "12.0;7;2.0;-1.0;false;20.0",
"12.0;7;2.0;Infinity;false;20.0", "12.0;7;2.0;3.0;yes;20.0", "12.0;7;2.0;3.0;false;0.0",
"12;7;2.0;3.0;false;20.0", "12.0;7;2.0;3.0;false;20.0;extra")) {
assertThrows(IllegalArgumentException.class, () -> OrdinaryVitals.decode(input), input);
}
}
}