Files
CustomNPC-Plus/src/main/java/noppes/npcs/scripted/wrapper/ScriptDataAbilities.java
T

219 lines
6.2 KiB
Java
Raw Normal View History

2026-01-28 14:44:19 -05:00
package noppes.npcs.scripted.wrapper;
2026-02-18 04:02:51 -05:00
import kamkeel.npcs.controllers.AbilityController;
2026-02-18 05:13:51 -05:00
import kamkeel.npcs.controllers.data.ability.Ability;
import kamkeel.npcs.controllers.data.ability.AbilityAction;
2026-01-28 14:44:19 -05:00
import net.minecraft.entity.EntityLivingBase;
import noppes.npcs.DataAbilities;
import noppes.npcs.api.ability.IAbility;
import noppes.npcs.api.ability.IDataAbilities;
import noppes.npcs.entity.EntityNPCInterface;
2026-01-30 20:21:45 -03:00
import java.util.List;
2026-01-28 14:44:19 -05:00
/**
* Script wrapper for DataAbilities providing API access to NPC abilities.
*/
public class ScriptDataAbilities implements IDataAbilities {
private final DataAbilities data;
private final EntityNPCInterface npc;
public ScriptDataAbilities(EntityNPCInterface npc) {
this.npc = npc;
this.data = npc.abilities;
}
@Override
public boolean isEnabled() {
return data.enabled;
}
@Override
public void setEnabled(boolean enabled) {
data.enabled = enabled;
}
@Override
public IAbility[] getAbilities() {
return data.getAbilities().toArray(new IAbility[0]);
}
@Override
public void addAbility(IAbility ability) {
if (ability instanceof Ability) {
Ability a = (Ability) ability;
// Validate ability allows NPCs
if (!a.getAllowedBy().allowsNpc()) {
return; // Ability is PLAYER_ONLY
}
data.addAbility(a);
2026-01-28 14:44:19 -05:00
}
}
2026-01-30 05:01:54 -05:00
@Override
public void addAbilityReference(String key) {
// Validate ability exists and allows NPCs
Ability ability = AbilityController.Instance.resolveAbility(key);
if (ability == null) {
return; // Ability doesn't exist
}
if (!ability.getAllowedBy().allowsNpc()) {
return; // Ability is PLAYER_ONLY
}
// Store by UUID/registry key for stable reference
String canonicalKey = ability.getId() != null ? ability.getId() : key;
data.addAbilityReference(canonicalKey);
2026-01-30 05:01:54 -05:00
}
2026-01-28 14:44:19 -05:00
@Override
public void removeAbility(String abilityId) {
data.removeAbility(abilityId);
}
@Override
public IAbility getAbility(String abilityId) {
return data.getAbility(abilityId);
}
@Override
public boolean hasAbility(String abilityId) {
return data.getAbility(abilityId) != null;
}
2026-01-30 05:01:54 -05:00
@Override
public boolean isAbilityReference(String abilityId) {
List<AbilityAction> slots = data.getAbilityActions();
2026-01-30 05:01:54 -05:00
for (int i = 0; i < slots.size(); i++) {
AbilityAction slot = slots.get(i);
if (slot.isAbilityReference()) {
2026-01-30 05:01:54 -05:00
if (slot.getReferenceId().equals(abilityId)) return true;
} else if (!slot.isReference()) {
2026-01-30 05:01:54 -05:00
Ability a = slot.getAbility();
2026-02-18 04:02:51 -05:00
if (a != null && abilityId.equals(a.getId())) return false;
2026-01-30 05:01:54 -05:00
}
}
return false;
}
@Override
public boolean convertToInline(String abilityId) {
List<AbilityAction> slots = data.getAbilityActions();
2026-01-30 05:01:54 -05:00
for (int i = 0; i < slots.size(); i++) {
AbilityAction slot = slots.get(i);
if (slot.isAbilityReference() && slot.getReferenceId().equals(abilityId)) {
2026-01-30 05:01:54 -05:00
return data.convertToInline(i);
}
}
return false;
}
2026-01-28 14:44:19 -05:00
@Override
public void clearAbilities() {
data.clearAbilities();
}
@Override
public IAbility getCurrentAbility() {
return data.getCurrentAbility();
}
@Override
public boolean isExecutingAbility() {
return data.isExecutingAbility();
}
@Override
public void interruptCurrentAbility() {
data.interruptCurrentAbility(null, 0);
}
@Override
public int getGlobalCooldown() {
return (int) data.getRemainingCooldown();
}
@Override
public void setGlobalCooldown(int ticks) {
2026-02-18 04:02:51 -05:00
if (ticks <= 0) {
data.resetCooldown();
} else {
data.setCooldownEndTime(npc.worldObj.getTotalWorldTime() + ticks);
}
2026-01-28 14:44:19 -05:00
}
@Override
public void resetCooldowns() {
data.resetCooldown();
}
@Override
public boolean forceStartAbility(String abilityId) {
return forceStartAbility(abilityId, null);
}
@Override
public boolean forceStartAbility(String abilityId, Object target) {
// Find the ability
Ability ability = data.getAbility(abilityId);
if (ability == null) {
return false;
}
// Resolve target
EntityLivingBase targetEntity = resolveTarget(target);
// Use the direct force start method
return data.forceStartAbility(ability, targetEntity);
}
@Override
2026-01-30 05:01:54 -05:00
public boolean executeAbility(String key) {
return executeAbility(key, null);
2026-01-28 14:44:19 -05:00
}
@Override
2026-01-30 05:01:54 -05:00
public boolean executeAbility(String key, Object target) {
// Validate ability exists and allows NPCs
Ability ability = AbilityController.Instance.resolveAbility(key);
if (ability == null) {
return false; // Ability doesn't exist
}
if (!ability.getAllowedBy().allowsNpc()) {
return false; // Ability is PLAYER_ONLY
}
2026-01-28 14:44:19 -05:00
EntityLivingBase targetEntity = resolveTarget(target);
2026-01-30 05:01:54 -05:00
return data.executeAbility(key, targetEntity);
2026-01-28 14:44:19 -05:00
}
@Override
public IAbility createAbility(String typeId) {
if (AbilityController.Instance == null) {
return null;
}
return AbilityController.Instance.create(typeId);
}
/**
* Resolve a target object to an EntityLivingBase.
*/
private EntityLivingBase resolveTarget(Object target) {
if (target == null) {
// Try to use the NPC's current attack target
return npc.getAttackTarget();
}
if (target instanceof EntityLivingBase) {
return (EntityLivingBase) target;
}
if (target instanceof noppes.npcs.api.entity.IEntityLivingBase) {
Object mcEntity = ((noppes.npcs.api.entity.IEntityLivingBase<?>) target).getMCEntity();
if (mcEntity instanceof EntityLivingBase) {
return (EntityLivingBase) mcEntity;
}
}
return null;
}
}