mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 10:22:25 +00:00
Abilities are now Stored by Name not UUID. Refactor Built In and Toggleable Abilities
This commit is contained in:
1 parent
f39d843c70
commit
6514b217b9
12 files changed
+465
-127
No files matched your search
@@ -3,13 +3,14 @@ package noppes.npcs;
|
||||
import kamkeel.npcs.controllers.data.ability.Ability;
|
||||
import kamkeel.npcs.controllers.data.ability.AbilityController;
|
||||
import kamkeel.npcs.controllers.data.ability.AbilityPhase;
|
||||
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.
|
||||
@@ -168,6 +169,140 @@ public abstract class AbstractDataAbilities {
|
||||
return currentAbility;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// 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
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -11,6 +11,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;
|
||||
@@ -297,6 +298,9 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
if (positionLocked) releaseLockedPosition();
|
||||
}
|
||||
|
||||
// Tick active toggles (independent of currentAbility)
|
||||
tickActiveToggles();
|
||||
|
||||
// Tick current ability if executing (shared logic from base class)
|
||||
if (currentAbility != null && currentAbility.isExecuting()) {
|
||||
tickCurrentAbility();
|
||||
@@ -664,6 +668,7 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
*/
|
||||
public void reset() {
|
||||
stopCurrentAbility();
|
||||
clearActiveToggles();
|
||||
|
||||
// Roll cooldown so NPC doesn't immediately attack after reset
|
||||
rollCooldownOnReset();
|
||||
@@ -983,6 +988,13 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
}
|
||||
compound.setTag("Abilities", abilityList);
|
||||
|
||||
// Active toggles
|
||||
NBTTagList toggleList = new NBTTagList();
|
||||
for (String key : activeToggles.keySet()) {
|
||||
toggleList.appendTag(new NBTTagString(key));
|
||||
}
|
||||
compound.setTag("ActiveToggles", toggleList);
|
||||
|
||||
return compound;
|
||||
}
|
||||
|
||||
@@ -1000,5 +1012,15 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
abilitySlots.add(slot);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
@@ -263,6 +263,9 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
|
||||
return;
|
||||
}
|
||||
|
||||
// Tick active toggles (independent of currentAbility)
|
||||
tickActiveToggles();
|
||||
|
||||
if (currentAbility != null && currentAbility.isExecuting()) {
|
||||
tickCurrentAbility();
|
||||
|
||||
@@ -385,6 +388,11 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onToggleStateChanged(String key, boolean active) {
|
||||
syncToClient();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// SELECTION
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
@@ -750,6 +758,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) {
|
||||
@@ -772,6 +787,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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user