mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 16:12:17 +00:00
IAbilityAction interface, AbilityAction unified slot, and bug fixes
- Added IAbilityAction interface (internal + API) for unified Ability/ChainedAbility handling - Created AbilityAction (replaces AbilitySlot) with 3 modes: INLINE_ABILITY, ABILITY_REFERENCE, CHAIN_REFERENCE - Unified DataAbilities storage from dual lists to single List<AbilityAction> - Added ChainedAbility, ChainedAbilityEntry, ChainedAbilityController - Updated PlayerAbilityData with resolveActionKey() for chain: prefix resolution - Added resolveAction/canResolveAction to AbilityController - Fixed unsafe cast in DataAbilities.onDamage() - Fixed NPE in GuiNPCAbilities for chain reference slots - Fixed NPC chain interrupt double cooldown - Fixed removeAbility() incorrectly removing chain references Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
1 parent
f39d843c70
commit
2f814c1903
17 files changed
+1482
-144
No files matched your search
@@ -38,7 +38,7 @@ import java.util.List;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
|
||||
public abstract class Ability implements IAbility {
|
||||
public abstract class Ability implements IAbility, IAbilityAction {
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// CONFIGURATION (saved to NBT)
|
||||
@@ -1277,6 +1277,16 @@ public abstract class Ability implements IAbility {
|
||||
return AbilityController.Instance.fromNBT(this.writeNBT());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChain() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAbilityAction deepCopyAction() {
|
||||
return deepCopy();
|
||||
}
|
||||
|
||||
public boolean isNpcInlineEdit() {
|
||||
return npcInlineEdit;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
package kamkeel.npcs.controllers.data.ability;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
/**
|
||||
* Unified slot for storing any combat action: inline ability, ability reference, or chain reference.
|
||||
* Replaces {@link AbilitySlot} with support for all {@link IAbilityAction} types.
|
||||
* <p>
|
||||
* References are lazy-resolved via their respective controllers and cached until
|
||||
* the controller revision changes.
|
||||
*/
|
||||
public class AbilityAction {
|
||||
|
||||
public enum SlotType {
|
||||
INLINE_ABILITY,
|
||||
ABILITY_REFERENCE,
|
||||
CHAIN_REFERENCE
|
||||
}
|
||||
|
||||
private SlotType slotType;
|
||||
private Ability inlineAbility;
|
||||
private String referenceId;
|
||||
|
||||
/** Per-slot enabled override for reference slots (null = use master's default). */
|
||||
private Boolean enabledOverride;
|
||||
|
||||
// Cache
|
||||
private transient IAbilityAction cachedAction;
|
||||
private transient int cachedRevision = -1;
|
||||
|
||||
private AbilityAction() {}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// FACTORY METHODS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public static AbilityAction inline(Ability ability) {
|
||||
AbilityAction slot = new AbilityAction();
|
||||
slot.slotType = SlotType.INLINE_ABILITY;
|
||||
slot.inlineAbility = ability;
|
||||
return slot;
|
||||
}
|
||||
|
||||
public static AbilityAction abilityReference(String referenceId) {
|
||||
AbilityAction slot = new AbilityAction();
|
||||
slot.slotType = SlotType.ABILITY_REFERENCE;
|
||||
slot.referenceId = referenceId;
|
||||
return slot;
|
||||
}
|
||||
|
||||
public static AbilityAction chainReference(String referenceId) {
|
||||
AbilityAction slot = new AbilityAction();
|
||||
slot.slotType = SlotType.CHAIN_REFERENCE;
|
||||
slot.referenceId = referenceId;
|
||||
return slot;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// QUERIES
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public SlotType getSlotType() {
|
||||
return slotType;
|
||||
}
|
||||
|
||||
public boolean isReference() {
|
||||
return slotType != SlotType.INLINE_ABILITY;
|
||||
}
|
||||
|
||||
public boolean isChainReference() {
|
||||
return slotType == SlotType.CHAIN_REFERENCE;
|
||||
}
|
||||
|
||||
public boolean isAbilityReference() {
|
||||
return slotType == SlotType.ABILITY_REFERENCE;
|
||||
}
|
||||
|
||||
public String getReferenceId() {
|
||||
return referenceId;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// RESOLUTION
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Resolve the action. Inline returns directly; references resolve
|
||||
* from the appropriate controller with version-based caching.
|
||||
*/
|
||||
public IAbilityAction getAction() {
|
||||
switch (slotType) {
|
||||
case INLINE_ABILITY:
|
||||
return inlineAbility;
|
||||
|
||||
case ABILITY_REFERENCE: {
|
||||
AbilityController ctrl = AbilityController.Instance;
|
||||
if (ctrl == null) return null;
|
||||
int rev = ctrl.getCustomAbilityRevision();
|
||||
if (cachedAction != null && cachedRevision == rev) {
|
||||
return cachedAction;
|
||||
}
|
||||
Ability resolved = ctrl.resolveAbility(referenceId);
|
||||
if (resolved != null && enabledOverride != null) {
|
||||
resolved.setEnabled(enabledOverride);
|
||||
}
|
||||
cachedAction = resolved;
|
||||
cachedRevision = rev;
|
||||
return cachedAction;
|
||||
}
|
||||
|
||||
case CHAIN_REFERENCE: {
|
||||
ChainedAbilityController ctrl = ChainedAbilityController.Instance;
|
||||
if (ctrl == null) return null;
|
||||
int rev = ctrl.getRevision();
|
||||
if (cachedAction != null && cachedRevision == rev) {
|
||||
return cachedAction;
|
||||
}
|
||||
ChainedAbility resolved = ctrl.resolve(referenceId);
|
||||
if (resolved != null && enabledOverride != null) {
|
||||
resolved.setEnabled(enabledOverride);
|
||||
}
|
||||
cachedAction = resolved;
|
||||
cachedRevision = rev;
|
||||
return cachedAction;
|
||||
}
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get as Ability (returns null if this is a chain slot or resolution fails).
|
||||
*/
|
||||
public Ability getAbility() {
|
||||
IAbilityAction action = getAction();
|
||||
return (action instanceof Ability) ? (Ability) action : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get as ChainedAbility (returns null if this is an ability slot or resolution fails).
|
||||
*/
|
||||
public ChainedAbility getChainedAbility() {
|
||||
IAbilityAction action = getAction();
|
||||
return (action instanceof ChainedAbility) ? (ChainedAbility) action : null;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// ENABLED OVERRIDE
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
if (slotType == SlotType.INLINE_ABILITY) {
|
||||
if (inlineAbility != null) {
|
||||
inlineAbility.setEnabled(enabled);
|
||||
}
|
||||
} else {
|
||||
this.enabledOverride = enabled;
|
||||
if (cachedAction != null) {
|
||||
cachedAction.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// INLINE CONVERSION (ability references only)
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public Ability getInlineAbility() {
|
||||
return inlineAbility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert this reference slot to an inline slot by copying the resolved ability.
|
||||
* Only works for ability references (chains cannot be inlined).
|
||||
*/
|
||||
public boolean convertToInline() {
|
||||
if (slotType != SlotType.ABILITY_REFERENCE) return false;
|
||||
|
||||
Ability resolved = getAbility();
|
||||
if (resolved == null) return false;
|
||||
|
||||
inlineAbility = AbilityController.Instance.fromNBT(resolved.writeNBT());
|
||||
slotType = SlotType.INLINE_ABILITY;
|
||||
referenceId = null;
|
||||
cachedAction = null;
|
||||
cachedRevision = -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// NBT
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public NBTTagCompound writeNBT() {
|
||||
switch (slotType) {
|
||||
case CHAIN_REFERENCE: {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
nbt.setString("ChainReference", referenceId);
|
||||
if (enabledOverride != null) {
|
||||
nbt.setBoolean("RefEnabled", enabledOverride);
|
||||
}
|
||||
return nbt;
|
||||
}
|
||||
|
||||
case ABILITY_REFERENCE: {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
nbt.setString("Reference", referenceId);
|
||||
if (enabledOverride != null) {
|
||||
nbt.setBoolean("RefEnabled", enabledOverride);
|
||||
}
|
||||
return nbt;
|
||||
}
|
||||
|
||||
case INLINE_ABILITY:
|
||||
default:
|
||||
return inlineAbility != null ? inlineAbility.writeNBT() : new NBTTagCompound();
|
||||
}
|
||||
}
|
||||
|
||||
public static AbilityAction fromNBT(NBTTagCompound nbt) {
|
||||
if (nbt == null) return null;
|
||||
|
||||
// Chain reference (new format)
|
||||
if (nbt.hasKey("ChainReference")) {
|
||||
AbilityAction slot = chainReference(nbt.getString("ChainReference"));
|
||||
if (nbt.hasKey("RefEnabled")) {
|
||||
slot.enabledOverride = nbt.getBoolean("RefEnabled");
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
// Ability reference (existing format)
|
||||
if (nbt.hasKey("Reference")) {
|
||||
AbilityAction slot = abilityReference(nbt.getString("Reference"));
|
||||
if (nbt.hasKey("RefEnabled")) {
|
||||
slot.enabledOverride = nbt.getBoolean("RefEnabled");
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
// Inline ability (existing format)
|
||||
Ability ability = AbilityController.Instance.fromNBT(nbt);
|
||||
if (ability == null) return null;
|
||||
return inline(ability);
|
||||
}
|
||||
}
|
||||
@@ -448,6 +448,36 @@ public class AbilityController implements IAbilityHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// UNIFIED ACTION RESOLUTION
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Resolve any action key to an IAbilityAction.
|
||||
* Checks abilities first, then chained abilities.
|
||||
*/
|
||||
public IAbilityAction resolveAction(String key) {
|
||||
if (key == null || key.isEmpty()) return null;
|
||||
|
||||
Ability ability = resolveAbility(key);
|
||||
if (ability != null) return ability;
|
||||
|
||||
if (ChainedAbilityController.Instance != null) {
|
||||
return ChainedAbilityController.Instance.resolve(key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a key can be resolved as any action (ability or chain).
|
||||
*/
|
||||
public boolean canResolveAction(String key) {
|
||||
if (canResolveAbility(key)) return true;
|
||||
return ChainedAbilityController.Instance != null
|
||||
&& ChainedAbilityController.Instance.canResolve(key);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// EXTENSION POINTS
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
@@ -621,4 +651,19 @@ public class AbilityController implements IAbilityHandler {
|
||||
public boolean deleteCustomAbilityByName(String name) {
|
||||
return deleteCustomAbility(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getChainedAbilityNames() {
|
||||
return ChainedAbilityController.Instance.getNames().toArray(new String[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasChainedAbilityName(String name) {
|
||||
return ChainedAbilityController.Instance.hasName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteChainedAbilityByName(String name) {
|
||||
return ChainedAbilityController.Instance.delete(name);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,10 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
* <p>
|
||||
* References are lazy-resolved via {@link AbilityController#resolveAbility(String)}
|
||||
* and cached until the controller version changes.
|
||||
*
|
||||
* @deprecated Use {@link AbilityAction} instead, which supports abilities and chained abilities.
|
||||
*/
|
||||
@Deprecated
|
||||
public class AbilitySlot {
|
||||
|
||||
private Ability inlineAbility;
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
package kamkeel.npcs.controllers.data.ability;
|
||||
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import noppes.npcs.api.ability.IChainedAbility;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A chained ability is an ordered sequence of ability references that execute
|
||||
* one after another. It has its own conditions, cooldown, weight, and per-entry
|
||||
* delay configuration. Individual ability conditions are skipped during chain execution.
|
||||
* <p>
|
||||
* All entries must be ability references (by name/key). Inline abilities are not supported.
|
||||
* When saving, all references are validated against {@link AbilityController}.
|
||||
*/
|
||||
public class ChainedAbility implements IChainedAbility, IAbilityAction {
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// CONFIGURATION (saved to NBT)
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
private String name = "";
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
/** Weight for NPC weighted random selection. */
|
||||
private int weight = 10;
|
||||
|
||||
/** Whether each ability in the chain uses its own windup phase. */
|
||||
private boolean windUpAll = true;
|
||||
|
||||
/** Chain-level cooldown in ticks (applied after entire chain completes). */
|
||||
private int cooldownTicks = 100;
|
||||
|
||||
/** Minimum range for NPC eligibility check. */
|
||||
private float minRange = 0;
|
||||
|
||||
/** Maximum range for NPC eligibility check. */
|
||||
private float maxRange = 20;
|
||||
|
||||
/** Which entity types can use this chained ability. */
|
||||
private UserType allowedBy = UserType.BOTH;
|
||||
|
||||
/** Chain-level conditions (individual ability conditions are ignored). */
|
||||
private List<Condition> conditions = new ArrayList<>();
|
||||
|
||||
/** Ordered list of ability entries to execute. */
|
||||
private List<ChainedAbilityEntry> entries = new ArrayList<>();
|
||||
|
||||
public ChainedAbility() {}
|
||||
|
||||
public ChainedAbility(String name) {
|
||||
this.name = name != null ? name : "";
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// GETTERS / SETTERS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name != null ? name : "";
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = Math.max(1, weight);
|
||||
}
|
||||
|
||||
public boolean isWindUpAll() {
|
||||
return windUpAll;
|
||||
}
|
||||
|
||||
public void setWindUpAll(boolean windUpAll) {
|
||||
this.windUpAll = windUpAll;
|
||||
}
|
||||
|
||||
public int getCooldownTicks() {
|
||||
return cooldownTicks;
|
||||
}
|
||||
|
||||
public void setCooldownTicks(int cooldownTicks) {
|
||||
this.cooldownTicks = Math.max(0, cooldownTicks);
|
||||
}
|
||||
|
||||
public float getMinRange() {
|
||||
return minRange;
|
||||
}
|
||||
|
||||
public void setMinRange(float minRange) {
|
||||
this.minRange = Math.max(0, minRange);
|
||||
}
|
||||
|
||||
public float getMaxRange() {
|
||||
return maxRange;
|
||||
}
|
||||
|
||||
public void setMaxRange(float maxRange) {
|
||||
this.maxRange = Math.max(0, maxRange);
|
||||
}
|
||||
|
||||
public UserType getAllowedBy() {
|
||||
return allowedBy;
|
||||
}
|
||||
|
||||
public void setAllowedBy(UserType allowedBy) {
|
||||
this.allowedBy = allowedBy != null ? allowedBy : UserType.BOTH;
|
||||
}
|
||||
|
||||
public List<Condition> getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
public void setConditions(List<Condition> conditions) {
|
||||
this.conditions = conditions != null ? conditions : new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<ChainedAbilityEntry> getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
public void setEntries(List<ChainedAbilityEntry> entries) {
|
||||
this.entries = entries != null ? entries : new ArrayList<>();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// IChainedAbility INTERFACE
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@Override
|
||||
public int getEntryCount() {
|
||||
return entries.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEntryReference(int index) {
|
||||
if (index < 0 || index >= entries.size()) return null;
|
||||
return entries.get(index).getAbilityReference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEntryDelay(int index) {
|
||||
if (index < 0 || index >= entries.size()) return 0;
|
||||
return entries.get(index).getDelayTicks();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// ENTRY MANAGEMENT
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public void addEntry(ChainedAbilityEntry entry) {
|
||||
if (entry != null) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public void addEntry(String abilityReference, int delayTicks) {
|
||||
entries.add(new ChainedAbilityEntry(abilityReference, delayTicks));
|
||||
}
|
||||
|
||||
public void removeEntry(int index) {
|
||||
if (index >= 0 && index < entries.size()) {
|
||||
entries.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
public void moveEntryUp(int index) {
|
||||
if (index > 0 && index < entries.size()) {
|
||||
ChainedAbilityEntry entry = entries.remove(index);
|
||||
entries.add(index - 1, entry);
|
||||
}
|
||||
}
|
||||
|
||||
public void moveEntryDown(int index) {
|
||||
if (index >= 0 && index < entries.size() - 1) {
|
||||
ChainedAbilityEntry entry = entries.remove(index);
|
||||
entries.add(index + 1, entry);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// CONDITIONS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Check all chain-level conditions against the caster and target.
|
||||
*/
|
||||
public boolean checkConditions(EntityLivingBase caster, EntityLivingBase target) {
|
||||
for (Condition c : conditions) {
|
||||
if (!c.check(caster, target)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check conditions for player use (skip target-requiring conditions).
|
||||
*/
|
||||
public boolean checkConditionsForPlayer(EntityLivingBase caster) {
|
||||
for (Condition c : conditions) {
|
||||
if (c.requiresTarget()) continue;
|
||||
if (!c.check(caster, null)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// VALIDATION
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Validate this chained ability. Returns a list of error messages.
|
||||
* An empty list means valid.
|
||||
*/
|
||||
public List<String> validate() {
|
||||
List<String> errors = new ArrayList<>();
|
||||
|
||||
if (name == null || name.isEmpty()) {
|
||||
errors.add("Name is required");
|
||||
}
|
||||
|
||||
if (entries.isEmpty()) {
|
||||
errors.add("At least one ability entry is required");
|
||||
}
|
||||
|
||||
AbilityController controller = AbilityController.Instance;
|
||||
if (controller != null) {
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
ChainedAbilityEntry entry = entries.get(i);
|
||||
String ref = entry.getAbilityReference();
|
||||
if (ref == null || ref.isEmpty()) {
|
||||
errors.add("Entry " + (i + 1) + ": ability reference is empty");
|
||||
} else if (!controller.canResolveAbility(ref)) {
|
||||
errors.add("Entry " + (i + 1) + ": cannot resolve ability '" + ref + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// DEEP COPY
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Create a deep copy via NBT round-trip.
|
||||
*/
|
||||
public ChainedAbility deepCopy() {
|
||||
ChainedAbility copy = new ChainedAbility();
|
||||
copy.readNBT(this.writeNBT());
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChain() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAbilityAction deepCopyAction() {
|
||||
return deepCopy();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// NBT SERIALIZATION
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public NBTTagCompound writeNBT() {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
nbt.setString("Name", name);
|
||||
nbt.setBoolean("Enabled", enabled);
|
||||
nbt.setInteger("Weight", weight);
|
||||
nbt.setBoolean("WindUpAll", windUpAll);
|
||||
nbt.setInteger("CooldownTicks", cooldownTicks);
|
||||
nbt.setFloat("MinRange", minRange);
|
||||
nbt.setFloat("MaxRange", maxRange);
|
||||
nbt.setInteger("AllowedBy", allowedBy.ordinal());
|
||||
|
||||
// Conditions
|
||||
NBTTagList condList = new NBTTagList();
|
||||
for (Condition c : conditions) {
|
||||
condList.appendTag(c.writeNBT());
|
||||
}
|
||||
nbt.setTag("Conditions", condList);
|
||||
|
||||
// Entries
|
||||
NBTTagList entryList = new NBTTagList();
|
||||
for (ChainedAbilityEntry entry : entries) {
|
||||
entryList.appendTag(entry.writeNBT());
|
||||
}
|
||||
nbt.setTag("Entries", entryList);
|
||||
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public void readNBT(NBTTagCompound nbt) {
|
||||
name = nbt.getString("Name");
|
||||
enabled = !nbt.hasKey("Enabled") || nbt.getBoolean("Enabled");
|
||||
weight = nbt.hasKey("Weight") ? nbt.getInteger("Weight") : 10;
|
||||
windUpAll = !nbt.hasKey("WindUpAll") || nbt.getBoolean("WindUpAll");
|
||||
cooldownTicks = nbt.hasKey("CooldownTicks") ? nbt.getInteger("CooldownTicks") : 100;
|
||||
minRange = nbt.getFloat("MinRange");
|
||||
maxRange = nbt.hasKey("MaxRange") ? nbt.getFloat("MaxRange") : 20;
|
||||
allowedBy = UserType.fromOrdinal(nbt.getInteger("AllowedBy"));
|
||||
|
||||
// Conditions
|
||||
conditions.clear();
|
||||
NBTTagList condList = nbt.getTagList("Conditions", 10);
|
||||
for (int i = 0; i < condList.tagCount(); i++) {
|
||||
Condition c = Condition.fromNBT(condList.getCompoundTagAt(i));
|
||||
if (c != null) {
|
||||
conditions.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
// Entries
|
||||
entries.clear();
|
||||
NBTTagList entryList = nbt.getTagList("Entries", 10);
|
||||
for (int i = 0; i < entryList.tagCount(); i++) {
|
||||
ChainedAbilityEntry entry = ChainedAbilityEntry.fromNBT(entryList.getCompoundTagAt(i));
|
||||
if (entry != null) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package kamkeel.npcs.controllers.data.ability;
|
||||
|
||||
import kamkeel.npcs.controllers.SyncController;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Global registry and storage for {@link ChainedAbility} definitions.
|
||||
* Follows the same patterns as {@link AbilityController} for custom abilities:
|
||||
* JSON file storage, revision tracking for cache invalidation, and client sync.
|
||||
*/
|
||||
public class ChainedAbilityController {
|
||||
|
||||
public static ChainedAbilityController Instance = new ChainedAbilityController();
|
||||
|
||||
private final Map<String, ChainedAbility> chainedAbilities = new LinkedHashMap<>();
|
||||
|
||||
/** Incremented on any change; used for cache invalidation. */
|
||||
private int revision = 0;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// LOAD / SAVE
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Load all chained abilities from disk.
|
||||
*/
|
||||
public void load() {
|
||||
chainedAbilities.clear();
|
||||
|
||||
File dir = getDir();
|
||||
File[] files = dir.exists() ? dir.listFiles() : null;
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (!file.isFile() || !file.getName().endsWith(".json")) continue;
|
||||
try {
|
||||
String filename = file.getName();
|
||||
String key = filename.substring(0, filename.length() - 5);
|
||||
|
||||
NBTTagCompound nbt = NBTJsonUtil.LoadFile(file);
|
||||
ChainedAbility chain = new ChainedAbility();
|
||||
chain.readNBT(nbt);
|
||||
|
||||
// Use filename as authoritative key
|
||||
chain.setName(key);
|
||||
chainedAbilities.put(key, chain);
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading chained ability: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
revision++;
|
||||
LogWriter.info("Loaded " + chainedAbilities.size() + " chained abilities");
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a chained ability to disk. Handles renames and uniqueness.
|
||||
*/
|
||||
public boolean save(ChainedAbility chain) {
|
||||
if (chain == null) return false;
|
||||
|
||||
String name = chain.getName();
|
||||
if (name == null || name.isEmpty()) return false;
|
||||
|
||||
File dir = getDir();
|
||||
File fileNew = new File(dir, name + ".json_new");
|
||||
File fileCurrent = new File(dir, name + ".json");
|
||||
|
||||
try {
|
||||
NBTTagCompound nbt = chain.writeNBT();
|
||||
NBTJsonUtil.SaveFile(fileNew, nbt);
|
||||
|
||||
if (fileCurrent.exists()) {
|
||||
fileCurrent.delete();
|
||||
}
|
||||
fileNew.renameTo(fileCurrent);
|
||||
if (fileNew.exists()) {
|
||||
fileNew.delete();
|
||||
}
|
||||
|
||||
chainedAbilities.put(name, chain);
|
||||
revision++;
|
||||
LogWriter.info("Saved chained ability: " + name);
|
||||
SyncController.syncAllChainedAbilities();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error saving chained ability: " + name, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a chained ability by name.
|
||||
*/
|
||||
public boolean delete(String name) {
|
||||
if (name == null || name.isEmpty()) return false;
|
||||
|
||||
ChainedAbility removed = chainedAbilities.remove(name);
|
||||
if (removed == null) return false;
|
||||
|
||||
File dir = getDir();
|
||||
File file = new File(dir, name + ".json");
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
revision++;
|
||||
LogWriter.info("Deleted chained ability: " + name);
|
||||
SyncController.syncAllChainedAbilities();
|
||||
return true;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// RESOLUTION
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Resolve a chained ability by name, returning a deep copy.
|
||||
* Tries exact match first, then case-insensitive.
|
||||
*/
|
||||
public ChainedAbility resolve(String name) {
|
||||
if (name == null || name.isEmpty()) return null;
|
||||
|
||||
// Exact match
|
||||
ChainedAbility chain = chainedAbilities.get(name);
|
||||
if (chain != null) return chain.deepCopy();
|
||||
|
||||
// Case-insensitive match
|
||||
for (Map.Entry<String, ChainedAbility> entry : chainedAbilities.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(name)) {
|
||||
return entry.getValue().deepCopy();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a name can be resolved without creating a deep copy.
|
||||
*/
|
||||
public boolean canResolve(String name) {
|
||||
if (name == null || name.isEmpty()) return false;
|
||||
|
||||
if (chainedAbilities.containsKey(name)) return true;
|
||||
|
||||
for (String key : chainedAbilities.keySet()) {
|
||||
if (key.equalsIgnoreCase(name)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// QUERIES
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public ChainedAbility get(String name) {
|
||||
return chainedAbilities.get(name);
|
||||
}
|
||||
|
||||
public Set<String> getNames() {
|
||||
return new LinkedHashSet<>(chainedAbilities.keySet());
|
||||
}
|
||||
|
||||
public Map<String, ChainedAbility> getChainedAbilities() {
|
||||
return chainedAbilities;
|
||||
}
|
||||
|
||||
public boolean hasName(String name) {
|
||||
return chainedAbilities.containsKey(name);
|
||||
}
|
||||
|
||||
public int getRevision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// CLIENT SYNC
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Set chained abilities from sync payload (client-side).
|
||||
*/
|
||||
public void setChainedAbilities(Map<String, ChainedAbility> synced) {
|
||||
chainedAbilities.clear();
|
||||
chainedAbilities.putAll(synced);
|
||||
revision++;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// STORAGE
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
private File getDir() {
|
||||
File dir = new File(CustomNpcs.getWorldSaveDirectory(), "chained_abilities");
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package kamkeel.npcs.controllers.data.ability;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
/**
|
||||
* A single entry in a {@link ChainedAbility}, representing one ability
|
||||
* to execute in sequence along with its delay configuration.
|
||||
*/
|
||||
public class ChainedAbilityEntry {
|
||||
|
||||
/** Reference key (built-in name or custom ability name). */
|
||||
private String abilityReference = "";
|
||||
|
||||
/** Delay in ticks BEFORE this ability starts (after previous entry completes). */
|
||||
private int delayTicks = 0;
|
||||
|
||||
public ChainedAbilityEntry() {}
|
||||
|
||||
public ChainedAbilityEntry(String abilityReference, int delayTicks) {
|
||||
this.abilityReference = abilityReference != null ? abilityReference : "";
|
||||
this.delayTicks = Math.max(0, delayTicks);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// GETTERS / SETTERS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public String getAbilityReference() {
|
||||
return abilityReference;
|
||||
}
|
||||
|
||||
public void setAbilityReference(String abilityReference) {
|
||||
this.abilityReference = abilityReference != null ? abilityReference : "";
|
||||
}
|
||||
|
||||
public int getDelayTicks() {
|
||||
return delayTicks;
|
||||
}
|
||||
|
||||
public void setDelayTicks(int delayTicks) {
|
||||
this.delayTicks = Math.max(0, delayTicks);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// NBT
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public NBTTagCompound writeNBT() {
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
nbt.setString("Reference", abilityReference);
|
||||
nbt.setInteger("Delay", delayTicks);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public static ChainedAbilityEntry fromNBT(NBTTagCompound nbt) {
|
||||
if (nbt == null) return null;
|
||||
|
||||
ChainedAbilityEntry entry = new ChainedAbilityEntry();
|
||||
entry.abilityReference = nbt.getString("Reference");
|
||||
entry.delayTicks = Math.max(0, nbt.getInteger("Delay"));
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package kamkeel.npcs.controllers.data.ability;
|
||||
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Common interface for all combat actions that can be assigned to NPCs or players.
|
||||
* Implemented by {@link Ability} (individual ability) and {@link ChainedAbility} (sequence).
|
||||
* <p>
|
||||
* This enables unified storage via {@link AbilityAction}, unified weighted random selection,
|
||||
* and unified eligibility checking without instanceof or separate codepaths.
|
||||
*/
|
||||
public interface IAbilityAction {
|
||||
|
||||
String getName();
|
||||
|
||||
boolean isEnabled();
|
||||
|
||||
void setEnabled(boolean enabled);
|
||||
|
||||
int getWeight();
|
||||
|
||||
int getCooldownTicks();
|
||||
|
||||
float getMinRange();
|
||||
|
||||
float getMaxRange();
|
||||
|
||||
UserType getAllowedBy();
|
||||
|
||||
List<Condition> getConditions();
|
||||
|
||||
boolean checkConditions(EntityLivingBase caster, EntityLivingBase target);
|
||||
|
||||
boolean checkConditionsForPlayer(EntityLivingBase caster);
|
||||
|
||||
/**
|
||||
* Whether this action is a chained ability (sequence of abilities) vs an individual ability.
|
||||
*/
|
||||
boolean isChain();
|
||||
|
||||
/**
|
||||
* Create a deep copy of this action.
|
||||
*/
|
||||
IAbilityAction deepCopyAction();
|
||||
|
||||
NBTTagCompound writeNBT();
|
||||
}
|
||||
Reference in new issue
Block a user