diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 6efcf33..3508753 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -296,6 +296,7 @@ public class ZNpcsPlus { .addSubcommand("setlocation", new SetLocationCommand(npcRegistry)) .addSubcommand("lookatme", new LookAtMeCommand(npcRegistry)) .addSubcommand("setrotation", new SetRotationCommand(npcRegistry)) + .addSubcommand("changeid", new ChangeIdCommand(npcRegistry)) .addSubcommand("property", new MultiCommand(bootstrap.loadHelpMessage("property")) .addSubcommand("set", new PropertySetCommand(npcRegistry)) .addSubcommand("remove", new PropertyRemoveCommand(npcRegistry))) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ChangeIdCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ChangeIdCommand.java new file mode 100644 index 0000000..1d64db6 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ChangeIdCommand.java @@ -0,0 +1,36 @@ +package lol.pyr.znpcsplus.commands; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.command.CommandHandler; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Collections; +import java.util.List; + +public class ChangeIdCommand implements CommandHandler { + private final NpcRegistryImpl npcRegistry; + + public ChangeIdCommand(NpcRegistryImpl npcRegistry) { + this.npcRegistry = npcRegistry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " changeid "); + NpcEntryImpl old = context.parse(NpcEntryImpl.class); + String newId = context.popString(); + if (npcRegistry.getById(newId) != null) context.halt(Component.text("There is already an npc with the new id you have provided", NamedTextColor.RED)); + npcRegistry.switchIds(old.getId(), newId); + context.send(Component.text("Npc's id changed to " + newId.toLowerCase(), NamedTextColor.GREEN)); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); + return Collections.emptyList(); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java index 1220234..3b760e6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java @@ -161,6 +161,16 @@ public class NpcRegistryImpl implements NpcRegistry { storage.deleteNpc(entry); } + public void switchIds(String oldId, String newId) { + NpcEntryImpl entry = getById(oldId); + delete(oldId); + NpcEntryImpl newEntry = new NpcEntryImpl(newId, entry.getNpc()); + newEntry.setSave(entry.isSave()); + newEntry.setProcessed(entry.isProcessed()); + newEntry.setAllowCommandModification(entry.isAllowCommandModification()); + register(newEntry); + } + public void unload() { npcList.forEach(npcEntry -> npcEntry.getNpc().delete()); } diff --git a/plugin/src/main/resources/help-messages/root.txt b/plugin/src/main/resources/help-messages/root.txt index 70ff31f..da9cfcb 100644 --- a/plugin/src/main/resources/help-messages/root.txt +++ b/plugin/src/main/resources/help-messages/root.txt @@ -4,6 +4,7 @@ * /npc create * /npc delete + * /npc changeid * /npc toggle * /npc list * /npc type