From b406382564964e19d49257ede42bc6e17a57b71c Mon Sep 17 00:00:00 2001 From: Kam Date: Sun, 1 Mar 2026 15:21:47 -0500 Subject: [PATCH] Fix Counter Damage for DBC / CNPC+ --- .../java/kamkeel/npcs/addon/DBCAddon.java | 9 +++ .../data/ability/type/AbilityCounter.java | 28 ++++---- .../data/ability/type/AbilityDefend.java | 65 +++++++++++++------ 3 files changed, 64 insertions(+), 38 deletions(-) diff --git a/src/main/java/kamkeel/npcs/addon/DBCAddon.java b/src/main/java/kamkeel/npcs/addon/DBCAddon.java index ff290e9d2..08c493a3c 100644 --- a/src/main/java/kamkeel/npcs/addon/DBCAddon.java +++ b/src/main/java/kamkeel/npcs/addon/DBCAddon.java @@ -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; } diff --git a/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityCounter.java b/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityCounter.java index f0d5ab4b6..b13d78ce8 100644 --- a/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityCounter.java +++ b/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityCounter.java @@ -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; diff --git a/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityDefend.java b/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityDefend.java index c9f6e1e0a..7ca8aa3e7 100644 --- a/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityDefend.java +++ b/src/main/java/kamkeel/npcs/controllers/data/ability/type/AbilityDefend.java @@ -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); } /**