MV Actually prevent scope 1 dead ally to be used in battle

...I may have gone into a rabbit hole trying to do something. TL;DR at the end.

> I was looking into making a skill that removes the state "sleep" by slapping an ally (taking some HP in the process).
> I found out you can't, by default, select "any ally except the user" in the scope
> Looked for a plugin. Didn't find a free one
> Took a free one that does something similar but not the same, tried to look into the code
> This is so hard, augh. In the process of searching some keywords, I found a free Japanese plugin that does what I wanted
> Finally!!! I used it
> It works but... why can I try to revive an ally that isn't dead? This must be a plugin conflict breaking something...
> Tried a fresh project. It's default behaviour.
> ................What

I tested some more. A skill OR item that targets ally (dead) CANNOT be used in the pause menu (you get the "error" sound). However, you CAN use it in battle (it just won't do anything). The message where it says it's being used will display, though! And it will get consumed! I don't want that!! What!!!

So naturally, this new plugin that I thought would save my life lets me select the user. It just will nullify the effects, the same way default... revives... do... apparently.

TL;DR: could someone look into making it so that skills and items outside of the scope (item/skill for dead allies used on alive allies), instead of proceeding like normal and later failing, will just play a buzzer and not let you continue? (If such a plugin already exists, please tell me!)

I may understand the reason, though... for "all allies" it is ideal that a group revive will just work even if your whole party isn't dead (duh). But for my project I don't even think I'll need such a skill or item, so I'm fine if this option is locked for me in the process.
Pretty sure I remember either doing this or finding a plugin that does it. I'll look through my project and see if I can find it.
A skill OR item that targets ally (dead) CANNOT be used in the pause menu (you get the "error" sound).
If you literally CANNOT use it even on dead allies, that's not default behaviour.
However, you CAN use it in battle (it just won't do anything).
Yes: by default alive/dead scope restrictions (part of testApply) are ignored during the input phase of battle. E.g. if an ally isn't dead in the input phase, but you think they will be before you can cast "Heal", you might choose to cast "Revive" instead.

In case Solar_Flare can't find the plugin they mentioned, something like this might help (untested):
Spoiler: plugin code v2
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc v2 - test-apply battle actions during the input phase.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/posts/1552638/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (function() {
"use strict";

  // v1: patch - disable skills/items with no remaining targets after filtering.
  for (const C of [Window_BattleSkill, Window_BattleItem])
    void (function(alias) {
      C.prototype.isEnabled = function(item) {
        if (alias.apply(this, arguments)) {
          // Design: cf `Scene_ItemBase.prototype.isItemEffectsValid`.
          const action = new Game_Action(this._actor);
          action.setItemObject(item);
          for (const target of action.itemTargetCandidates())
            if (action.testApply(target))
              return true;
        }
        return false;
      };
    })(C.prototype.isEnabled);

  // v2: patch - disable selection of targets that are invalid after `testApply`.
  void (function(alias) {
    Window_BattleActor.prototype.isEnabled = function(item) {
      if (alias.apply(this, arguments)) {
        const action = new Game_Action(BattleManager.actor());
        const target = $gameParty.battleMembers()[index];
        action.setItemObject(item);
        return action.testApply(target);
      }
      return false;
    };
  })(Window_BattleActor.prototype.isEnabled);
  void (function(alias) {
    Window_BattleEnemy.prototype.isEnabled = function(item) {
      if (alias.apply(this, arguments)) {
        const action = new Game_Action(BattleManager.actor());
        const target = this._enemies[this.index()];
        action.setItemObject(item);
        return action.testApply(target);
      }
      return false;
    };
  })(Window_BattleEnemy.prototype.isEnabled);  

})();
Edit: wait, no, that wouldn't limit target selection, it only disables an item if there are no valid targets. 😴 I've updated the code above to version 2: now it has stuff for the actor/enemy selection windows as well (still untested).
If you literally CANNOT use it even on dead allies, that's not default behaviour.
Sorry, I meant that you can't use it on alive allies (as intended). Which means, the pause menu works the way I'd like to, but not the battle menu! I can see how my wording could have been better
In case Solar_Flare can't find the plugin they mentioned, something like this might help (untested):
Spoiler: plugin code v2
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc v2 - test-apply battle actions during the input phase.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/posts/1552638/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (function() {
"use strict";

  // v1: patch - disable skills/items with no remaining targets after filtering.
  for (const C of [Window_BattleSkill, Window_BattleItem])
    void (function(alias) {
      C.prototype.isEnabled = function(item) {
        if (alias.apply(this, arguments)) {
          // Design: cf `Scene_ItemBase.prototype.isItemEffectsValid`.
          const action = new Game_Action(this._actor);
          action.setItemObject(item);
          for (const target of action.itemTargetCandidates())
            if (action.testApply(target))
              return true;
        }
        return false;
      };
    })(C.prototype.isEnabled);

  // v2: patch - disable selection of targets that are invalid after `testApply`.
  void (function(alias) {
    Window_BattleActor.prototype.isEnabled = function(item) {
      if (alias.apply(this, arguments)) {
        const action = new Game_Action(BattleManager.actor());
        const target = $gameParty.battleMembers()[index];
        action.setItemObject(item);
        return action.testApply(target);
      }
      return false;
    };
  })(Window_BattleActor.prototype.isEnabled);
  void (function(alias) {
    Window_BattleEnemy.prototype.isEnabled = function(item) {
      if (alias.apply(this, arguments)) {
        const action = new Game_Action(BattleManager.actor());
        const target = this._enemies[this.index()];
        action.setItemObject(item);
        return action.testApply(target);
      }
      return false;
    };
  })(Window_BattleEnemy.prototype.isEnabled);

})();
Edit: wait, no, that wouldn't limit target selection, it only disables an item if there are no valid targets. 😴 I've updated the code above to version 2: now it has stuff for the actor/enemy selection windows as well (still untested).
I was trying v1 and was gonna say, thank you but it crashes! XD

I tried v2 and it does not crash, however... what it does is preventing revives to be consumed if they are used on alive allies (good!), but it doesn't really prevent you from wasting your turn using it. I'd like it to work like the pause menu, that goes "nope", gives me the error sound and doesn't continue the action.

Not sure if this info is helpful, but I know it must be possible (perhaps with some Yanfly plugin I don't want, because it adds more things I don't want to use, lol) because In Stars and Time (MV) has revives working this way I want, and so does Super Lesbian Animal RPG (VX Ace). I will keep looking into things as best as I can, in the meantime x'D Will update if I find a solution myself.
I did find the plugin – it's part of my Skill Tweaks plugin. I don't think I released that anywhere yet.

This is the segment that handles it:

JavaScript:
		Window_BattleActor.prototype.isEnabled = function(index) {
			let actor = $gameParty.battleMembers()[index];
			let action = BattleManager.inputtingAction();
			return actor && action && (action.isForDeadFriend() ? actor.isDead() : actor.isAlive());
		};
		
		Window_BattleActor.prototype.isCurrentItemEnabled = function() {
			return this.isEnabled(this.index()) && Window_Selectable.prototype.isCurrentItemEnabled.call(this);
		};
		
		const old_drawActorItem = Window_BattleActor.prototype.drawItem;
		Window_BattleActor.prototype.drawItem = function(index) {
			this.changePaintOpacity(this.isEnabled(index));
			old_drawActorItem.call(this, index);
			this.changePaintOpacity();
		};

It doesn't do a full testApply, so it won't prevent you (for example) using a healing potion on someone at full health; but it should, at least, prevent you from using a resurrect item on someone alive.
I did find the plugin – it's part of my Skill Tweaks plugin. I don't think I released that anywhere yet.

This is the segment that handles it:

JavaScript:
        Window_BattleActor.prototype.isEnabled = function(index) {
            let actor = $gameParty.battleMembers()[index];
            let action = BattleManager.inputtingAction();
            return actor && action && (action.isForDeadFriend() ? actor.isDead() : actor.isAlive());
        };
       
        Window_BattleActor.prototype.isCurrentItemEnabled = function() {
            return this.isEnabled(this.index()) && Window_Selectable.prototype.isCurrentItemEnabled.call(this);
        };
       
        const old_drawActorItem = Window_BattleActor.prototype.drawItem;
        Window_BattleActor.prototype.drawItem = function(index) {
            this.changePaintOpacity(this.isEnabled(index));
            old_drawActorItem.call(this, index);
            this.changePaintOpacity();
        };

It doesn't do a full testApply, so it won't prevent you (for example) using a healing potion on someone at full health; but it should, at least, prevent you from using a resurrect item on someone alive.
Funnily, by default the pause menu DOES prevent you from wasting a potion if the target is at full health XD So clearly the engine knows how to check (?).

Your plugin works! Thank you very much. I will be using it, and also one from your MV plugins post :9 (the passive states! Very useful)

It does prevent you from trying to use MP healing items if the target is at full MP... but not the HP counterparts, which is a little silly. Buuut I think I can live with this. (Or I could tinker with it later. But I'm just glad it does the revive thing already!)

I now have a different problem that I may have to find a workaround for x'D.
The plugin I used for "let the actor select an ally but not himself" (this one) won't behave like your plugin either, it will behave like how revives worked by default (= it will let you use the skill, but it won't have any effect. I would like if you couldn't select it at all.)

I may be asking for too much, but if someone wants to look into that, I'd be very grateful. I will also try to look at the code if no one does, I'm just not sure I'll succeed at it x'). To summarize this plugin, the only feature I'd use is putting

<SERemoveUser>

In the skill notetag, and select scope 1 ally. Whatever other feature that may be affected I don't mind much.
I'm not sure I understand. Are you saying that if my plugin and Triacontane's plugin are both enabled, mine is ineffective?
I'm not sure I understand. Are you saying that if my plugin and Triacontane's plugin are both enabled, mine is ineffective?
No, I mean, your plugin is effective in my problem with using items meant for dead allies with alive allies.
I hoped that, after turning on your plugin, Triacontane's plugin would work that same way when using its feature to limit scope so that it wouldn't let you select the user (only allies) but it wasn't that easy, haha.
Ah, you're saying you wanted the user to be disabled too.

It's definitely possible to modify my plugin to also do that if the user is supposed to be excluded. Maybe something like this?

JavaScript:
		Window_BattleActor.prototype.isEnabled = function(index) {
			let actor = $gameParty.battleMembers()[index];
			let action = BattleManager.inputtingAction();
			if(action.item() && action.item().meta.SERemoveUser && action.subject() == actor) return false; // ADD THIS LINE
			return actor && action && (action.isForDeadFriend() ? actor.isDead() : actor.isAlive());
		};
		
		Window_BattleActor.prototype.isCurrentItemEnabled = function() {
			return this.isEnabled(this.index()) && Window_Selectable.prototype.isCurrentItemEnabled.call(this);
		};
		
		const old_drawActorItem = Window_BattleActor.prototype.drawItem;
		Window_BattleActor.prototype.drawItem = function(index) {
			this.changePaintOpacity(this.isEnabled(index));
			old_drawActorItem.call(this, index);
			this.changePaintOpacity();
		};
Ah, you're saying you wanted the user to be disabled too.

It's definitely possible to modify my plugin to also do that if the user is supposed to be excluded. Maybe something like this?

JavaScript:
        Window_BattleActor.prototype.isEnabled = function(index) {
            let actor = $gameParty.battleMembers()[index];
            let action = BattleManager.inputtingAction();
            if(action.item() && action.item().meta.SERemoveUser && action.subject() == actor) return false; // ADD THIS LINE
            return actor && action && (action.isForDeadFriend() ? actor.isDead() : actor.isAlive());
        };
      
        Window_BattleActor.prototype.isCurrentItemEnabled = function() {
            return this.isEnabled(this.index()) && Window_Selectable.prototype.isCurrentItemEnabled.call(this);
        };
      
        const old_drawActorItem = Window_BattleActor.prototype.drawItem;
        Window_BattleActor.prototype.drawItem = function(index) {
            this.changePaintOpacity(this.isEnabled(index));
            old_drawActorItem.call(this, index);
            this.changePaintOpacity();
        };
Oh my god, that does it. Thank you very much!

For some reason I don't understand... it works for my current setup (SRD_BattleStatusCustomizer, which changes the appearance of battle UI, with some new windows), but not for the vanilla setup (??). I tried both turning these off and in a fresh project, and they get the same error. (I may be doing something wrong, but I don't know what.)

Spoiler
rpg_managers.js:1949 TypeError: Cannot read property 'item' of null
at Window_BattleActor.isEnabled (SFG_AliveDeadScopeFix.js:24)
at Window_BattleActor.drawItem (SFG_AliveDeadScopeFix.js:34)
at Window_BattleActor.Window_Selectable.drawAllItems (rpg_windows.js:1256)
at Window_BattleActor.Window_BattleStatus.refresh (rpg_windows.js:5490)
at Window_BattleActor.Window_BattleStatus.initialize (rpg_windows.js:5468)
at Window_BattleActor.initialize (rpg_windows.js:5553)
at new Window_BattleActor (rpg_windows.js:5546)
at Scene_Battle.createActorWindow (rpg_scenes.js:2432)
at Scene_Battle.createAllWindows (rpg_scenes.js:2371)
at Scene_Battle.createDisplayObjects (rpg_scenes.js:2351)

That said... I don't really need it to work for the vanilla setup, just thought I'd mention it just in case, lol

I will now investigate if I can either make it check for full HP (and not let you do it) or... turn that off for MP, so they both work the same x'D
OKAY I MAY HAVE DONE IT
I can't guarantee I will never encounter problems when I test more but for now it works... I added this line:

In your .isEnabled function
JavaScript:
if(action.item().damage.type == 3 && actor.mhp == actor.hp) return false;

It may be a little silly but it checks that the item used is type 3 (HP recovery) and the target actor has full HP... I hope I don't break anything! XD Now it all works the same!! Uahh!!

Thank you very much again. And also to cathyril, I may not have used your solution, but it was a good step towards what I wanted!
The stack trace you showed can probably be fixed by replacing if(action with if(action && action, in case you run into it in a place that matters.

Original thread: https://forums.rpgmakerweb.com/threads/actually-prevent-scope-1-dead-ally-to-be-used-in-battle.183100/