MV (Yanfly Engine) SMT Buff System using States

Hello! I'm sure this kind of thing has been asked before, but I couldn't find anything.
I want to make a buff system that works by using states rather than parameters. This is because, rather than modifying the parameters, I want to directly affect damage dealt/taken and hit+avoid, same as the buff system in recent Shin Megami Tensei titles.
For those unfamiliar, it's a pretty basic buff system: the buffs/debuffs stack up to two times (so the stages are -2, -1, 0, +1, and +2), and buffs/debuffs stack on eachother (If I gain +1 Attack while having -1 Attack, both cancel eachother out, while if I gain +2 Attack while having -1 Attack, I instead gain +1 Attack. etc...). Buffs/debuffs last 3 turns, and when a buff is updated, the duration of the new stage is reset to 3.

I remember doing this once already using Yanfly's Buff & States Core (as well as States Categories but that's not relevant right now) but then changing my mind about 2 years ago when I last opened up the editor (I know, I know...), but now that it's been a while, I can't for the life of me remember how I did it...

Looking back at my older project, this is what I found in the notetags of my Attack Up State:
<Help Description>
: Unit deals 20% more damage with attacks.
</Help Description>

<Category: Buff>
<Reapply Add Turns>

<Custom Apply Effect>
// Play the animation.
user.startAnimation(123);
//Check if unit has Attack Down.
if (target.isStateAffected(23)) {
//Remove Attack Down.
target.removeState(23)
//Remove Attack Up.
target.removeState(20)
}
</Custom Apply Effect>

<Custom Confirm Effect>
// Check if the action deals HP damage.
if (this.isHpEffect() && value > 0) {
// Increase the damage by a multiplier.
value *= 1.2;
// Round the damage.
value = Math.round(value);
}
</Custom Confirm Effect>
Note: States 20-25 are, respectively, "Attack Up", "Defense Up", "Agility Up", "Attack Down", "Defense Down", "Agility Down". You may notice there is no "second stage" to the buffs/debuffs, which is because in that project I changed my mind and made buffs/debuffs only stack once.

I tried using the Custom Apply Effect to write logic for other stages, checking if the Attack Up x1, Attack Down x1, and Attack Down x2 stages were on the user and then adding/removing states accordingly, but I came to the sudden realization that the Custom Apply Effect takes place right after the state is already applied. So for example, if I were to add Attack Up x1 to a battler who has no buffs, it would add Attack Up x1, then notice that the battler indeed has Attack Up x1 already and replace it with Attack Up x2. I'm also having some other issues that no doubt stem from my rustiness and incompetence, but one thing at a time...
Custom Apply Effect takes place right after the state is already applied. So for example, if I were to add Attack Up x1 to a battler who has no buffs, it would add Attack Up x1, then notice that the battler indeed has Attack Up x1 already and replace it with Attack Up x2. I'm also having some other issues that no doubt stem from my rustiness and incompetence, but one thing at a time...
you can use properties and manage the state based on that. so instead of the apply effect adding the next buff in the chain if they have the current, you set a something like target._sukukaja = (target._sukukaja + 1) || 1; and have the sukukaja buff sources refer to that property to determine which version to use, or have the state on apply use it to determine if it should give another, etc.

although id really suggest scrapping the states for each level and using something like Quasi's params plus plugin to handle it all within one state for each kind of buff/debuff. then you can still have the apply effect manipulate the property, but the strength of the effect can scale with the property (i.e. HIT +10% per _sukukaja, DEF -10% per _rakunda, etc.)
you can use properties and manage the state based on that. so instead of the apply effect adding the next buff in the chain if they have the current, you set a something like target._sukukaja = (target._sukukaja + 1) || 1; and have the sukukaja buff sources refer to that property to determine which version to use, or have the state on apply use it to determine if it should give another, etc.

although id really suggest scrapping the states for each level and using something like Quasi's params plus plugin to handle it all within one state for each kind of buff/debuff. then you can still have the apply effect manipulate the property, but the strength of the effect can scale with the property (i.e. HIT +10% per _sukukaja, DEF -10% per _rakunda, etc.)

I ended up figuring it out on my own! I had to do some finagling with temporary variables, but I got something that works. I don't think that's what I originally did 2 years ago, but as long as it works...! I'll share the notetags below in case someone else find this post:

Attack Up x1 State:
JavaScript:
<Help Description>
: User's attacks deal 20% more damage.
</Help Description>

<Category: Buff>
<Category: Attack Buff>
<Reapply Reset Turns>

<Custom Apply Effect>
// Play the animation.
user.startAnimation(123);
// If Attack Up counter is undefined, set it to 0.
if (user._AtkUp == null) {
  user._AtkUp = 0;
}
// Increase Attack Up counter by 1.
user._AtkUp += 1;
// Check if user has Attack Up x1.
if (user._AtkUp > 1) {
  // Set Attack Up counter to undefined.
  user._AtkUp = null;
  //  Remove Attack Up x1.
  target.removeState(13)
  //  Add Attack Up x2.
  target.addState(14)
// Check if user has Attack Down x1.
} else if (target.isStateAffected(15)) {
  // Set Attack Up counter to undefined.
  user._AtkUp = null;
  //  Remove Attack Up x1.
  target.removeState(13)
  //  Remove Attack Down x1.
  target.removeState(15)
// Check if user has Attack Down x2.
} else if (target.isStateAffected(16)) {
  // Set Attack Up counter to undefined.
  user._AtkUp = null;
  //  Remove Attack Up x1.
  target.removeState(13)
  //Remove Attack Down x2.
  target.removeState(16)
  //  Add Attack Down x1.
  target.addState(15)
}
</Custom Apply Effect>

