Trap skill -> Detonates when physically attacked.

Hey everyone!

I'm trying to make a trap skill and I just can't figure out how to get it to work. Here's what I want it to do:
Trapper has several types of traps.
Trapper uses "Poison Trap" on ALLY A.
"P. Trap" state is applied to Ally A for 3 rounds.
If ALLY A is physically attacked, ALLY A counters with "Poison Explosion" skill damaging attacking enemy.
"P. Trap" state is removed from ALLY A.

Seems simple, but I can NOT figure this one out. I've gotten it to MOSTLY work with a custom counter (with Yanfly's Counter Control), but the state isn't removed when ALLY A counters with the exploding trap skill. If I use the "remove by damage" option in the standard state settings, the state is removed by magic damage as well.

Is there a way to make a state that activates a common event when the character is hit?

Is there a completely different way to do this I'm not thinking of?

Any help would be appreciated.

Thanks!
Take a look at Buffs & States Core and its available notetags. Specifically:

Code:
State Notetags:

<Custom Respond Effect>
code
code
</Custom Respond Effect>

This effect will run when the battler is selected as a target. This will
occur only if the action connects and will occur after damage execution.
This effect will run after damage calculation occurs.
This looks promising! Looking into it now. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/trap-skill-detonates-when-physically-attacked.111315/