fix(passives): retain exact native cleanup ownership
This commit is contained in:
1 parent
3a184d5298
commit
0acf90909b
6 files changed
+768
-32
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package games.dmg.fantasystick.passive;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import net.minecraft.world.effect.MobEffectInstance;
|
||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
/** Read-only Purpur 2618 phase/instance proof. Never changes native maps, events or mutation flags. */
|
||||
final class NativePotionState {
|
||||
private static final Field TICKING = tickingField();
|
||||
private static final StackWalker STACK = StackWalker.getInstance();
|
||||
private NativePotionState() { }
|
||||
|
||||
static void prepare() {
|
||||
if (TICKING.getType() != boolean.class) throw new IllegalStateException("Unsupported native potion phase");
|
||||
}
|
||||
|
||||
/** Reentrant event/lifecycle calls must not consume an in-flight native decision or mutate by type. */
|
||||
static boolean dispatching() {
|
||||
return STACK.walk(frames -> {
|
||||
var iterator = frames.iterator();
|
||||
for (int depth = 0; depth < 256 && iterator.hasNext(); depth++) {
|
||||
var frame = iterator.next();
|
||||
if (frame.getClassName().equals("org.bukkit.plugin.RegisteredListener")
|
||||
&& frame.getMethodName().equals("callEvent")) return true;
|
||||
if (frame.getClassName().equals("net.minecraft.world.entity.LivingEntity")
|
||||
&& frame.getMethodName().contains("Effect")) return true;
|
||||
}
|
||||
return iterator.hasNext(); // Unknown deeper context cannot authorize a mutation.
|
||||
});
|
||||
}
|
||||
|
||||
static boolean quiet(Player player) {
|
||||
try { return !TICKING.getBoolean(handle(player)); }
|
||||
catch (IllegalAccessException failure) { throw new IllegalStateException("Native potion phase unavailable", failure); }
|
||||
}
|
||||
|
||||
static MobEffectInstance identity(Player player, PotionEffectType type) {
|
||||
return handle(player).getEffect(CraftPotionEffectType.bukkitToMinecraftHolder(type));
|
||||
}
|
||||
|
||||
private static net.minecraft.server.level.ServerPlayer handle(Player player) {
|
||||
if (!(player instanceof CraftPlayer craft)) throw new IllegalStateException("Unsupported native potion holder");
|
||||
return java.util.Objects.requireNonNull(craft.getHandle(), "Native potion holder unavailable");
|
||||
}
|
||||
|
||||
private static Field tickingField() {
|
||||
try {
|
||||
var field = net.minecraft.world.entity.LivingEntity.class.getDeclaredField("tickingEffects");
|
||||
field.setAccessible(true); return field;
|
||||
} catch (ReflectiveOperationException failure) {
|
||||
throw new IllegalStateException("Unsupported native potion internals; no unsafe fallback", failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import net.minecraft.world.effect.MobEffectInstance;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
@@ -18,31 +19,47 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
final EquipmentSessions.Active active;
|
||||
final Player player;
|
||||
PotionEffect owned;
|
||||
MobEffectInstance nativeOwned;
|
||||
PotionEffect displaced;
|
||||
long displacedUntil;
|
||||
long refreshedAt;
|
||||
long applications;
|
||||
PassivePotionEventBridge.Application application;
|
||||
int mutating;
|
||||
State(EquipmentSessions.Active active, Player player) { this.active = active; this.player = player; }
|
||||
|
||||
boolean quiet() {
|
||||
if (mutating != 0) return false;
|
||||
try { return NativePotionState.quiet(player); }
|
||||
catch (RuntimeException unavailable) { return false; }
|
||||
}
|
||||
boolean owns(PotionEffect current, MobEffectInstance identity) {
|
||||
return nativeOwned != null && nativeOwned == identity && same(current, owned);
|
||||
}
|
||||
void refresh() {
|
||||
if (!quiet()) return;
|
||||
refreshedAt = sweepGeneration;
|
||||
if (displaced != null && !displaced.isInfinite() && sweepGeneration >= displacedUntil) displaced = null;
|
||||
PotionEffect current;
|
||||
try { current = player.getPotionEffect(type); }
|
||||
PotionEffect current; MobEffectInstance identity;
|
||||
try { current = player.getPotionEffect(type); identity = NativePotionState.identity(player, type); }
|
||||
catch (RuntimeException unavailable) { return; }
|
||||
if (owned != null && !same(current, owned)) {
|
||||
if (owned != null && nativeOwned == null && current != null) return; // Unconfirmed application, not removal authority.
|
||||
if (owned != null && !owns(current, identity)) {
|
||||
endAudit("native_layer_replaced_or_removed", current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
||||
owned = null; displaced = null;
|
||||
owned = null; nativeOwned = null; displaced = null;
|
||||
}
|
||||
if (!eligible()) { remove(endReason()); return; }
|
||||
if (current != null && (!replaceWeaker || current.getAmplifier() >= amplifier)) return;
|
||||
if (!quiet()) return;
|
||||
var effect = new PotionEffect(type, PotionEffect.INFINITE_DURATION, amplifier, false, false, true);
|
||||
mutating++;
|
||||
try {
|
||||
if (player.addPotionEffect(effect, true)) {
|
||||
owned = effect; displaced = current;
|
||||
displacedUntil = current == null ? 0 : current.isInfinite()
|
||||
? Long.MAX_VALUE : sweepGeneration + current.getDuration();
|
||||
nativeOwned = NativePotionState.identity(player, type);
|
||||
if (nativeOwned == null) return; // Preserve responsibility without inventing a native acknowledgment.
|
||||
if (events != null) {
|
||||
try {
|
||||
var record = sessions.currentRecord(active).orElse(null);
|
||||
@@ -52,7 +69,11 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
}
|
||||
applications++;
|
||||
}
|
||||
} catch (RuntimeException unavailable) { owned = null; displaced = null; }
|
||||
} catch (RuntimeException unavailable) { /* Preserve any captured native ownership and displaced evidence. */ }
|
||||
finally {
|
||||
mutating--;
|
||||
if (closed) finishClose();
|
||||
}
|
||||
}
|
||||
|
||||
boolean eligible() {
|
||||
@@ -76,29 +97,36 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
application = null;
|
||||
}
|
||||
void remove(String reason) {
|
||||
if (!quiet()) return;
|
||||
if (owned != null) {
|
||||
PotionEffect current;
|
||||
try { current = player.getPotionEffect(type); }
|
||||
PotionEffect current; MobEffectInstance identity;
|
||||
try { current = player.getPotionEffect(type); identity = NativePotionState.identity(player, type); }
|
||||
catch (RuntimeException unavailable) { return; }
|
||||
if (same(current, owned)) {
|
||||
if (nativeOwned == null && current != null) return;
|
||||
if (owns(current, identity)) {
|
||||
if (!quiet()) return;
|
||||
mutating++;
|
||||
try {
|
||||
player.removePotionEffect(type);
|
||||
current = player.getPotionEffect(type);
|
||||
identity = NativePotionState.identity(player, type);
|
||||
// The original instance, not an event flag or an equal Bukkit snapshot, is the lease.
|
||||
if (identity == nativeOwned) return;
|
||||
boolean restored = false;
|
||||
if (displaced != null && (displaced.isInfinite() || sweepGeneration < displacedUntil)) {
|
||||
if (current == null && identity == null && displaced != null
|
||||
&& (displaced.isInfinite() || sweepGeneration < displacedUntil)) {
|
||||
if (!NativePotionState.quiet(player)) return;
|
||||
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. */ }
|
||||
}
|
||||
if (restored) current = player.getPotionEffect(type);
|
||||
endAudit(reason, current != null && !restored ? "different_native_layer_preserved" : "owned_layer_removed", current, restored);
|
||||
} catch (RuntimeException unavailable) { return; }
|
||||
finally { mutating--; }
|
||||
} else endAudit(reason, current == null ? "native_layer_absent" : "different_native_layer_preserved", current, false);
|
||||
owned = null; displaced = null;
|
||||
owned = null; nativeOwned = null; displaced = null;
|
||||
}
|
||||
states.remove(active.player(), this);
|
||||
}
|
||||
@@ -114,7 +142,10 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
private int stateCursor;
|
||||
private long sweepGeneration;
|
||||
private org.bukkit.scheduler.BukkitTask sweepTask;
|
||||
private org.bukkit.plugin.Plugin plugin;
|
||||
private boolean closed;
|
||||
private boolean closing;
|
||||
private boolean closeScheduled;
|
||||
private PassivePotionEventBridge events;
|
||||
|
||||
void attachEvents(PassivePotionEventBridge bridge) {
|
||||
@@ -131,12 +162,15 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
|
||||
void install(org.bukkit.plugin.Plugin plugin) {
|
||||
Objects.requireNonNull(plugin);
|
||||
if (closed || sweepTask != null) throw new IllegalStateException("Passive potion sweep already installed or closed");
|
||||
if (closed || sweepTask != null || this.plugin != null) throw new IllegalStateException("Passive potion sweep already installed or closed");
|
||||
NativePotionState.prepare();
|
||||
// Native identity proof needs no event listener, including during partial installation failure.
|
||||
sweepTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this::sweep, 1, 1);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
void sweep() {
|
||||
if (closed) return;
|
||||
if (closed || NativePotionState.dispatching()) return;
|
||||
sweepGeneration++;
|
||||
var active = sessions.activeCharacters();
|
||||
int activeChecks = Math.min(MAX_ACTIVE_CHECKS, active.size());
|
||||
@@ -176,11 +210,33 @@ final class PersistentPotionEffect implements AutoCloseable {
|
||||
&& first.hasIcon() == second.hasIcon();
|
||||
}
|
||||
|
||||
private void finishClose() {
|
||||
if (!closed || closing) return;
|
||||
if (NativePotionState.dispatching() || states.values().stream().anyMatch(state -> !state.quiet())) {
|
||||
if (!closeScheduled && plugin != null && plugin.isEnabled()) {
|
||||
closeScheduled = true;
|
||||
try { plugin.getServer().getScheduler().runTask(plugin, () -> {
|
||||
// One bounded post-dispatch attempt, never a self-rescheduling shutdown loop.
|
||||
if (NativePotionState.dispatching()) return;
|
||||
sweepGeneration++;
|
||||
closing = true;
|
||||
try { for (var state : List.copyOf(states.values())) state.remove("plugin_shutdown"); }
|
||||
finally { closing = false; }
|
||||
}); }
|
||||
catch (RuntimeException unavailable) { /* Retain unresolved ownership; no false cleanup acknowledgment. */ }
|
||||
}
|
||||
return;
|
||||
}
|
||||
closing = true;
|
||||
try { for (var state : List.copyOf(states.values())) state.remove("plugin_shutdown"); }
|
||||
finally { closing = false; }
|
||||
// Unconfirmed leases remain explicit in this owner. There is no cross-restart removal authority.
|
||||
}
|
||||
|
||||
@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();
|
||||
finishClose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user