Need Help for what you call TP Shield

Hi guys! I could use your advise. I'm working on a TP Guard System.
Problem is, I'm at a loss for how to go about it.
I'm building it from scratch and borrowing methods from the core mv scripts.

What I have so far is this:

Spoiler
Code:
Game_Action.prototype.executeHpDamage = function(target, value) {
//custom calculation for TP damage    
var tpdmg = value;
    if ((target.tp > 0) && (value >= 0)) {
        if (value > target.tp){
            value = value - target.tp;
        } else {
            value = 0;
        }
    }
    
    if (this.isDrain()) {
        value = Math.min(target.hp, value);
    }
    this.makeSuccess(target);
    target.gainHp(-value);
    if (value > 0) {
        target.onDamage(value);
        target.TpGrdDamage(tpdmg); //TP Damage processing function.
    }
    this.gainDrainedHp(value);
};

Game_Battler.prototype.onDamage = function(value) {
    this.removeStatesByDamage();
    //MODDED Removed Tp Charge from Damage.
   //this.chargeTpByDamage(value / this.mhp);
};

//TP Damage Function
Game_Battler.TpGrdDamage = function(value) {
    this.gainTp(-value);
};

It's a small script based on my VX Ace version. But with how different MV and VX Ace are in coding, translating it from Ruby to java is a bigger challenge than I realized.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-for-what-you-call-tp-shield.120047/