187 lines
9.5 KiB
Java
187 lines
9.5 KiB
Java
package games.dmg.fantasystick.passive;
|
|
|
|
import games.dmg.fantasystick.session.EquipmentSessions;
|
|
import games.dmg.fantasystick.events.PassivePotionEventBridge;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.potion.PotionEffect;
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
/** Bounded current-character ownership for one persistent native potion layer. */
|
|
final class PersistentPotionEffect implements AutoCloseable {
|
|
private static final int MAX_ACTIVE_CHECKS = 256;
|
|
|
|
private final class State {
|
|
final EquipmentSessions.Active active;
|
|
final Player player;
|
|
PotionEffect owned;
|
|
PotionEffect displaced;
|
|
long displacedUntil;
|
|
long refreshedAt;
|
|
long applications;
|
|
PassivePotionEventBridge.Application application;
|
|
State(EquipmentSessions.Active active, Player player) { this.active = active; this.player = player; }
|
|
|
|
void refresh() {
|
|
refreshedAt = sweepGeneration;
|
|
if (displaced != null && !displaced.isInfinite() && sweepGeneration >= displacedUntil) displaced = null;
|
|
PotionEffect current;
|
|
try { current = player.getPotionEffect(type); }
|
|
catch (RuntimeException unavailable) { return; }
|
|
if (owned != null && !same(current, owned)) {
|
|
endAudit("native_layer_replaced_or_removed", current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
|
owned = null; displaced = null;
|
|
}
|
|
if (!eligible()) { remove(endReason()); return; }
|
|
if (current != null && (!replaceWeaker || current.getAmplifier() >= amplifier)) return;
|
|
var effect = new PotionEffect(type, PotionEffect.INFINITE_DURATION, amplifier, false, false, true);
|
|
try {
|
|
if (player.addPotionEffect(effect, true)) {
|
|
owned = effect; displaced = current;
|
|
displacedUntil = current == null ? 0 : current.isInfinite()
|
|
? Long.MAX_VALUE : sweepGeneration + current.getDuration();
|
|
if (events != null) {
|
|
try {
|
|
var record = sessions.currentRecord(active).orElse(null);
|
|
if (record != null) application = events.applied(passive, player, active, record.name(), owned, current,
|
|
applications == 0 ? "learned_current_session" : "owned_layer_reestablished");
|
|
} catch (RuntimeException ignored) { /* Audit capture cannot discard native ownership. */ }
|
|
}
|
|
applications++;
|
|
}
|
|
} catch (RuntimeException unavailable) { owned = null; displaced = null; }
|
|
}
|
|
|
|
boolean eligible() {
|
|
if (closed || !sessions.current(active)) return false;
|
|
var record = sessions.currentRecord(active).orElse(null);
|
|
return record != null && !record.dead() && record.passives().contains(passive);
|
|
}
|
|
|
|
String endReason() {
|
|
if (events == null) return "owner_no_longer_eligible";
|
|
try {
|
|
if (closed) return "plugin_shutdown";
|
|
if (!sessions.current(active)) return "session_no_longer_current";
|
|
var record = sessions.currentRecord(active).orElse(null);
|
|
if (record != null && record.dead()) return "character_defeated";
|
|
return "passive_unlearned_or_character_unavailable";
|
|
} catch (RuntimeException ignored) { return "owner_no_longer_eligible"; }
|
|
}
|
|
void endAudit(String reason, String disposition, PotionEffect current, boolean restored) {
|
|
if (events != null && application != null) events.ended(application, player, reason, disposition, current, restored);
|
|
application = null;
|
|
}
|
|
void remove(String reason) {
|
|
if (owned != null) {
|
|
PotionEffect current;
|
|
try { current = player.getPotionEffect(type); }
|
|
catch (RuntimeException unavailable) { return; }
|
|
if (same(current, owned)) {
|
|
try {
|
|
player.removePotionEffect(type);
|
|
boolean restored = false;
|
|
if (displaced != null && (displaced.isInfinite() || sweepGeneration < displacedUntil)) {
|
|
int duration = displaced.isInfinite() ? PotionEffect.INFINITE_DURATION
|
|
: Math.toIntExact(Math.min(Integer.MAX_VALUE, displacedUntil - sweepGeneration));
|
|
restored = player.addPotionEffect(new PotionEffect(displaced.getType(), duration, displaced.getAmplifier(),
|
|
displaced.isAmbient(), displaced.hasParticles(), displaced.hasIcon()), true);
|
|
}
|
|
if (events != null) {
|
|
try {
|
|
var after = player.getPotionEffect(type);
|
|
if (!same(after, owned)) endAudit(reason, "owned_layer_removed", after, restored);
|
|
} catch (RuntimeException unavailable) { /* No native acknowledgement: do not invent an end record. */ }
|
|
}
|
|
} catch (RuntimeException unavailable) { return; }
|
|
} else endAudit(reason, current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
|
owned = null; displaced = null;
|
|
}
|
|
states.remove(active.player(), this);
|
|
}
|
|
}
|
|
|
|
private final String passive;
|
|
private final PotionEffectType type;
|
|
private final int amplifier;
|
|
private final boolean replaceWeaker;
|
|
private final EquipmentSessions sessions;
|
|
private final Map<UUID, State> states = new java.util.HashMap<>();
|
|
private int activeCursor;
|
|
private int stateCursor;
|
|
private long sweepGeneration;
|
|
private org.bukkit.scheduler.BukkitTask sweepTask;
|
|
private boolean closed;
|
|
private PassivePotionEventBridge events;
|
|
|
|
void attachEvents(PassivePotionEventBridge bridge) {
|
|
if (closed || events != null || !states.isEmpty()) throw new IllegalStateException("Passive potion events already attached or evaluation started");
|
|
events = Objects.requireNonNull(bridge);
|
|
}
|
|
|
|
PersistentPotionEffect(String passive, PotionEffectType type, int amplifier, boolean replaceWeaker,
|
|
EquipmentSessions sessions) {
|
|
this.passive = Objects.requireNonNull(passive); this.type = Objects.requireNonNull(type);
|
|
if (passive.isBlank() || amplifier < 0) throw new IllegalArgumentException("Invalid persistent potion effect");
|
|
this.amplifier = amplifier; this.replaceWeaker = replaceWeaker; this.sessions = Objects.requireNonNull(sessions);
|
|
}
|
|
|
|
void install(org.bukkit.plugin.Plugin plugin) {
|
|
Objects.requireNonNull(plugin);
|
|
if (closed || sweepTask != null) throw new IllegalStateException("Passive potion sweep already installed or closed");
|
|
sweepTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this::sweep, 1, 1);
|
|
}
|
|
|
|
void sweep() {
|
|
if (closed) return;
|
|
sweepGeneration++;
|
|
var active = sessions.activeCharacters();
|
|
int activeChecks = Math.min(MAX_ACTIVE_CHECKS, active.size());
|
|
for (int index = 0; index < activeChecks; index++) {
|
|
var snapshot = active.get((activeCursor + index) % active.size());
|
|
reconcile(snapshot);
|
|
}
|
|
activeCursor = active.isEmpty() ? 0 : (activeCursor + activeChecks) % active.size();
|
|
|
|
var known = List.copyOf(states.values());
|
|
int stateChecks = Math.min(MAX_ACTIVE_CHECKS, known.size());
|
|
for (int index = 0; index < stateChecks; index++) {
|
|
var state = known.get((stateCursor + index) % known.size());
|
|
if (state.refreshedAt != sweepGeneration) state.refresh();
|
|
}
|
|
stateCursor = known.isEmpty() ? 0 : (stateCursor + stateChecks) % known.size();
|
|
}
|
|
|
|
private void reconcile(EquipmentSessions.ActiveCharacter snapshot) {
|
|
var active = snapshot.session();
|
|
var state = states.get(active.player());
|
|
boolean learned = !snapshot.record().dead() && snapshot.record().passives().contains(passive);
|
|
if (!learned || !sessions.current(active)) { if (state != null) state.refresh(); return; }
|
|
if (state != null && !state.active.equals(active)) { state.remove("owner_session_replaced"); state = states.get(active.player()); }
|
|
if (state == null) {
|
|
var player = sessions.currentPlayer(active).orElse(null);
|
|
if (player == null) return;
|
|
state = new State(active, player); states.put(active.player(), state);
|
|
}
|
|
state.refresh();
|
|
}
|
|
|
|
private static boolean same(PotionEffect first, PotionEffect second) {
|
|
return first != null && second != null && first.getType().equals(second.getType())
|
|
&& first.getAmplifier() == second.getAmplifier() && first.isInfinite() && second.isInfinite()
|
|
&& first.isAmbient() == second.isAmbient() && first.hasParticles() == second.hasParticles()
|
|
&& first.hasIcon() == second.hasIcon();
|
|
}
|
|
|
|
@Override public void close() {
|
|
if (closed) return;
|
|
closed = true;
|
|
if (sweepTask != null) sweepTask.cancel();
|
|
for (var state : List.copyOf(states.values())) state.remove("plugin_shutdown");
|
|
states.clear();
|
|
}
|
|
}
|