(Yanfly Engine) State that prevents ONE ailment, then removes itself?

Hello! I am looking for a way to create a state that, rather than making the target immune to states under the "Ailment" category, it prevents the next time that one of those states is added to the target, then the state removes itself. If anyone has played Etrian Odyssey 3, I am basically looking for a way to replicate the effect of the skill "Prevent Order" of the Sovereign class.
I am using a few Yanfly Engine plugins. I'm trying to use as few as possible, but, I do have all of them available if needed.

I already tried using the Buff&States Core Plugin to add this to each "Ailment" state:
JavaScript:
<Custom Apply Effect>
// Check if target is affected by Prevention.
if user.isStateAffected(42) {
  // Remove this state.
  user.removeState(stateId);
  // Remove Prevention.
  user.removeState(42);
}
</Custom Apply Effect>
(42 is the stateId of the "Prevention" state.)
However, that had the unfortunate consequence of removing the "Ailment" state even when it was already applied. For example:
  1. - Enemy 1 afflicts Actor 1 with the "Poison" state for 3 turns.
  2. - Actor 2 grants Actor 1 the "Prevention" state. Actor 1's "Poison" state is still active for 3 turns.
  3. - Enemy 2 tries to extend the duration of Actor 1's Poison by 3 more turns by trying to inflict the Poison state again. However, the Custom Apply Effect kicks in: Actor 1 is cured of ALL Poison (even the Poison they already had), rather than simply preventing the new infliction and still being affected by Poison for 3 turns.
Ideally, in this scenario, Actor 1 would simply just keep being affected by Poison for the original 3 turns rather than being cured completely or having the Poison's duration extended to 6 turns.

If at all possible, it would be even better if I could affix this kind of prevention effect to the Prevention state itself, just so I don't have to go to every single "Ailment" state and copypaste a bunch of code.

Also, I do have the State Categories plugin, and I did assign the "Ailment" category to the "Ailment" states, but I haven't managed to take advantage of state categories in any meaningful way yet when it comes to this effect. Thanks in advance for your time!
You could make it so that on <Custom Deselect Effect> (and <Custom Regeneration Effect> as a fallback), your state 42 looks for each ailment it should prevent and if it finds any, removes the first one it finds and then itself. itll make it so that your prevent order handles status removal all by itself, but it wont fix the problem of reapplication cleansing current active effects.

For that one i could think of a few ways but none are particularly "clean". I think the best way is to put the check for state 42 (alongside some animation to show that prevention happened) on each skill that can inflict an ailment so that the application just straight up doesnt happen, but i dont know how many skills that'd be for you.
I've done something like this before, but I don't know what language I'd search for to find it.

So you'd do something like:
- Have your state have the State Resist trait to whatever you want. We'll use ID 6.
- Give it the notetag:
Code:
<Custom Deselect Effect>
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && effect.dataId==6))
    target.removeState(stateId);
</Custom Deselect Effect>

That will check the incoming action/item to see if it has the trait to apply state 6. If yes, this state blocked it with its state resist, and it removes itself.

You can use boolean OR to check for multiple states, or you can use Yanfly's state categories, or you can add an animation on the actor, whatever...but that's the basic idea.
I've done something like this before, but I don't know what language I'd search for to find it.

So you'd do something like:
- Have your state have the State Resist trait to whatever you want. We'll use ID 6.
- Give it the notetag:
Code:
<Custom Deselect Effect>
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && effect.dataId==6))
    target.removeState(stateId);
</Custom Deselect Effect>

That will check the incoming action/item to see if it has the trait to apply state 6. If yes, this state blocked it with its state resist, and it removes itself.

You can use boolean OR to check for multiple states, or you can use Yanfly's state categories, or you can add an animation on the actor, whatever...but that's the basic idea.
I've tried this code:
JavaScript:
<Custom Deselect Effect>
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && effect.dataId==9)) {
  target.startAnimation(125);
  target.removeState(stateId);
}
</Custom Deselect Effect>
However, it doesn't seem to work properly.

The "9" in the effect.dataId is just the ID of a random Ailment state. I wanted to try only preventing one of these states to start just to make sure the code worked. The animation is there to give a proper confirmation to whether the code works properly or not.
Another thing that I changed is that I used 0% State Rates rather than State Resists, knowing that a state with a State Resist will automatically cure the battler of the resisted state if they are affected.

Onto the issues, first I tried using a skill with a 100% rate to inflict state 9. Just a very plain Certain Hit skill with nothing going on other that "It inflicts State 9." The skill just... goes right through and inflicts the state with no issues. I tried lowering the skill's infliction rate to 99% to see if it had something to do with the 100% infliction rate maybe bypassing some checks, but the skill just lands anyway.

