MV Accessory that nullifies 1 status effect per battle - "Cannot move" restriction

I have an accessory in my game called the "Scapegoat Token", which once per battle will "absorb" a negative status effect that would have been applied to the wearer.

The way I implemented this was whenever a character is hit by a skill that might apply a negative status, a common event checks if that character now has one of those states, if they are wearing the accessory, and if the token has not already been "broken" earlier in the battle. If all those conditions are met, that state is removed and the switch indicating that the token is "broken" for the rest of the battle is toggled.

Here's a screenshot showing the general shape of it:
Screenshot (444).png



I had previously tested and found that actors weren't affected by restrictions that were immediately removed, for example, a character that was hit by a state with the "attack an enemy" restriction that was absorbed by the token will use the correctly selected skill against the correctly selected enemy or ally.
However, more recently I found that if the token removes a state with the "cannot move" restriction before that character has taken their action for that turn, that restriction still takes effect even though it was removed. The renders the item useless for states where the effect is "cannot move for 1 turn".

So I need to find a way to get this accessory properly nullifying "cannot move" states. Is there a way to "restore" a turn that was lost in this way?

An implementation that uses the "resist" trait might work, but part of the reason this implementation had been working for me is that removing the state after it's been applied gave something tangible to check against to know when the token should "break." When different skills have different rates of applying a state that are also sometimes different than the skill's success rate, I have no idea how to check if a resisted state was "blocked" by the accessory.
a character that was hit by a state with the "attack an enemy" restriction that was absorbed by the token will use the correctly selected skill against the correctly selected enemy or ally...

if the token removes a state with the "cannot move" restriction before that character has taken their action for that turn, that restriction still takes effect even though it was removed.
when a state that has a restriction and is applied to a battler, the engine checks if the battler had a restriction prior, and if so, removes their current action. it shouldnt make a distinction between any of the restrictions in that regard. do you have a plugin that changes this?

An implementation that uses the "resist" trait might work, but part of the reason this implementation had been working for me is that removing the state after it's been applied gave something tangible to check against to know when the token should "break." When different skills have different rates of applying a state that are also sometimes different than the skill's success rate, I have no idea how to check if a resisted state was "blocked" by the accessory.
You might need a plugin for this. You'd have to be able to detect when a state is going to be added successfully, but also block it from actually being added as to not trigger onRestict() effects.

If you wouldnt mind redoing the logic of your system a bit and changing the scapegoat switch to a state that gets placed on the battler at the start of the battle, itd be straightforward to patch addState to check if the target has scapegoat, see if the incoming state has a restriction (or a notetag if its meant to be any negative state and not just restrictive ones), and if so, stop right there and remove the scapegoat state.
Turns out that was due to this snippet, which allows characters with the "only attack" restriction to use their action on that turn to attack instead of just losing the turn, which might be a decent clue?
My edit was commenting out a line that was only for MZ.

Code:
//Another Fen on the forums

Game_Battler.prototype.onRestrict = function() {
    Game_BattlerBase.prototype.onRestrict.call(this);
    if (!this.canMove()) {
    // rethy cheat - MV function
        //this.clearTpbChargeTime();
        this.clearActions();
    }
    for (const state of this.states()) {
        if (state.removeByRestriction) {
            this.removeState(state.id);
        }
    }
};

I'll have to wait to take a closer look at it or addState though. There would be states like poison which wouldn't necessarily have a restriction, but should still be blocked.
I'll have to wait to take a closer look at it or addState though. There would be states like poison which wouldn't necessarily have a restriction, but should still be blocked.
As I said, using a notetag to distinguish negative states also works if just a restriction isnt enough, but its up to you if you'd want to do it that way. If youre ok with setting up the scapegoat state as mentioned, i can give you a plugin later thatll do as i described.
As I said, using a notetag to distinguish negative states also works if just a restriction isnt enough, but its up to you if you'd want to do it that way. If youre ok with setting up the scapegoat state as mentioned, i can give you a plugin later thatll do as i described.
If you're willing to do that, that would be awesome. I might try messing with that snippet and see if adding a couple of conditionals is enough to do the trick first.
Edit: Simply commenting out this.clearActions() seems to have the intended effect. I'm not sure if there are larger consequences that I am failing to grasp by doing so.
Edit: Simply commenting out this.clearActions() seems to have the intended effect. I'm not sure if there are larger consequences that I am failing to grasp by doing so.
if it works, it works. if there is an issue down the line, try this:

Code:
(() => {
    var addState = Game_Battler.prototype.addState;
    Game_Battler.prototype.addState = function (stateId) {
    const scapegoat = 0;
    const state = $dataStates[stateId];
    if (state && state.meta.Negative && this.isStateAffected(scapegoat)){
        this.removeState(scapegoat);

        if ($gameParty.inBattle()){
            BattleManager._logWindow.addText("Scapegoat did the thing! ("+this.name()+" was protected from "+ state.name+")")
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("clear");
        }
        return;
    }
    addState.apply(this, arguments);
    };
})();
and:
- place a <Negative> notetag in any state that should be blocked
- change the "scapegoat" variable to the id of the scapegoat state
- change the protection message to what you had before
if it works, it works. if there is an issue down the line, try this:

Code:
(() => {
    var addState = Game_Battler.prototype.addState;
    Game_Battler.prototype.addState = function (stateId) {
    const scapegoat = 0;
    const state = $dataStates[stateId];
    if (state && state.meta.Negative && this.isStateAffected(scapegoat)){
        this.removeState(scapegoat);

        if ($gameParty.inBattle()){
            BattleManager._logWindow.addText("Scapegoat did the thing! ("+this.name()+" was protected from "+ state.name+")")
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("wait");
            BattleManager._logWindow.push("clear");
        }
        return;
    }
    addState.apply(this, arguments);
    };
})();
and:
- place a <Negative> notetag in any state that should be blocked
- change the "scapegoat" variable to the id of the scapegoat state
- change the protection message to what you had before
Awesome! Thank you so much.

Original thread: https://forums.rpgmakerweb.com/threads/accessory-that-nullifies-1-status-effect-per-battle-cannot-move-restriction.183794/