Skip to content

Commit

Permalink
Add 'rename' command. Usage: - rename (npc:n@...) [new_name]
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Jul 4, 2013
1 parent 618176c commit 9d95b41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Expand Up @@ -235,6 +235,9 @@ public void registerCoreMembers() {
registerCoreMember(RemoveCommand.class,
"REMOVE", "remove (type:<entity>/npcid:<#>) (region:<name>)", 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);

Expand Down
@@ -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);

}

}

0 comments on commit 9d95b41

Please sign in to comment.