<Custom Remove Effect>
// Set Attack Up counter to undefined.
user._AtkUp = null;
</Custom Remove Effect>

<Custom Leave Effect>
// Set Attack Up counter to undefined.
user._AtkUp = null;
</Custom Leave Effect>

<Custom Confirm Effect>
// Check if the action deals HP damage.
if (this.isHpEffect() && value > 0) {
  // Increase the damage by a multiplier.
  value *= 1.20;
  // Round up the damage.
  value = Math.ceil(value);
}
</Custom Confirm Effect>

Attack Up x2 State:
JavaScript:
<Help Description>
: User's attacks deal 40% more damage.
</Help Description>

<Category: Buff>
<Category: Attack Buff>
<Reapply Reset Turns>

<Custom Apply Effect>
// Play the animation.
user.startAnimation(123);
// Check if user has Attack Up x1.
if (user.isStateAffected(13)) {
  //  Remove Attack Up x1.
  target.removeState(13)
// Check if user has Attack Down x1.
} else if (target.isStateAffected(15)) {
  //  Remove Attack Up x2.
  target.removeState(14)
  //  Remove Attack Down x1.
  target.removeState(15)
  //  Add Attack up x1.
  target.addState(13)
// Check if user has Attack Down x2.
} else if (target.isStateAffected(16)) {
  //  Remove Attack Up x2.
  target.removeState(14)
  //Remove Attack Down x2.
  target.removeState(16)
}
</Custom Apply Effect>

<Custom Confirm Effect>
// Check if the action deals HP damage.
if (this.isHpEffect() && value > 0) {
  // Increase the damage by a multiplier.
  value *= 1.40;
  // Round up the damage.
  value = Math.ceil(value);
}
</Custom Confirm Effect>

Attack Down x1 State:
JavaScript:
<Help Description>
: User's attacks deal 15% less damage.
</Help Description>

<Category: Debuff>
<Category: Attack Debuff>
<Reapply Reset Turns>

<Custom Apply Effect>
// Play the animation.
user.startAnimation(124);
// If Attack Down counter is undefined, set it to 0.
if (user._AtkDown == null) {
  user._AtkDown = 0;
}
// Increase Attack Down counter by 1.
user._AtkDown += 1;
// Check if user has Attack Up x1.
if (user.isStateAffected(13)) {
  // Set Attack Down counter to undefined.
  user._AtkDown = null;
  //  Remove Attack Up x1.
  target.removeState(13)
  //  Remove Attack Down x1.
  target.removeState(15)
// Check if user has Attack Up x2.
} else if (target.isStateAffected(14)) {
  // Set Attack Down counter to undefined.
  user._AtkDown = null;
  //  Remove Attack Up x2.
  target.removeState(14)
  //  Remove Attack Down x1.
  target.removeState(15)
  //  Add Attack Up x1.
  target.addState(13)
// Check if user has Attack Down x1.
} else if (user._AtkDown > 1) {
  // Set Attack Down counter to undefined.
  user._AtkDown = null;
  //  Remove Attack Down x1.
  target.removeState(15)
  //  Add Attack Down x2.
  target.addState(16)
}
</Custom Apply Effect>

<Custom Remove Effect>
// Set Attack Down counter to undefined.
user._AtkDown = null;
</Custom Remove Effect>

<Custom Leave Effect>
// Set Attack Down counter to undefined.
user._AtkDown = null;
</Custom Leave Effect>

<Custom Confirm Effect>
// Check if the action deals HP damage.
if (this.isHpEffect() && value > 0) {
  // Decrease the damage by a multiplier.
  value *= 0.85;
  // Round up the damage.
  value = Math.ceil(value);
}
</Custom Confirm Effect>

Attack Down x2 State:
JavaScript:
<Help Description>
: User's attacks deal 30% less damage.
</Help Description>

<Category: Debuff>
<Category: Attack Debuff>
<Reapply Reset Turns>

<Custom Apply Effect>
// Play the animation.
user.startAnimation(124);
// Check if user has Attack Up x1.
if (user.isStateAffected(13)) {
  //  Remove Attack Up x1.
  target.removeState(13)
  //  Remove Attack Down x2.
  target.removeState(16)
  //  Add Attack Down x1.
  target.addState(15)
// Check if user has Attack Up x2.
} else if (target.isStateAffected(14)) {
  //  Remove Attack Up x2.
  target.removeState(14)
  //  Remove Attack Down x2.
  target.removeState(16)
// Check if user has Attack Down x1.
} else if (user._AtkDown > 1) {
  //Remove Attack Down x1.
  target.removeState(15)
}
</Custom Apply Effect>

<Custom Confirm Effect>
// Check if the action deals HP damage.
if (this.isHpEffect() && value > 0) {
  // Decrease the damage by a multiplier.
  value *= 0.70;
  // Round up the damage.
  value = Math.ceil(value);
}
</Custom Confirm Effect>

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-engine-smt-buff-system-using-states.179886/