How to gain MP when receiving damage?

I'd like to get something up where when a character receives damage, they gain a % of that damage as MP.

Can anyone suggest how to do this? I've looked at the existing options for traits etc. and it doesn't seem to be possible without a bit of extra work.
You should be able to do this with Yanfly's Buffs & States Core plugin: http://yanfly.moe/2015/12/25/yep-50-buffs-states-core/

It offers a variety of state notetags including <Custom Respond Effect>, which lets you run some code immediately after the user gets hit. Haven't tested, but I think something like this could work:
Code:
<Custom Respond Effect>
if (value > 0) {
  var gain = Math.floor(value * 0.5);
  user.gainMp(gain);
}
</Custom Respond Effect>
This says "if the battler affected by this state receives more than zero damage, make them gain half of that (rounded down) as MP". (A negative value means healing.) You can change the multiplier to whatever you like.
Thanks! With your help I was able to do it - only difference was I had to use target.gainMp

Original thread: https://forums.rpgmakerweb.com/threads/how-to-gain-mp-when-receiving-damage.103415/