Counter, Dodge, Guard Abilities

This commit is contained in:
Kam committed 2026-03-01 00:09:57 -05:00
1 parent fc71fb5a33
commit b49316d9bb
7 files changed
+99 -4

No files matched your search

@@ -5,7 +5,9 @@ import cpw.mods.fml.relauncher.SideOnly;
import kamkeel.npcs.controllers.data.ability.enums.UserType;
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.api.ability.type.IAbilityCounter;
import noppes.npcs.client.gui.builder.FieldDef;
@@ -53,17 +55,39 @@ public class AbilityCounter extends AbilityDefend implements IAbilityCounter {
}
}
private transient boolean counterTriggered;
// ═══════════════════════════════════════════════════════════════════
// DEFEND HOOKS
// ═══════════════════════════════════════════════════════════════════
@Override
protected void onDefendTick(EntityLivingBase caster, EntityLivingBase target, int tick) {
if (counterTriggered && lastAttacker != null && lastAttacker.isEntityAlive()) {
counterTriggered = false;
// Calculate counter damage
float damage;
if (counterType == CounterType.FLAT) {
damage = counterValue;
} else {
damage = lastDamageTaken * (counterValue / 100.0f);
}
// Hit back
DamageSource counterSource = caster instanceof EntityPlayer
? DamageSource.causePlayerDamage((EntityPlayer) caster)
: DamageSource.causeMobDamage(caster);
lastAttacker.attackEntityFrom(counterSource, damage);
signalCompletion();
}
}
@Override
protected float performDefend(float amount) {
return amount;
counterTriggered = true;
return 0;
}
// ═══════════════════════════════════════════════════════════════════
@@ -103,7 +103,18 @@ public abstract class AbilityDefend extends Ability implements IAbilityDefend {
if (source.isMagicDamage() || source.isFireDamage() || source.isExplosion() || source.isProjectile())
return amount;
return performDefend(amount);
lastAttacker = attacker;
lastDamageTaken = amount;
hitCount++;
float result = performDefend(amount);
// Auto-complete after max hits
if (maxHitAmount > 0 && hitCount >= maxHitAmount) {
signalCompletion();
}
return result;
}
/**
@@ -45,7 +45,7 @@ public class AbilityDodge extends AbilityDefend implements IAbilityDodge {
@Override
protected float performDefend(float amount) {
return amount;
return 0;
}
// ═══════════════════════════════════════════════════════════════════
@@ -39,7 +39,7 @@ public class AbilityGuard extends AbilityDefend implements IAbilityGuard {
@Override
protected float performDefend(float amount) {
return amount;
return Math.max(0, amount * (1.0f - damageReduction));
}
// ═══════════════════════════════════════════════════════════════════
@@ -2,6 +2,7 @@ package noppes.npcs;
import kamkeel.npcs.controllers.AbilityController;
import kamkeel.npcs.controllers.data.ability.Ability;
import kamkeel.npcs.controllers.data.ability.type.AbilityDefend;
import kamkeel.npcs.controllers.data.ability.enums.AbilityPhase;
import kamkeel.npcs.controllers.data.ability.data.ChainedAbility;
import kamkeel.npcs.controllers.data.ability.data.entry.ChainedAbilityEntry;
@@ -317,6 +318,19 @@ public abstract class AbstractDataAbilities {
return currentAbility;
}
/**
* Get the active defend ability if one is currently defending.
*/
public AbilityDefend getActiveDefend() {
if (currentAbility instanceof AbilityDefend) {
AbilityDefend defend = (AbilityDefend) currentAbility;
if (defend.isDefending()) {
return defend;
}
}
return null;
}
/**
* Check if a chained ability is currently executing.
*/
@@ -10,10 +10,15 @@ import kamkeel.npcs.controllers.AbilityController;
import kamkeel.npcs.controllers.AttributeController;
import kamkeel.npcs.controllers.SyncController;
import kamkeel.npcs.controllers.data.ability.Ability;
import kamkeel.npcs.controllers.data.ability.type.AbilityCounter;
import kamkeel.npcs.controllers.data.ability.type.AbilityDefend;
import kamkeel.npcs.controllers.data.ability.type.AbilityDodge;
import kamkeel.npcs.controllers.data.ability.type.AbilityGuard;
import kamkeel.npcs.network.packets.data.ability.PlayerAbilitySyncPacket;
import kamkeel.npcs.controllers.data.ability.enums.AbilityPhase;
import kamkeel.npcs.util.AttributeAttackUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ContainerPlayer;
@@ -624,6 +629,22 @@ public class ScriptPlayerEventHandler {
noppes.npcs.scripted.event.player.PlayerEvent.AttackEvent pevent1 = new noppes.npcs.scripted.event.player.PlayerEvent.AttackEvent((IPlayer) NpcAPI.Instance().getIEntity((EntityPlayer) event.source.getEntity()), event.entityLiving, attackAmount, event.source);
cancel = cancel || EventHooks.onPlayerAttack(handler, pevent1);
}
// Dodge & Counter: cancel attack entirely
if (!cancel && event.entityLiving instanceof EntityPlayerMP) {
PlayerData pData = PlayerData.get((EntityPlayer) event.entityLiving);
if (pData != null && pData.abilityData != null) {
AbilityDefend defend = pData.abilityData.getActiveDefend();
if (defend instanceof AbilityDodge || defend instanceof AbilityCounter) {
EntityLivingBase attacker = source instanceof EntityLivingBase ? (EntityLivingBase) source : null;
float result = defend.onDefend(attacker, event.source, event.ammount);
if (result != event.ammount) {
cancel = true;
}
}
}
}
event.setCanceled(cancel);
}
}
@@ -653,6 +674,13 @@ public class ScriptPlayerEventHandler {
event.ammount = 0;
cancel = true;
} else {
// Guard: reduce damage
AbilityDefend defend = pData.abilityData.getActiveDefend();
if (defend instanceof AbilityGuard) {
EntityLivingBase attacker = source instanceof EntityLivingBase ? (EntityLivingBase) source : null;
event.ammount = defend.onDefend(attacker, event.source, event.ammount);
}
float modified = pData.abilityData.onDamage(event.source, event.ammount);
event.ammount = modified;
}
@@ -6,6 +6,9 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.addon.DBCAddon;
import kamkeel.npcs.controllers.data.ability.type.AbilityCounter;
import kamkeel.npcs.controllers.data.ability.type.AbilityDefend;
import kamkeel.npcs.controllers.data.ability.type.AbilityDodge;
import kamkeel.npcs.addon.client.DBCClient;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumSoundOperation;
@@ -709,6 +712,21 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
// Resistances
i = stats.resistances.applyResistance(damagesource, i);
// Defend abilities
AbilityDefend defend = this.abilities != null ? this.abilities.getActiveDefend() : null;
if (defend != null) {
// Dodge & Counter: cancel attack entirely
if (defend instanceof AbilityDodge || defend instanceof AbilityCounter) {
float result = defend.onDefend(attackingEntity, damagesource, i);
if (result != i) {
return false;
}
} else {
// Guard: reduce damage
i = defend.onDefend(attackingEntity, damagesource, i);
}
}
NpcEvent.DamagedEvent event = new NpcEvent.DamagedEvent(this.wrappedNPC, attackingEntity, i, damagesource);
if (EventHooks.onNPCDamaged(this, event) || isKilled())
return false;