How to Make A Thorns State That Returns Damage to Attacker?

I can't seem to figure this one out. I'm trying to make a skill that will heal hit points, add a defense buff and a thorns state for two turns. I can figure everything else out except how to make a thorns state, where a percentage of damage dealt is returned to the dealer. The two big problems is that 1) the enemy only takes damage when their attack lands, and lands on the character that has the state active; 2) it takes a percentage of the damage done; 3) it's only for physical attacks. Anyone know how to achieve this?
Sounds exactly like Thornmail, which Yanfly has a Tips and Tricks video on, along with the code, here: http://yanfly.moe/2016/03/12/tips-tricks-thornmail/

Just apply it to a state, skip the part where wearing the armour grants the state, comment out or delete the bonus damage from the code and you should be set.

EDIT: decided just to edit the code for you myself, since it only takes a few seconds.

Spoiler: Place this in the state's notebox
<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;
// Rounds up the recoil damage.
var dmg = Math.ceil(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 in short: download and install the Yanfly Core Engine if you haven't already (you'll need it to make this work), install the required Buffs & States Core plugin under it, paste that code into the notebox of your Thorns state, make a skill that applies the Thorns state and set the Thorns state to wear off after two turns.
That's awesome, can't wait to try it out! Thanks so much! I looked around everywhere for a video but for some reason that one didn't show up. I figured Yanfly would have a plugin for it, but the description for Buffs & States Core didn't sound like it would work for that.

[Edit: This worked great! Thanks again! Admins, y'all can close this topic. ^_^ ]

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-a-thorns-state-that-returns-damage-to-attacker.102949/