Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'rename' command. Usage: - rename (npc:n@...) [new_name]
- Loading branch information
1 parent
618176c
commit 9d95b41
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/net/aufdemrand/denizen/scripts/commands/npc/RenameCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
|
||
} |