Fix Counter Damage for DBC / CNPC+

This commit is contained in:
Kam committed 2026-03-01 15:21:47 -05:00
1 parent eacb3f8c5f
commit b406382564
3 files changed
+64 -38

No files matched your search

@@ -37,6 +37,15 @@ public class DBCAddon {
public void doDBCDamage(EntityNPCInterface npc, float attackStrength, Entity receiver) {
}
/**
* Returns the pre-calculated DBC attack damage of the current attacker,
* or the vanilla damage if DBC damage was not calculated.
* Used by Counter (PERCENT mode) to base counter damage on the full DBC attack, not vanilla base.
*/
public float getAttackerDBCDamage(float vanillaDamage) {
return vanillaDamage;
}
public boolean isKO(EntityNPCInterface npc, EntityPlayer player) {
return false;
}
@@ -9,10 +9,7 @@ import kamkeel.npcs.controllers.data.ability.gui.AbilityFieldDefs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import noppes.npcs.NpcDamageSource;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.api.ability.type.IAbilityCounter;
import noppes.npcs.client.gui.builder.FieldDef;
@@ -66,27 +63,24 @@ public class AbilityCounter extends AbilityDefend implements IAbilityCounter {
@Override
protected float performDefend(EntityLivingBase attacker, float amount) {
// For PERCENT mode with DBC, use the full DBC-calculated damage, not vanilla base
float incomingDamage = amount;
if (DBCAddon.IsAvailable() && attacker instanceof EntityPlayer) {
incomingDamage = DBCAddon.instance.getAttackerDBCDamage(amount);
}
// Calculate counter damage
float damage;
if (counterType == CounterType.FLAT) {
damage = counterValue;
} else {
damage = amount * (counterValue / 100.0f);
damage = incomingDamage * (counterValue / 100.0f);
}
// Hit back
if (caster != null && attacker.isEntityAlive()) {
if (caster instanceof EntityNPCInterface && DBCAddon.instance.canDBCAttack((EntityNPCInterface) caster, damage, attacker)) {
// NPC with DBC: dummy hit for events/knockback, then apply DBC damage with NPC's stats
attacker.attackEntityFrom(new NpcDamageSource("mob", (EntityNPCInterface) caster), 0.001f);
DBCAddon.instance.doDBCDamage((EntityNPCInterface) caster, damage, attacker);
} else {
// Player or non-DBC NPC: normal damage
DamageSource counterSource = caster instanceof EntityPlayer
? DamageSource.causePlayerDamage((EntityPlayer) caster)
: DamageSource.causeMobDamage(caster);
attacker.attackEntityFrom(counterSource, damage);
}
// Counter-attack through the standard ability damage pipeline.
// DBCAbilityExtender handles DBC stat routing (ignore flags, scaling, etc.) automatically.
if (caster != null && attacker.isEntityAlive() && damage > 0) {
applyAbilityDamage(caster, attacker, damage, 0);
}
return 0;
@@ -41,6 +41,10 @@ public abstract class AbilityDefend extends Ability implements IAbilityDefend {
protected transient float lastDamageTaken;
protected transient Animation pendingDefendAnimation;
protected transient int defendAnimEndTick = -1;
/** Prevents double-counting hits when multiple damage paths call onDefend for the same hit in a single tick. */
protected transient long lastDefendTick = -1;
/** Defers signalCompletion to the next tick so all damage paths for the last hit still see isDefending()=true. */
protected transient boolean pendingCompletion;
// ═══════════════════════════════════════════════════════════════════
// CONSTRUCTOR DEFAULTS
@@ -69,7 +73,7 @@ public abstract class AbilityDefend extends Ability implements IAbilityDefend {
@Override
public void onActiveTick(EntityLivingBase caster, EntityLivingBase target, int tick) {
if (tick >= durationTicks) {
if (pendingCompletion || tick >= durationTicks) {
signalCompletion();
}
}
@@ -83,6 +87,8 @@ public abstract class AbilityDefend extends Ability implements IAbilityDefend {
lastDamageTaken = 0.0f;
pendingDefendAnimation = null;
defendAnimEndTick = -1;
lastDefendTick = -1;
pendingCompletion = false;
}
// ═══════════════════════════════════════════════════════════════════
@@ -99,30 +105,47 @@ public abstract class AbilityDefend extends Ability implements IAbilityDefend {
* @return The modified damage to apply (unchanged if not physical melee)
*/
public final float onDefend(EntityLivingBase attacker, DamageSource source, float amount) {
if (!isDefending())
if (!isDefending()) {
return amount;
if (attacker == null || source == null)
}
if (attacker == null || source == null) {
return amount;
// Only react to physical melee damage
if (source.isMagicDamage() || source.isFireDamage() || source.isExplosion() || source.isProjectile())
return amount;
lastAttacker = attacker;
lastDamageTaken = amount;
hitCount++;
float result = performDefend(attacker, amount);
// Queue defend reaction animation (played on next tick by AbstractDataAbilities)
pendingDefendAnimation = getDefendAnimation();
// Auto-complete after max hits
if (maxHitAmount > 0 && hitCount >= maxHitAmount) {
signalCompletion();
}
return result;
// Only react to physical melee damage
if (source.isMagicDamage() || source.isFireDamage() || source.isExplosion() || source.isProjectile()) {
return amount;
}
// Deduplicate per tick: when multiple damage paths (e.g. vanilla + DBC) call onDefend
// for the same hit, only count it once for hitCount / signalCompletion / animation.
long currentTick = (caster != null && caster.worldObj != null)
? caster.worldObj.getTotalWorldTime() : -1;
boolean firstCallThisTick = (currentTick < 0 || currentTick != lastDefendTick);
// If max hits reached on a previous tick and this is a NEW hit, reject it
if (pendingCompletion && firstCallThisTick) {
return amount;
}
if (firstCallThisTick) {
lastDefendTick = currentTick;
lastAttacker = attacker;
lastDamageTaken = amount;
hitCount++;
// Queue defend reaction animation (played on next tick by AbstractDataAbilities)
pendingDefendAnimation = getDefendAnimation();
// Defer completion to next tick so all damage paths in this tick
// still see isDefending()=true and apply guard reduction.
if (maxHitAmount > 0 && hitCount >= maxHitAmount) {
pendingCompletion = true;
}
}
// Always calculate the correct reduction for the caller's damage amount
return performDefend(attacker, amount);
}
/**