diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index 85d0017d42..47d884b5f9 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -235,6 +235,9 @@ public void registerCoreMembers() { registerCoreMember(RemoveCommand.class, "REMOVE", "remove (type:/npcid:<#>) (region:)", 0); + registerCoreMember(RenameCommand.class, + "RENAME", "rename (npc:n@...) [new_name]", 1); + registerCoreMember(ResetCommand.class, "RESET", "reset [fails|finishes|cooldown] (script:script_name{attached script})", 1); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/RenameCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/RenameCommand.java new file mode 100644 index 0000000000..e0b1a634a2 --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/RenameCommand.java @@ -0,0 +1,56 @@ +package net.aufdemrand.denizen.scripts.commands.npc; + +import net.aufdemrand.denizen.exceptions.CommandExecutionException; +import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizen.objects.Element; +import net.aufdemrand.denizen.objects.aH; +import net.aufdemrand.denizen.scripts.ScriptEntry; +import net.aufdemrand.denizen.scripts.commands.AbstractCommand; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.citizensnpcs.api.event.DespawnReason; +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.Location; + +/** + * Renames a NPC. + * + * + */ + +public class RenameCommand extends AbstractCommand { + + @Override + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + + if (!scriptEntry.hasObject("name")) + scriptEntry.addObject("name", arg.asElement()); + + } + + if (!scriptEntry.hasObject("name")) + throw new InvalidArgumentsException("Must specify a name!"); + + if (scriptEntry.getNPC() == null || !scriptEntry.getNPC().isValid()) + throw new InvalidArgumentsException("Must have a NPC attached!"); + } + + @Override + public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException { + + Element name = (Element) scriptEntry.getObject("name"); + + dB.report(getName(), name.debug()); + + NPC npc = scriptEntry.getNPC().getCitizen(); + + Location prev = npc.isSpawned() ? npc.getBukkitEntity().getLocation() : null; + npc.despawn(DespawnReason.PENDING_RESPAWN); + npc.setName(name.asString()); + if (prev != null) + npc.spawn(prev); + + } + +} \ No newline at end of file