The Final Piece

I am almost done with the core feature of my plugin. There is just one very important piece that seems to elude me for reasons I can't seem to figure out: Switching out the ActiveWeapon and ActiveOffhand of an actor if a skill selected in battle requires a different ActiveWeapon. The main function in question is below:

PHP:
        var _Game_Action_setTarget = Game_Action.prototype.setTarget;
        Game_Action.prototype.setTarget = function(skill) {
            //If this is used by an actor...
            if (this._subjectActorId>0){
                //Checks if required weapon is active. 
                var wtypeId1 = skill.requiredWtypeId1;
                var wtypeId2 = skill.requiredWtypeId2;
                //Get results about where weapon is active (if it is) from function. 
                var wactive1 = 1; 
                if (wtypeId1>0){
                    wactive1 = this.subject().activeWtypeSlot(wtypeId1);
                }
                var wactive2 = 1; 
                if (wtypeId2>0){
                    wactive2 = this.subject().activeWtypeSlot(wtypeId2);
                }
                var wactive1 = (wtypeId1>0) ? this.subject().isWtypeActive(wtypeId1):1;
                var wactive2 = (wtypeId2>0) ? this.subject().isWtypeActive(wtypeId2):1;
                //For switching back after using a skill. 
                var origActiveWeapon = $gameActors.actor(this._subjectActorId).activeWeapon; 
                var origActiveOffhand = $gameActors.actor(this._subjectActorId).activeOffhand; 
                var origOffhandEnabled = $gameActors.actor(this._subjectActorId).OffhandEnabled; 
                //If neither weapon is the main equipped weapon (if one was, we'd do nothing)
                if (wactive1!=1 && wactive2!=1) {
                    //Check if the required weapon is in the offhand slot. 
                    if (wactive1==2||wactive2==2){
                        //var switchType = "o";
                        this.subject().switchDual(); 
                    }//Add another condition with a >2 when the option to have additional active weapons is added. 
                    else {
                        //var switchType = "e";
                        this.subject().activateBestType(wtypeId1, wtypeId2);
                    }
                }
                //alert("Pause for Debug.");
                //Call original function. 
                _Game_Action_setTarget(this, skill);
                //If activeWeaponSwapback is enabled, swap the weapons back after skill is used. 
                if (activeWeaponSwapback){
                    this.subject().activeWeapon = origActiveWeapon; 
                    this.subject().activeOffhand = origActiveOffhand; 
                    this.subject().offhandenabled = origOffhandEnabled; 
                }
            }
            //Otherwise just execute as normal. 
            else {
                _Game_Action_setTarget(this, skill);
            }
        };

And here's activeWtypeSlot:
PHP:
        //Also checks the weapon type is active, but returns a number instead--0 for no match, 1 for main, and 2 for offhand. 
        Game_Actor.prototype.activeWtypeSlot = function(wtypeId) {
            //If both active weapons are initialized, check that it matches. 
            if (this.activeOffhand && this.activeWeapon){
                if (wtypeId == this.activeWeapon.wtypeId){
                    return 1; 
                }
                else if (wtypeId == this.activeOffhand.wtypeId){
                    return 2; 
                }
                else {
                    return 0; 
                }
            }
            //If only the main weapon is active, check that it matches. 
            else if (this.activeWeapon){
                if (wtypeId == this.activeWeapon.wtypeId){
                    return 1; 
                }
                else {
                    return 0; 
                }
            }
            //Otherwise, return 0 because the actor has no active weapons (and probably no weapons at all).
            else {
                return 0; 
            }
        };

I have confirmed with plugin commands that Game_Actor.activateBestType() works as intended outside of battle. I have also been successful in ensuring that the actor's active weapon is the one used to attack in combat by default.

Am I accessing the actor using the action the right way? Or is there something else I'm missing?

Original thread: https://forums.rpgmakerweb.com/threads/the-final-piece.81348/