Another thing that I was testing in the meantime is giving actors Attack State 9 with 100% rate. And what transpired confused me even more... Because the state did NOT land, but the code above didn't run either. No animation 125, and the Prevent state persisted.
The problem is you're throwing a few different things into the mix.

Another thing that I changed is that I used 0% State Rates rather than State Resists...Just a very plain Certain Hit skill
Certain Hit ignores state rate. If you want this to work with Certain Hit, you'll either need additional new code as a plugin, or use State Resist like I said above.

Another thing that I was testing in the meantime is giving actors Attack State 9 with 100% rate.
Attack State information is stored differently. You would need different code if you want this to work on attack states - I wrote this thinking you wanted to block a skill that applied a state normally.
The problem is you're throwing a few different things into the mix.


Certain Hit ignores state rate. If you want this to work with Certain Hit, you'll either need additional new code as a plugin, or use State Resist like I said above.


Attack State information is stored differently. You would need different code if you want this to work on attack states - I wrote this thinking you wanted to block a skill that applied a state normally.
I see... That explains a lot. So for example if the skill with 100% chance to inflict State 9 was a Magical Hit instead of a Certain Hit it would work?
Sadly having to accomodate for this would throw the entire rest of my project's skills database into disarray, so for now I'll think of a different effect to substitute the Prevention effect and maybe come back to it eventually.
That's not to say that I'm completely giving up on the effect though. If anyone finds out how to replicate the effect, I'd appreciate it immensely.
Yes, the whole point of Certain Hit is it bypasses all chances of failure. It can't miss, can't be dodged, can't be resisted by chance...but if the target is straight up immune, that still applies.

That's not to say that I'm completely giving up on the effect though. If anyone finds out how to replicate the effect, I'd appreciate it immensely.
...I already told you how to do it. So what exactly are you asking for? It's not like it's a grand mystery, all the pertinent code is in your js folder 😛

You just need to explain specifically what more you want to accomplish.
I figured out a workaround to make Ailment-inflicting skills Magical Hits while keeping them "Status" skills for other purposes in my project, AND give the Prevention state State Rates rather than State Resists so that the Prevention state doesn't also act like a cure-all. Still doesn't feel right to have ailment-inflicting skills count as physical or magical, but hey. Ya gotta do what ya gotta do.
Still doesn't work on Attack States, but for now I can live with that. Not a lot of things in my project really use Attack States to inflict Ailments, so it shouldn't be that big a deal. I can figure something out.

Now, the only thing that's left to "clean up" is thiiiiis part of the code
<Custom Deselect Effect>
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && effect.dataId==9)) {
target.startAnimation(125);
target.removeState(stateId);
}
</Custom Deselect Effect>
If at all possible, rather than listing all the state IDs I want the state to prevent, which would something like this... abomination*...
... effect.dataId==4||5||6||7||8||9||10||11||12 ...
*= at least as far as my limited js knowledge takes me...
I'd like to know if there's a "cleaner" way to say, for example "if the effect belongs to the 'Ailment' state category".
I know that the States Categories plugin has a function that looks like this and checks if the battler is affected by a state that belongs to a certain category
battler.isStateCategoryAffected('text')
But I don't know if something like "effect.isStateCategory('text')" would work in place of "effect.dataId==Insert-lots-of-IDs-and-OR-statements-here". Back when I was testing things on my own, the "isStateCategory" function never worked for me, but I might have missed something.

Thanks for your help and patience, by the way. I don't wanna make it sound like I'm not grateful! 🙏
*= at least as far as my limited js knowledge takes me...
Yeah, that's not valid JavaScript syntax 😉

The functions for state categories provided in the instructions - such as isStateCategory() - only work on battlers that already have states, which is exactly how they're written in the documentation.

You can check whether a state would have a category thusly:
Code:
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && $dataStates[effect.dataId].category.contains("NEGATIVE")))

So that will block any incoming state that has the negative category. Note that it must be in all upper case.
JavaScript:
<Custom Deselect Effect>
if (this.item().effects.some(effect => effect.code==Game_Action.EFFECT_ADD_STATE && $dataStates[effect.dataId].category.contains("AILMENT"))) {
  target.startAnimation(125);
  target.removeState(stateId);
}
</Custom Deselect Effect>
It's working perfectly now! This is exactly the effect I was looking for, save for it not covering Attack States, but it's good enough for me. Thank you so much!

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-engine-state-that-prevents-one-ailment-then-removes-itself.160673/