Fix Ability Cloning Behavior for Scripts

This commit is contained in:
Kam committed 2026-03-10 21:51:12 -04:00
1 parent 7b8ad7c6b5
commit 9b0c364e13
5 files changed
+108 -2

No files matched your search

@@ -101,6 +101,7 @@ import kamkeel.npcs.network.packets.request.category.CategoryListRequestPacket;
import kamkeel.npcs.network.packets.request.category.CategoryMoveItemPacket;
import kamkeel.npcs.network.packets.request.category.CategoryRemovePacket;
import kamkeel.npcs.network.packets.request.category.CategorySavePacket;
import kamkeel.npcs.network.packets.request.ability.CopyAbilityScriptsPacket;
import kamkeel.npcs.network.packets.request.ability.CustomAbilityClonePacket;
import kamkeel.npcs.network.packets.request.ability.CustomAbilityGetPacket;
import kamkeel.npcs.network.packets.request.ability.CustomAbilityRemovePacket;
@@ -541,6 +542,7 @@ public class PacketHandler {
REQUEST_PACKET.registerPacket(new ChainedAbilityRemovePacket());
REQUEST_PACKET.registerPacket(new CustomAbilityClonePacket());
REQUEST_PACKET.registerPacket(new ChainedAbilityClonePacket());
REQUEST_PACKET.registerPacket(new CopyAbilityScriptsPacket());
// Category Packets
REQUEST_PACKET.registerPacket(new CategorySavePacket());
@@ -226,6 +226,7 @@ public enum EnumRequestPacket {
CustomAbilityClone,
ChainedAbilityClone,
CopyAbilityScripts,
LinkedItemClone,
EffectClone,
}
@@ -0,0 +1,90 @@
package kamkeel.npcs.network.packets.request.ability;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.controllers.AbilityController;
import kamkeel.npcs.controllers.data.ability.Ability;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.PacketUtil;
import kamkeel.npcs.network.enums.EnumItemPacketType;
import kamkeel.npcs.network.enums.EnumRequestPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import noppes.npcs.CustomNpcsPermissions;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.data.AbilityScript;
import java.io.IOException;
/**
* Copies script data from a source ability to a target ability UUID.
* Used when cloning an ability into an NPC as inline - the clone gets a new UUID
* but needs the source's script handler copied server-side (scripts are never sent to client).
*/
public final class CopyAbilityScriptsPacket extends AbstractPacket {
public static String packetName = "Request|CopyAbilityScripts";
private String sourceAbilityName;
private String targetAbilityId;
public CopyAbilityScriptsPacket() {
}
public CopyAbilityScriptsPacket(String sourceAbilityName, String targetAbilityId) {
this.sourceAbilityName = sourceAbilityName;
this.targetAbilityId = targetAbilityId;
}
@Override
public Enum getType() {
return EnumRequestPacket.CopyAbilityScripts;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.REQUEST_PACKET;
}
@Override
public CustomNpcsPermissions.Permission getPermission() {
return CustomNpcsPermissions.GLOBAL_ABILITY;
}
@SideOnly(Side.CLIENT)
@Override
public void sendData(ByteBuf out) throws IOException {
ByteBufUtils.writeString(out, sourceAbilityName);
ByteBufUtils.writeString(out, targetAbilityId);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
if (!(player instanceof EntityPlayerMP))
return;
if (!PacketUtil.verifyItemPacket(packetName, EnumItemPacketType.WAND, player))
return;
String sourceName = ByteBufUtils.readString(in);
String targetId = ByteBufUtils.readString(in);
if (targetId == null || targetId.isEmpty()) return;
// Look up source ability's script handler
Ability source = AbilityController.Instance.getCustomAbility(sourceName);
if (source == null) return;
AbilityScript sourceHandler = source.getScriptHandler();
if (sourceHandler == null) return;
// Clone the script handler for the target UUID via NBT round-trip
NBTTagCompound scriptNbt = sourceHandler.writeToNBT(new NBTTagCompound());
AbilityScript targetHandler = new AbilityScript(targetId);
targetHandler.readFromNBT(scriptNbt);
AbilityController.Instance.abilityScriptHandlers.put(targetId, targetHandler);
}
}
@@ -7,6 +7,7 @@ import kamkeel.npcs.controllers.data.ability.AbilityVariant;
import kamkeel.npcs.controllers.data.ability.data.ChainedAbility;
import kamkeel.npcs.controllers.data.ability.data.entry.ChainedAbilityEntry;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.packets.request.ability.CopyAbilityScriptsPacket;
import kamkeel.npcs.network.packets.request.ability.AbilitiesGetAllPacket;
import kamkeel.npcs.network.packets.request.ability.AbilitiesNpcGetPacket;
import kamkeel.npcs.network.packets.request.ability.AbilitiesNpcSavePacket;
@@ -816,6 +817,14 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
* Adds as an inline slot.
*/
public void loadAbility(Ability loadedAbility) {
loadAbility(loadedAbility, null);
}
/**
* Called from SubGuiAbilityLoad when a cloned ability is loaded with a known source.
* Copies scripts server-side from source to the new inline ability.
*/
public void loadAbility(Ability loadedAbility, String sourceAbilityName) {
if (loadedAbility != null) {
loadedAbility.setId(UUID.randomUUID().toString());
npcSlots.add(AbilityAction.inline(loadedAbility));
@@ -825,6 +834,10 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
selectAbilityByIndex(selectedAbilityIndex);
initGui();
save();
// Copy scripts from source ability server-side (scripts are never sent to client)
if (sourceAbilityName != null && !sourceAbilityName.isEmpty()) {
PacketClient.sendClient(new CopyAbilityScriptsPacket(sourceAbilityName, loadedAbility.getId()));
}
}
}
@@ -1046,7 +1059,7 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
if (slot.convertToInline()) {
Ability ability = slot.getAbility();
if (ability != null && !ability.isBuiltIn()) {
ability.setId(UUID.randomUUID().toString());
// UUID already generated by convertToInline() - don't reassign or script handler is orphaned
ability.setNpcInlineEdit(true);
setSubGui(ability.createConfigGui(this));
}
@@ -216,7 +216,7 @@ public class SubGuiAbilityLoad extends SubGuiInterface implements ICustomScrollL
if (parentConfig != null) {
parentConfig.loadAbility(loaded);
} else if (parentAbilities != null) {
parentAbilities.loadAbility(loaded);
parentAbilities.loadAbility(loaded, selectedName);
}
}
close();