(SOLVED) Battlers lose a turn when "restriction" states are applied

I've noticed that applying a state in battle to an enemy or actor that has any kind of restriction (confusion, charm, sleep, stun, etc) makes them take no action, no matter what the restriction is, if they haven't acted on that turn.

So for instance, I apply a Rage state on an enemy that hasn't acted on that turn (I've tried in different VisuMZ battle systems and a new project with no plugins), they don't act as if they were stunned, and THEN, for the rest of the duration of the state, they act normally.
This is very annoying, since that's what Stun is for.

Is there any way to bypass this?

Thanks!
Oh, good find.

You could try to add this modified function as a plugin (should be placed high up in the plugin list to reduce the chance for incompatibilities):
JavaScript:
Game_Battler.prototype.onRestrict = function() {
    Game_BattlerBase.prototype.onRestrict.call(this);
    if (!this.canMove()) {
        this.clearTpbChargeTime();
        this.clearActions();
    }
    for (const state of this.states()) {
        if (state.removeByRestriction) {
            this.removeState(state.id);
        }
    }
};
Or, if @Another Fen's code doesn't work with the plugins you're using, you could try something like:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Don't reset actions when restriction is applied.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/posts/1538833/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Non-conflicting identifier for new "don't reset" flag on `Game_Battler`. */
  const P = Symbol();

  // Patch - flag battler if they can still move under restriction.
  void (alias => {
    Game_Battler.prototype.onRestrict = function() {
      if (this.canMove())
        this[P] = true;
      alias.apply(this, arguments);
      delete this[P];
    };
  })(Game_Battler.prototype.onRestrict);

  // Patch - clear charge time only if not flagged.
  void (alias => {
    Game_Battler.prototype.clearTpbChargeTime = function() {
      if (!(P in this))
        alias.apply(this, arguments);
    };
  })(Game_Battler.prototype.clearTpbChargeTime);

  // Patch - clear action list only if not flagged.
  void (alias => {
    Game_Battler.prototype.clearActions = function() {
      if (!(P in this))
        alias.apply(this, arguments);
    };
  })(Game_Battler.prototype.clearActions);

})();
Thanks for the fast replies!
I just tested both real quick, they seem to work perfectly!
Thanks so much!
For the sake of completion, you might consider something like this:

This only applies if you are using TPB mode and are confusing a battler while they are casting, but is a kind of blunt way to cancel their casting (there are probably better ways to do this, it is the first solution I could think of).
JavaScript:
Game_Battler.prototype.onRestrict = function() {
    Game_BattlerBase.prototype.onRestrict.call(this);
    if (!this.canMove()) {
        this.clearTpbChargeTime();
        this.clearActions();
    } else if (BattleManager.isTpb()) {
        this._actions.filter(action => !action._forcing).forEach(action => action.clear());
    }
    for (const state of this.states()) {
        if (state.removeByRestriction) {
            this.removeState(state.id);
        }
    }
};

Late Edit: If you are using caethyrils solution because of issues, you can add the cast-canceling addition like this:
JavaScript:
Game_Battler.prototype.onRestrict = (alias => function() {
    alias.call(this);
    if (BattleManager.isTpb()) {
        this._actions.filter(action => !action._forcing).forEach(action => action.clear());
    }
})(Game_Battler.prototype.onRestrict);
For the sake of completion, you might consider something like this:

This only applies if you are using TPB mode and are confusing a battler while they are casting, but is a kind of blunt way to cancel their casting (there are probably better ways to do this, it is the first solution I could think of).
JavaScript:
Game_Battler.prototype.onRestrict = function() {
    Game_BattlerBase.prototype.onRestrict.call(this);
    if (!this.canMove()) {
        this.clearTpbChargeTime();
        this.clearActions();
    } else if (BattleManager.isTpb()) {
        this._actions.filter(action => !action._forcing).forEach(action => action.clear());
    }
    for (const state of this.states()) {
        if (state.removeByRestriction) {
            this.removeState(state.id);
        }
    }
};

Thanks! For now I have a plugin option toggled on where enemies or auto-battle actors decide their action on the spot, so no need to interrupt their casting, but bookmarking it for other projects or an eventual change in battle systems.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-battlers-lose-a-turn-when-restriction-states-are-applied.181262/