MV The stateOrigin() Function Returns Incorrect Unit

So I started using console.log() quite often as of late and by doing so, I've found out why "origin" hasn't been working for me all this time. It turns out, it's selecting the wrong unit! For instance, if my Hero casts Ignite to inflict a Burn on the target, the "origin" of the Burn state should be our Hero. However, when I placed a <Custom Apply Effect> notetag containing console.log(this.stateOrigin()), it returns the following when I cast the spell:
Game_Enemy {_hp: 175, _mp: 140, _tp: 54, _hidden: false, _paramPlus: Array(8), …}
In other words, the game thinks the "origin" of the Burn state is the targeted enemy instead of the caster of the spell that inflicted the Burn! The following is a snippet from YEP_BuffStatesCore.js that I believe defines which unit is the "origin". I'm hoping to find something in here to edit so that the correct unit is considered the "origin" instead. (I'm also open to other solutions/suggestions)
JavaScript:
Game_BattlerBase.prototype.stateOrigin = function(stateId) {
    if (this._stateOrigin === undefined) this._stateOrigin = {};
    if (!this._stateOrigin[stateId]) return this;
    var arr = this._stateOrigin[stateId];
    if (arr[0] === 0) return $gameActors.actor(arr[1]);
    if (arr[0] === 1) return $gameTroop.members()[arr[1]];
    return this;
};

Game_BattlerBase.prototype.setStateOrigin = function(stateId, battler) {
    if (this._stateOrigin === undefined) this._stateOrigin = {};
    if (!$gameParty.inBattle()) return;
    if (!battler) {
      var battler = BattleManager._subject;
      if (!battler) return;
    }
    if (battler.isActor()) {
      var arr = [0, battler._actorId];
    } else {
      var arr = [1, battler.index()];
    }
    this._stateOrigin[stateId] = arr;
};
I really hope to get "origin" working, cuz my workarounds are getting more and more complicated...
I mean just look at what I have to put in my Skill & State notetags to replicate origin's functionality!
Spoiler: Skill: Ignite
JavaScript:
<letbs>
scope: circle(3)
sequence: skill
</letbs>
<Custom Show Eval>
if (!$gameParty.inBattle()) {
  visible = true;
} else if ($gameParty.menuActor().battleSkills().contains($dataSkills[272])) {
  visible = true;
} else {
  visible = false;
}
</Custom Show Eval>
<Critical Rate Formula>
rate = user.cri - (user.agi / 1000) + (user.mat / 1000) - target.cev;
</Critical Rate Formula>
<Critical Multiplier Formula>
value *= 1.5 + (user.mat / 100) + bonus;
</Critical Multiplier Formula>
<Custom Execution>
if (user.isLearnedSkill(273)) {
  var w = Math.round(user.mat / 4);
} else {
  var w = 0;
}
if (user.isLearnedSkill(277)) {
  var x = 1;
} else {
  var x = 0;
}
if (user.isLearnedSkill(285)) {
  var y = 1;
} else {
  var y = 0;
}
if (user.isLearnedSkill(289)) {
  var z = 1;
} else {
  var z = 0;
}
if (user == $gameParty.members()[0]) {
  $gameVariables.setValue(27, user.mat);
  $gameVariables.setValue(30, user.mdf);
  $gameVariables.setValue(84, w);
  $gameVariables.setValue(87, x);
  $gameVariables.setValue(90, y);
  $gameVariables.setValue(93, z);
} else if (user == $gameParty.members()[1]) {
  $gameVariables.setValue(28, user.mat);
  $gameVariables.setValue(31, user.mdf);
  $gameVariables.setValue(85, w);
  $gameVariables.setValue(88, x);
  $gameVariables.setValue(91, y);
  $gameVariables.setValue(94, z);
} else if (user == $gameParty.members()[2]) {
  $gameVariables.setValue(29, user.mat);
  $gameVariables.setValue(32, user.mdf);
  $gameVariables.setValue(86, w);
  $gameVariables.setValue(89, x);
  $gameVariables.setValue(92, y);
  $gameVariables.setValue(95, z);
}
</Custom Execution>
<After Eval>
if (user == $gameParty.members()[0]) {
  $gameVariables.setValue(27, 0);
  $gameVariables.setValue(30, 0);
  $gameVariables.setValue(84, 0);
  $gameVariables.setValue(87, 0);
  $gameVariables.setValue(90, 0);
  $gameVariables.setValue(93, 0);
} else if (user == $gameParty.members()[1]) {
  $gameVariables.setValue(28, 0);
  $gameVariables.setValue(31, 0);
  $gameVariables.setValue(85, 0);
  $gameVariables.setValue(88, 0);
  $gameVariables.setValue(91, 0);
  $gameVariables.setValue(94, 0);
} else if (user == $gameParty.members()[2]) {
  $gameVariables.setValue(29, 0);
  $gameVariables.setValue(32, 0);
  $gameVariables.setValue(86, 0);
  $gameVariables.setValue(89, 0);
  $gameVariables.setValue(92, 0);
  $gameVariables.setValue(95, 0);
}
</After Eval>
<Custom State 142 Turns>
if (user.isLearnedSkill(281)) {
  turn += 1;
}
</Custom State 142 Turns>
Spoiler: State: Burning
JavaScript:
<Custom Apply Effect>
console.log(this.stateOrigin());
console.log(this._stateOrigin);
var intellect = $gameVariables.value(27) + $gameVariables.value(28) + $gameVariables.value(29);
var willpower = $gameVariables.value(30) + $gameVariables.value(31) + $gameVariables.value(32);
var hyperthermia = $gameVariables.value(84) + $gameVariables.value(85) + $gameVariables.value(86);
var cauterization = $gameVariables.value(87) + $gameVariables.value(88) + $gameVariables.value(89);
var firePower = $gameVariables.value(90) + $gameVariables.value(91) + $gameVariables.value(92);
var flammable = $gameVariables.value(93) + $gameVariables.value(94) + $gameVariables.value(95);
target._burn = target._burn || 0;
target._burnINT = intellect;
target._burnWIL = willpower;
target._hyperthermia = hyperthermia;
target._cauterization = cauterization;
target._firePower = firePower;
target._flammable = flammable;
if (target._burn == 0) {
  target.minusAtk(hyperthermia);
  target.minusMdf(hyperthermia);
  target._burn += 1;
}
</Custom Apply Effect>
<Custom React Effect>
if (value > 0 && this.isHpEffect() && this.getItemElements().contains(4) && target._firePower > 0) {
  var elementRate = (target.elementRate(4) + target.stateTurns(142) * 0.05) / target.elementRate(4);
  value = Math.round(value * elementRate);
}
if (value < 0 && this.isHpEffect() && target._cauterization > 0) {
  value = Math.round(value * 0.75);
}
</Custom React Effect>
<Custom Regenerate Effect>
var value = Math.round(target.elementRate(4) * target._burnINT ** (1 + target._burnWIL / 1000) * 0.3 * (100 + target.level) / (100 + target.mdf));
var msg = target.name() + ' took ' + value + ' \\c[17]Fire\\c damage!';
console.log(target.elementRate(4));
console.log(target._burnINT);
console.log(target._burnWIL);
console.log(target.level);
console.log(target.mdf);
target.gainHp(-value);
BattleManagerTBS._logWindow.addText(msg);
BattleManagerTBS.activeEntity().addPopup();
//BattleManagerTBS._logWindow.clear();
</Custom Regenerate Effect>
<Custom Leave Effect>
target.addState(143);
</Custom Leave Effect>
<Custom Remove Effect>
target.addAtk(target._hyperthermia);
target.addMdf(target._hyperthermia);
delete target._hyperthermia;
delete target._firePower;
delete target._burn;
</Custom Remove Effect>
<element rate: 5, -30% />
<Reapply Add Turns>
Spoiler: Previous threads
Yanfly's Buffs & States Core is not the problem here. You can disable all plugins except Buffs & States Core and test this for yourself. So another plugin is at fault. I assume it's something related to LeCode Tactical Battle System, correct? Please remember to mention that, it's very relevant information. 😴

