/
DespawnCommand.java
91 lines (78 loc) · 2.98 KB
/
DespawnCommand.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizen.objects.dNPC;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.trait.trait.Spawned;
import java.util.Arrays;
import java.util.List;
public class DespawnCommand extends AbstractCommand {
// <--[command]
// @Name Despawn
// @Syntax despawn (<npc>|...)
// @Plugin Citizens
// @Required 0
// @Short Temporarily despawns the linked NPC or a list of NPCs.
// @Group npc
//
// @Description
// This command will temporarily despawn either the linked NPC or
// a list of other NPCs. Despawning means they are no longer visible
// or interactable, but they still exist and can be respawned.
//
// @Tags
// <n@npc.is_spawned>
//
// @Usage
// Use to despawn the linked NPC.
// - despawn
//
// @Usage
// Use to despawn several NPCs.
// - despawn <npc>|<player.selected_npc>|n@32
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("npcs")
&& arg.matchesArgumentList(dNPC.class)) {
scriptEntry.addObject("npcs", arg.asType(dList.class).filter(dNPC.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("npcs")) {
if (Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("npcs", Arrays.asList(Utilities.getEntryNPC(scriptEntry)));
}
else {
throw new InvalidArgumentsException("Must specify a valid list of NPCs!");
}
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {
// Get objects
List<dNPC> npcs = (List<dNPC>) scriptEntry.getObject("npcs");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
aH.debugObj("NPCs", npcs.toString()));
}
for (dNPC npc : npcs) {
if (npc.isSpawned()) {
if (npc.getCitizen().hasTrait(Spawned.class)) {
npc.getCitizen().getTrait(Spawned.class).setSpawned(false);
}
npc.getCitizen().despawn(DespawnReason.PLUGIN);
}
}
}
}