Merge upstream dev: name-based storage, UUID/DisplayName, toggle system

Resolved conflicts in AbstractDataAbilities, DataAbilities, PlayerAbilityData
to integrate both toggle system (upstream) and chain delay tracking (local).
ChainedAbility now has UUID and DisplayName fields matching Ability pattern.
ChainedAbilityController rewritten: saves in abilities/chained/, dual-index
by name and UUID, legacy migration from chained_abilities/ directory.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
KamandClaude Opus 4.6 committed 2026-02-17 23:27:49 -05:00
commit 0311dc5806
14 files changed
+628 -148

No files matched your search

@@ -5,13 +5,14 @@ import kamkeel.npcs.controllers.data.ability.AbilityController;
import kamkeel.npcs.controllers.data.ability.AbilityPhase;
import kamkeel.npcs.controllers.data.ability.ChainedAbility;
import kamkeel.npcs.controllers.data.ability.ChainedAbilityEntry;
import kamkeel.npcs.controllers.data.ability.ToggleEntry;
import kamkeel.npcs.controllers.data.telegraph.TelegraphInstance;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.DamageSource;
import noppes.npcs.controllers.AnimationController;
import noppes.npcs.controllers.data.Animation;
import java.util.List;
import java.util.*;
/**
* Shared base class for NPC and Player ability data management.
@@ -197,6 +198,140 @@ public abstract class AbstractDataAbilities {
return currentChain;
}
// ═══════════════════════════════════════════════════════════════════
// TOGGLE STATE (active toggles, independent of currentAbility)
// ═══════════════════════════════════════════════════════════════════
/** Currently active toggles. Key = ability key, Value = toggle entry with tick counter. */
protected Map<String, ToggleEntry> activeToggles = new LinkedHashMap<>();
/**
* Toggle an ability ON or OFF. If currently on, turns off; if off, turns on.
* @param key The ability key (e.g., "npcdbc:ki_fist")
* @return true if the toggle is now ON, false if OFF
*/
public boolean toggleAbility(String key) {
if (activeToggles.containsKey(key)) {
deactivateToggle(key);
return false;
} else {
return activateToggle(key);
}
}
/**
* Set a toggle to a specific state.
* @param key The ability key
* @param on true to activate, false to deactivate
*/
public void setAbilityToggled(String key, boolean on) {
if (on && !activeToggles.containsKey(key)) {
activateToggle(key);
} else if (!on && activeToggles.containsKey(key)) {
deactivateToggle(key);
}
}
/**
* Check if a toggle ability is currently active.
*/
public boolean isAbilityToggled(String key) {
return activeToggles.containsKey(key);
}
/**
* Get all active toggle keys.
*/
public Set<String> getActiveToggleKeys() {
return new LinkedHashSet<>(activeToggles.keySet());
}
private boolean activateToggle(String key) {
Ability ability = AbilityController.Instance != null
? AbilityController.Instance.resolveAbility(key) : null;
if (ability == null || !ability.isToggleable()) return false;
ToggleEntry entry = new ToggleEntry(ability);
activeToggles.put(key, entry);
ability.onToggleOn(getEntity());
onToggleStateChanged(key, true);
return true;
}
private void deactivateToggle(String key) {
ToggleEntry entry = activeToggles.remove(key);
if (entry != null) {
entry.getAbility().onToggleOff(getEntity());
onToggleStateChanged(key, false);
}
}
/**
* Tick all active toggles. Call from tick() each game tick.
* Toggles with hasActiveToggle=true get their onToggleTick() called.
* If onToggleTick returns false, the toggle is auto-deactivated.
*/
protected void tickActiveToggles() {
if (activeToggles.isEmpty()) return;
EntityLivingBase entity = getEntity();
List<String> toRemove = null;
for (Map.Entry<String, ToggleEntry> mapEntry : activeToggles.entrySet()) {
ToggleEntry entry = mapEntry.getValue();
entry.incrementTick();
if (entry.getAbility().hasActiveToggle()) {
if (!entry.getAbility().onToggleTick(entity, entry.getTickCount())) {
if (toRemove == null) toRemove = new ArrayList<>();
toRemove.add(mapEntry.getKey());
}
}
}
if (toRemove != null) {
for (String key : toRemove) {
ToggleEntry entry = activeToggles.remove(key);
if (entry != null) {
entry.getAbility().onToggleOff(entity);
onToggleStateChanged(key, false);
}
}
}
}
/** Hook for subclass to react to toggle state changes (sync packets, script events). */
protected void onToggleStateChanged(String key, boolean active) {}
/**
* Clear all active toggles (e.g., on death/reset).
* Calls onToggleOff for each active toggle.
*/
protected void clearActiveToggles() {
if (activeToggles.isEmpty()) return;
EntityLivingBase entity = getEntity();
for (ToggleEntry entry : activeToggles.values()) {
entry.getAbility().onToggleOff(entity);
}
activeToggles.clear();
}
/**
* Add a toggle entry directly without calling onToggleOn.
* Used for client-side sync and NBT restoration.
*/
public void setToggleEntryDirect(String key, boolean active) {
if (active) {
Ability ability = AbilityController.Instance != null
? AbilityController.Instance.resolveAbility(key) : null;
if (ability != null && ability.isToggleable()) {
activeToggles.put(key, new ToggleEntry(ability));
}
} else {
activeToggles.remove(key);
}
}
// ═══════════════════════════════════════════════════════════════════
// COOLDOWN MANAGEMENT
// ═══════════════════════════════════════════════════════════════════
@@ -14,6 +14,7 @@ import kamkeel.npcs.network.packets.data.telegraph.TelegraphSpawnPacket;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.DamageSource;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.entity.EntityNPCInterface;
@@ -318,6 +319,9 @@ public class DataAbilities extends AbstractDataAbilities {
if (positionLocked) releaseLockedPosition();
}
// Tick active toggles (independent of currentAbility)
tickActiveToggles();
// Tick current ability if executing, or tick chain delay between entries
if (chainDelayRemaining > 0 || (currentAbility != null && currentAbility.isExecuting())) {
tickCurrentAbility();
@@ -699,6 +703,7 @@ public class DataAbilities extends AbstractDataAbilities {
*/
public void reset() {
stopCurrentAbility();
clearActiveToggles();
// Roll cooldown so NPC doesn't immediately attack after reset
rollCooldownOnReset();
@@ -1038,6 +1043,13 @@ public class DataAbilities extends AbstractDataAbilities {
}
compound.setTag("AbilityActions", actionList);
// Active toggles
NBTTagList toggleList = new NBTTagList();
for (String key : activeToggles.keySet()) {
toggleList.appendTag(new NBTTagString(key));
}
compound.setTag("ActiveToggles", toggleList);
return compound;
}
@@ -1078,5 +1090,15 @@ public class DataAbilities extends AbstractDataAbilities {
}
}
}
// Active toggles - restore state directly (no onToggleOn callback during load)
activeToggles.clear();
if (compound.hasKey("ActiveToggles")) {
NBTTagList toggleNbt = compound.getTagList("ActiveToggles", 8); // 8 = TAG_STRING
for (int i = 0; i < toggleNbt.tagCount(); i++) {
String key = toggleNbt.getStringTagAt(i);
setToggleEntryDirect(key, true);
}
}
}
}
@@ -474,9 +474,12 @@ public class GuiNpcManageAbilities extends GuiAbilityInterface
private boolean hasDuplicateName(Ability ability) {
String name = ability.getName();
if (name == null || name.isEmpty()) return false;
String oldName = ability.getId();
// A duplicate exists if the name is already in use and it's not the same ability being edited
return customData.containsKey(name) && !name.equals(oldName);
if (!customData.containsKey(name)) return false;
// Name exists — check if it belongs to a different ability (by UUID)
String uuid = ability.getId();
if (uuid == null || uuid.isEmpty()) return true; // New ability with conflicting name
Ability existing = AbilityController.Instance.getCustomAbilityByName(name);
return existing == null || !uuid.equals(existing.getId());
}
@Override
@@ -271,6 +271,9 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
return;
}
// Tick active toggles (independent of currentAbility)
tickActiveToggles();
if (chainDelayRemaining > 0 || (currentAbility != null && currentAbility.isExecuting())) {
tickCurrentAbility();
@@ -422,6 +425,11 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
}
}
@Override
protected void onToggleStateChanged(String key, boolean active) {
syncToClient();
}
// ═══════════════════════════════════════════════════════════════════
// SELECTION
// ═══════════════════════════════════════════════════════════════════
@@ -787,6 +795,13 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
compound.setTag("PlayerAbilities", list);
compound.setInteger("PlayerAbilitySelected", selectedIndex);
compound.setBoolean("AbilityAnimating", playingAbilityAnimation);
// Active toggles
NBTTagList toggleList = new NBTTagList();
for (String key : activeToggles.keySet()) {
toggleList.appendTag(new NBTTagString(key));
}
compound.setTag("ActiveToggles", toggleList);
}
public void readFromNBT(NBTTagCompound compound) {
@@ -809,6 +824,16 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
selectedIndex = Math.max(0, unlockedAbilities.size() - 1);
}
playingAbilityAnimation = compound.getBoolean("AbilityAnimating");
// Active toggles - restore state directly (no onToggleOn callback during load)
activeToggles.clear();
if (compound.hasKey("ActiveToggles")) {
NBTTagList toggleNbt = compound.getTagList("ActiveToggles", 8); // 8 = TAG_STRING
for (int i = 0; i < toggleNbt.tagCount(); i++) {
String key = toggleNbt.getStringTagAt(i);
setToggleEntryDirect(key, true);
}
}
}
/**
@@ -61,7 +61,9 @@ public class ScriptDataAbilities implements IDataAbilities {
if (!ability.getAllowedBy().allowsNpc()) {
return; // Ability is PLAYER_ONLY
}
data.addAbilityReference(key);
// Store by UUID/registry key for stable reference
String canonicalKey = ability.getId() != null ? ability.getId() : key;
data.addAbilityReference(canonicalKey);
}
@Override
@@ -36,7 +36,7 @@ public class ScriptPlayerAbilityData implements IPlayerAbilityData {
if (!ability.getAllowedBy().allowsPlayer()) {
return; // Ability is NPC-only
}
// Use canonical key (registry key for built-in, name for custom)
// Use canonical key (registry key for built-in, UUID for custom)
String canonicalKey = ability.getId() != null ? ability.getId() : key;
data.unlockAbility(canonicalKey);
}