Good luck finding a solution! 👋
Yes, I am aware the Buff & States Core plugin is not bugged or anything. I simply want to edit it in some way to make it compatible w/ LeTBS. I figured it would be easier to tackle the issue from the YEP side instead of the LeCode side (most people are not willing to work w/ LeTBS code). Sorry if it sounded like I was saying the Yanfly plugin wasn't working (it's not bugged in any way). I just want it to work differently than the default code in order to (hopefully) make it compatible w/ LeTBS. I have made plugin request threads (both paid and non-paid) regarding LeTBS compatibility issues but had no luck w/ that... I think some people will see "LTBS" somewhere and just stop reading further as a result; that is what I was afraid of. Apologies for any confusion, and thank you for your time.

tl;dr I'm hoping this issue can be resolved by editing YEP_BuffStateCore.js in some way instead of going through the daunting prospect of going through LeTBS code.
I took a quick look at this and I can make the following guess as to what the problem is:

setStateOrigin is called from battler.addState without a battler object being passed to it, only a stateId. setStateOrigin has a section specifically for when this happens, where it takes the current BattleManager._subject and uses it as the battler parameter if one isn't provided.

I suspect that the problem here is that the BattleManager at the point this state is being applied has already decided that the current subject is the target of the attack, not the attacker. To test this, use the console to check BattleManager._subject at various times during your skill usage.

Building on this, I think you might also have the problem not that the origin is being set as the target incorrectly, but that it is being set as the next battler to take an action after the state is applied. I'd bet that if you tested with multiple actors in the same battle, if one actor put the state on the target, and the next turn was the second actor, the origin would be set as the second actor.

I think the problem here is timing. The state is being added after LeTBS has already told the BattleManager that current turn has already ended.

A workaround could be to use code to force the origin to be set by a skill being used. I know from speaking with you at length about LeTBS that there are things you can run at various points during an action, similar to an <after eval> or something, so you could force the origin to be set for that specific state by calling target.setStateOrigin(stateId, user), which would overwrite the current origin (the target, for some reason) on state stateId with the correct origin (the subject).
Ah, that makes total sense! I really want to try it now, but I was about to hit the hay lol. So I'm gonna try that out 1st thing in the morning. Thanks!

Edit: I couldn't wait, so I tried it out...and it works!!!😂
Lol, I'm literally squealing like a little girl right now, I'm so happy! 😀

Original thread: https://forums.rpgmakerweb.com/threads/the-stateorigin-function-returns-incorrect-unit.131489/