mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-19 07:19:05 +00:00
Fix Some GUI Saving Bugs in Abilities and Chained Abilities
This commit is contained in:
1 parent
755a9313e4
commit
a94219cd0d
5 files changed
+61
-43
No files matched your search
@@ -38,6 +38,8 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.api.handler.IAbilityHandler;
|
||||
import noppes.npcs.controllers.PlayerDataController;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -376,6 +378,23 @@ public class AbilityController implements IAbilityHandler {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
// Clean up online players' unlocked ability lists
|
||||
if (PlayerDataController.Instance != null) {
|
||||
for (PlayerData pData : PlayerDataController.Instance.getAllPlayerData()) {
|
||||
if (pData.abilityData != null) {
|
||||
boolean changed = false;
|
||||
if (uuid != null && !uuid.isEmpty() && pData.abilityData.hasUnlockedAbility(uuid)) {
|
||||
pData.abilityData.lockAbility(uuid);
|
||||
changed = true;
|
||||
}
|
||||
if (pData.abilityData.hasUnlockedAbility(name)) {
|
||||
pData.abilityData.lockAbility(name);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
customAbilityRevision++;
|
||||
LogWriter.info("Deleted custom ability: " + name);
|
||||
SyncController.syncAllCustomAbilities();
|
||||
@@ -542,6 +561,22 @@ public class AbilityController implements IAbilityHandler {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
// Clean up online players' unlocked ability lists (chain keys use "chain:" prefix)
|
||||
if (PlayerDataController.Instance != null) {
|
||||
for (PlayerData pData : PlayerDataController.Instance.getAllPlayerData()) {
|
||||
if (pData.abilityData != null) {
|
||||
String chainKey = "chain:" + (uuid != null && !uuid.isEmpty() ? uuid : name);
|
||||
if (pData.abilityData.hasUnlockedAbility(chainKey)) {
|
||||
pData.abilityData.lockAbility(chainKey);
|
||||
}
|
||||
String chainNameKey = "chain:" + name;
|
||||
if (!chainNameKey.equals(chainKey) && pData.abilityData.hasUnlockedAbility(chainNameKey)) {
|
||||
pData.abilityData.lockAbility(chainNameKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
chainedAbilityRevision++;
|
||||
LogWriter.info("Deleted chained ability: " + name);
|
||||
SyncController.syncAllChainedAbilities();
|
||||
|
||||
@@ -124,9 +124,6 @@ public class AbilityAction {
|
||||
return cachedAction;
|
||||
}
|
||||
Ability resolved = ctrl.resolveAbility(referenceId);
|
||||
if (resolved != null && enabledOverride != null) {
|
||||
resolved.setEnabled(enabledOverride);
|
||||
}
|
||||
cachedAction = resolved;
|
||||
cachedRevision = rev;
|
||||
return cachedAction;
|
||||
@@ -140,9 +137,6 @@ public class AbilityAction {
|
||||
return cachedAction;
|
||||
}
|
||||
ChainedAbility resolved = ctrl.resolveChainedAbility(referenceId);
|
||||
if (resolved != null && enabledOverride != null) {
|
||||
resolved.setEnabled(enabledOverride);
|
||||
}
|
||||
cachedAction = resolved;
|
||||
cachedRevision = rev;
|
||||
return cachedAction;
|
||||
@@ -184,12 +178,22 @@ public class AbilityAction {
|
||||
}
|
||||
} else {
|
||||
this.enabledOverride = enabled;
|
||||
if (cachedAction != null) {
|
||||
cachedAction.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this slot is enabled. For inline slots, delegates to the action directly.
|
||||
* For reference slots, uses enabledOverride if set, otherwise the resolved action's own state.
|
||||
* This avoids mutating the shared controller instance.
|
||||
*/
|
||||
public boolean isSlotEnabled() {
|
||||
if (enabledOverride != null) {
|
||||
return enabledOverride;
|
||||
}
|
||||
IAbilityAction action = getAction();
|
||||
return action != null && action.isEnabled();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// INLINE CONVERSION (ability references only)
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -393,7 +393,7 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
|
||||
for (AbilityAction slot : actionSlots) {
|
||||
IAbilityAction action = slot.getAction();
|
||||
if (action != null && isActionEligible(action, target)) {
|
||||
if (action != null && isActionEligible(slot, action, target)) {
|
||||
eligible.add(action);
|
||||
weights.add(action.getWeight());
|
||||
totalWeight += action.getWeight();
|
||||
@@ -451,11 +451,11 @@ public class DataAbilities extends AbstractDataAbilities {
|
||||
/**
|
||||
* Check if an action (ability or chain) is eligible for use.
|
||||
*/
|
||||
private boolean isActionEligible(IAbilityAction action, EntityLivingBase target) {
|
||||
private boolean isActionEligible(AbilityAction slot, IAbilityAction action, EntityLivingBase target) {
|
||||
if (!action.getAllowedBy().allowsNpc()) {
|
||||
return false;
|
||||
}
|
||||
if (!action.isEnabled()) {
|
||||
if (!slot.isSlotEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -219,14 +219,7 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
|
||||
// On/Off toggle for selected slot (under Load button) — chains and abilities
|
||||
if (selectedSlotIndex >= 0 && selectedSlotIndex < npcSlots.size() && selectedEntryIndex == -1) {
|
||||
AbilityAction selectedSlot = npcSlots.get(selectedSlotIndex);
|
||||
boolean isEnabled = false;
|
||||
if (selectedSlot.isChain()) {
|
||||
ChainedAbility chain = selectedSlot.isInlineChain() ? selectedSlot.getInlineChain() : selectedSlot.getChainedAbility();
|
||||
isEnabled = chain != null && chain.isEnabled();
|
||||
} else {
|
||||
Ability ability = selectedSlot.getAbility();
|
||||
isEnabled = ability != null && ability.isEnabled();
|
||||
}
|
||||
boolean isEnabled = selectedSlot.isSlotEnabled();
|
||||
GuiNpcButton toggleBtn = new GuiNpcButton(BTN_TOGGLE_SLOT, centerX, y + 112, 40, 20,
|
||||
new String[]{"gui.off", "gui.on"}, isEnabled ? 1 : 0);
|
||||
addButton(toggleBtn);
|
||||
@@ -713,7 +706,7 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
|
||||
|
||||
String typeName = getDisplayName(ability.getTypeId());
|
||||
String customName = ability.getName();
|
||||
String colorPrefix = ability.isEnabled() ? "\u00A7a\u25CF\u00A7r " : "\u00A7c\u25CF\u00A7r ";
|
||||
String colorPrefix = slot.isSlotEnabled() ? "\u00A7a\u25CF\u00A7r " : "\u00A7c\u25CF\u00A7r ";
|
||||
|
||||
String nameDisplay;
|
||||
if (customName != null && !customName.isEmpty() && !customName.equals(typeName)) {
|
||||
@@ -734,8 +727,7 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
|
||||
private String getChainHeaderEntry(int index, AbilityAction slot) {
|
||||
ChainedAbility chain = slot.isInlineChain() ? slot.getInlineChain() : slot.getChainedAbility();
|
||||
String chainName = chain != null ? chain.getDisplayName() : "???";
|
||||
boolean enabled = chain != null && chain.isEnabled();
|
||||
String colorPrefix = enabled ? "\u00A7a\u25CF\u00A7r " : "\u00A7c\u25CF\u00A7r ";
|
||||
String colorPrefix = slot.isSlotEnabled() ? "\u00A7a\u25CF\u00A7r " : "\u00A7c\u25CF\u00A7r ";
|
||||
if (slot.isChainReference()) {
|
||||
return colorPrefix + (index + 1) + ". \u00A7e> [Chain] " + chainName + "\u00A7r";
|
||||
}
|
||||
@@ -1090,39 +1082,29 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
|
||||
* Called from SubGuiAbilityConfig when ability is saved.
|
||||
*/
|
||||
public void onAbilitySaved(Ability ability) {
|
||||
// Check if we were editing a global parent ability via "Modify Parent" on a chain entry
|
||||
// Editing a global parent ability via "Modify Parent" on a chain entry
|
||||
if (editingChainEntryParent) {
|
||||
PacketClient.sendClient(new CustomAbilitySavePacket(ability.writeNBT()));
|
||||
editingChainEntryParent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if we were editing an ability inside a chain entry
|
||||
if (editingChainEntry && editChainSlotIdx >= 0 && editChainSlotIdx < npcSlots.size()) {
|
||||
AbilityAction slot = npcSlots.get(editChainSlotIdx);
|
||||
ChainedAbility chain = slot.isInlineChain() ? slot.getInlineChain() : null;
|
||||
if (chain != null && editChainEntryIdx >= 0 && editChainEntryIdx < chain.getEntries().size()) {
|
||||
// Update the inline ability on the entry — it's already the same object reference,
|
||||
// but refresh the list and save to persist
|
||||
}
|
||||
editingChainEntry = false;
|
||||
editChainSlotIdx = -1;
|
||||
editChainEntryIdx = -1;
|
||||
updateNpcAbilitiesList();
|
||||
save();
|
||||
// Editing an ability inside a chain entry — already same object reference.
|
||||
// handleAbilityConfigClosed() will call updateNpcAbilitiesList() + save().
|
||||
if (editingChainEntry) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedAbilityIndex >= 0 && selectedAbilityIndex < npcSlots.size()) {
|
||||
AbilityAction slot = npcSlots.get(selectedAbilityIndex);
|
||||
if (slot.isReference()) {
|
||||
// Modify Parent on standalone reference — save to global preset
|
||||
PacketClient.sendClient(new CustomAbilitySavePacket(ability.writeNBT()));
|
||||
} else {
|
||||
// Inline ability — update slot data
|
||||
npcSlots.set(selectedAbilityIndex, AbilityAction.inline(ability));
|
||||
}
|
||||
updateNpcAbilitiesList();
|
||||
save();
|
||||
}
|
||||
// updateNpcAbilitiesList() + save() deferred to handleAbilityConfigClosed()
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -497,7 +497,6 @@ public class GuiNpcManageAbilities extends GuiAbilityInterface
|
||||
private void handleChainConfigClosed() {
|
||||
if (pendingSaveChain != null) {
|
||||
PacketClient.sendClient(new ChainedAbilitySavePacket(pendingSaveChain.writeNBT()));
|
||||
PacketClient.sendClient(new ChainedAbilitiesGetPacket());
|
||||
selected = pendingSaveChain.getName();
|
||||
pendingSaveChain = null;
|
||||
}
|
||||
@@ -570,7 +569,6 @@ public class GuiNpcManageAbilities extends GuiAbilityInterface
|
||||
return true;
|
||||
} else {
|
||||
PacketClient.sendClient(new CustomAbilitySavePacket(pendingSaveAbility.writeNBT()));
|
||||
PacketClient.sendClient(new CustomAbilitiesGetPacket());
|
||||
pendingSaveAbility = null;
|
||||
}
|
||||
} else if (pendingSaveAbility != null) {
|
||||
@@ -597,7 +595,6 @@ public class GuiNpcManageAbilities extends GuiAbilityInterface
|
||||
return true;
|
||||
}
|
||||
PacketClient.sendClient(new CustomAbilitySavePacket(pendingSaveAbility.writeNBT()));
|
||||
PacketClient.sendClient(new CustomAbilitiesGetPacket());
|
||||
pendingSaveAbility = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user