How to reduce Defense stat by damage taken from particular element?

Hello!

I am relatively new to RPG Maker MZ and JavaScript, so I'm not sure if what I want to do here would require a plug-in or not. From what I've seen, I want to say that it doesn't, but if it does I'll move this to plug-in requests.

Has anyone ever played Dungeon Encounters? The way combat works in that game is that there are two types of weapons: physical and magical, and two types of defense: physical and magical. Rather than defense values negating some of the damage done to the enemy, the defense values act as a shield before HP can be touched. For example, if an enemy has 10 physical defense, you would need to deal 10 physical damage before you could start dealing damage to its HP (with physical attacks, that is; if you wanted to deal magical damage you'd need to first deal with the magic defense).

I'm looking to create a system that's basically a direct rip-off of this because dang it it's fun and this is a project I'm making strictly for fun. So far what I've thought to do was make two elements, Physical and Magical. I'm thinking of making invisible states that check to see if the defense value of the actor or the enemy is above 0, and if it is, any damage done by the Physical element is reduced to 0 (and this I've gotten to work to a pretty good extent).

Now, my main question: how could I make it so that if the actor or enemy's defense/magical defense stat is above 0, whatever damage they take will reduce that respective stat? Is there an event I could make that could take the attack value and simply reduce the defense stat by that value, or could I just bake it into the damage formula? Alternatively, will this just not work at all and I'll have to consult a plug-in?

Let me know if I need to be more specific or my question is unclear. I'm typing this late at night so if it doesn't make sense, whoops! Thanks for spending the time to read this post and I hope to see any other potential Dungeon Encounters fans out there
You might want to look at this barriers plugin:

It automates a lot of what you'll need to do, and provides a UI for the barriers.

You could do what you want to do with the default engine, but you wouldn't have the visual aspect very easily, and you really don't want to be messing around with changing parameters willy nilly as general practice. Using variables for it would be a better approach, but I'd use a plugin.
By default, only HP/MP/TP are supposed to be changed during battle.
While there are event commands to change the other parameters, those are supposed to be for long-term changes like training to get better or getting cursed and so on - and none of those are supposed to drop the parameter values down to zero.

So like the post above I strongly suggest to use a different value than DEF for that purpose - there are several ways how this could be done by different plugins.
You might want to look at this barriers plugin:

It automates a lot of what you'll need to do, and provides a UI for the barriers.

You could do what you want to do with the default engine, but you wouldn't have the visual aspect very easily, and you really don't want to be messing around with changing parameters willy nilly as general practice. Using variables for it would be a better approach, but I'd use a plugin.
Thank you! I've tried this plug-in and it does exactly what I want it to do. However, there's one thing that I wanted to change about it, if possible.

The absorption shield, when depleted, will carry over any remaining damage it has after the shield is removed. Is there any way that I could remove its ability to do this? For example, even if the shield only has 1 point left on it, and there is 100 damage incoming, the 99 would not carry over. I've been looking for ways to do this, but unless I'm missing something I don't see a way to do that in the plug-in itself.
I have no idea. Can you combine the notetags to make the state be several kinds of barriers?

It would be easy to do with Yanfly's barriers and a few state notetags, but VisuStella doesn't provide script calls to check for the barrier points, and the plugin obfuscation prevents us from finding those script calls on our own.
I have no idea. Can you combine the notetags to make the state be several kinds of barriers?

It would be easy to do with Yanfly's barriers and a few state notetags, but VisuStella doesn't provide script calls to check for the barrier points, and the plugin obfuscation prevents us from finding those script calls on our own.
My one idea so far has been to make a state that would simply negate all damage temporarily, and have this state be applied immediately after the state that gives the shield is removed. This is a very roundabout way and probably doesn't work, but it's still something I've been trying since I've honestly got nothing else lol.

For this I'm using YEP Buffs & States Core

I'm probably missing something, because right now in the state I have this:
<Custom Remove Effect>
user.addState(4);
</Custom Remove Effect>
And it doesn't add the state upon removal. Is there something else I would need to add?

My one concern with this is the timing. It seems that damage is done during the removal of the shield, and that the damage would be done before the state that negates all damage would be applied. I still think this could be worth trying, though.
After spending some time with it, here's what I've learned:

When I tried to put two barrier types on one State, it seemed to influence whichever one was of highest priority (so, for example, a TP barrier would get affected after an Absorb barrier was finished, provided both were within the same state). My idea, although it was janky, was to add a state that was a simple 9999 absorption shield that would only serve to block any damage that would overflow once the regular shield went away. I ran into some issues with this:

1. I gave the 9999 absorption shield to every actor alongside the main one. Since the 9999 shield had a lower priority, only the regular shield was affected until this one was. Using YEP Skills & States core, I tried to utilize <JS On Erase> to remove the 9999 shield once the main shield was removed, but by doing this, the system would remove the shield at the same time damage was calculated, thereby still decreasing HP by the overflow value.

2. If I tried to add the 9999 shield instead after the main shield was removed, it would add the shield after damage was calculated, leaving the actor with less HP and another shield.

I decided to contact VisuStella to see if this overflow could be changed in the meantime. So far, no response, so I'm still experimenting with this. I've tried utilizing <hitType Breaks State> but that removed the 9999 shield too early. Provided there is a way to delay the removal of the 9999 shield after damage is calculated, wouldn't this be able to work? My ideas on this are running low 😩. The last thing I could think of is creating some event that stores the HP value of actors and troops when the battle begins and if their HP value is ever changed (say: if an attack ignores barriers entirely), and when the state is removed, it calls the event to retrieve that value and manually set the actor's HP value to that. I haven't tried that yet because it would be incredibly messy, lol.
After some time, I am happy to report that this thread may be closed due to being solved! After messing around with <JS Post-Damage>, I have found a way to use this plug-in while still preventing damage overflow. Thank you for the plugin suggestion! Though it took some messing around with, it takes care of many things that make it worth it to do this work around 😤

Original thread: https://forums.rpgmakerweb.com/threads/how-to-reduce-defense-stat-by-damage-taken-from-particular-element.159685/