YF Auto Passive/Buffs & States ; Damage "reduction"

Yay, Lunatic Mode!
Okay, so, I was reading about the Thornmail on YanFly's site, and got to wondering:

What if you could adjust that and make it heal the target after damage instead of doing recoil to the user? The state would heal, but only after the original damage was calculated and applied.

So, let's say "A" attacks "B"
B's HP: 101
A's attack does 100, B drops to 1, then heals 50% of what was done— Taking him back to 51HP. However, if the damage done would be ≥101, B would die and that heal wouldn't trigger.

Exactly how would you do that? I'm super new to JS, and simply attempted to edit YanFly's Thornmail script, but it apparently doesn't work that way.

This is what I did(feel free to laugh, because it's super bad)
Spoiler: Code
Code:
<Custom React Effect>
// Check to see if any physical damage is dealt.
if (value > 0 && this.isPhysical()) {
  // Sets the Recoil Rate to 15%.
  var rate = 0.15;
  // Determines the amount of recoil damage dealt.
  var recoil = value * rate;
  // Sets the DEF bonus rate to 25%.
  var rate = 0.25;
  // Determines the amount of bonus damage dealt.
  var bonus = target.def * rate;
  // Rounds up the bonus damage and recoil damage.
  var dmg = Math.ceil(bonus + recoil);
  // Makes the target gain HP equal to the dmg variable divided by half.
  target.gainHp(dmg / 2);
}
</Custom React Effect>

This is the original:
Spoiler: Code
Code:
<Custom React Effect>
// Check to see if any physical damage is dealt.
if (value > 0 && this.isPhysical()) {
  // Sets the Recoil Rate to 15%.
  var rate = 0.15;
  // Determines the amount of recoil damage dealt.
  var recoil = value * rate;
  // Sets the DEF bonus rate to 25%.
  var rate = 0.25;
  // Determines the amount of bonus damage dealt.
  var bonus = target.def * rate;
  // Rounds up the bonus damage and recoil damage.
  var dmg = Math.ceil(bonus + recoil);
  // Makes the attacker lose damage equal to the dmg variable.
  user.gainHp(-1 * dmg);
  // Check to see if the attacker is dead.
  if (user.isDead()) {
   // If the attacker is dead, make it collapse.
   user.performCollapse();
  }
}
</Custom React Effect>

So, if you guys don't mind, let me know what I did wrong :hswt:
I'm not 100% sure but I think the effect numbers are calculated and applied after the damage formula. So if you add a healing effect to the skill, it should work only if the target survives the dam from the damage form.
I'm not 100% sure but I think the effect numbers are calculated and applied after the damage formula. So if you add a healing effect to the skill, it should work only if the target survives the dam from the damage form.

How do you add the damage dealt to be the variable?
I'm not at my comp right now but once I can see the particulars, I'll let you know.

Original thread: https://forums.rpgmakerweb.com/threads/yf-auto-passive-buffs-states-damage-reduction.107897/