/
RenameCommand.java
56 lines (40 loc) · 1.77 KB
/
RenameCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 an 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(scriptEntry, 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);
}
}