/
CreateCommand.java
115 lines (99 loc) · 4.56 KB
/
CreateCommand.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.trait.Trait;
import org.bukkit.event.player.PlayerTeleportEvent;
public class CreateCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("entity_type")
&& arg.matchesArgumentType(dEntity.class)) {
// Avoid duplication of objects
dEntity ent = arg.asType(dEntity.class);
if (!ent.isGeneric() && !ent.isCitizensNPC()) {
throw new InvalidArgumentsException("Entity supplied must be generic or a Citizens NPC!");
}
scriptEntry.addObject("entity_type", ent);
}
else if (!scriptEntry.hasObject("spawn_location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("spawn_location", arg.asType(dLocation.class));
}
else if (!scriptEntry.hasObject("name")) {
scriptEntry.addObject("name", arg.asElement());
}
else if (!scriptEntry.hasObject("traits")
&& arg.matchesPrefix("t", "trait", "traits")) {
scriptEntry.addObject("traits", arg.asType(dList.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("name")) {
throw new InvalidArgumentsException("Must specify a name!");
}
if (!scriptEntry.hasObject("entity_type")) {
throw new InvalidArgumentsException("Must specify an entity type!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element name = (Element) scriptEntry.getObject("name");
dEntity type = (dEntity) scriptEntry.getObject("entity_type");
dLocation loc = (dLocation) scriptEntry.getObject("spawn_location");
dList traits = (dList) scriptEntry.getObject("traits");
dB.report(scriptEntry, getName(), name.debug() + type.debug() + (loc != null ? loc.debug() : "")
+ (traits != null ? traits.debug() : ""));
dNPC created;
if (!type.isGeneric() && type.isCitizensNPC()) {
created = new dNPC(type.getDenizenNPC().getCitizen().clone());
created.getCitizen().setName(name.asString());
}
else {
created = dNPC.mirrorCitizensNPC(CitizensAPI.getNPCRegistry()
.createNPC(type.getBukkitEntityType(), name.asString()));
}
// Add the created NPC into the script entry so it can be utilized if need be.
scriptEntry.addObject("created_npc", created);
if (created.isSpawned()) {
if (loc != null) {
created.getCitizen().teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
}
else {
created.getCitizen().despawn();
}
}
else {
if (loc != null) {
created.getCitizen().spawn(loc);
}
}
if (traits != null) {
for (String trait_name : traits) {
Trait trait = CitizensAPI.getTraitFactory().getTrait(trait_name);
if (trait != null) {
created.getCitizen().addTrait(trait);
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Could not add trait to NPC: " + trait_name);
}
}
}
for (Mechanism mechanism : type.getWaitingMechanisms()) {
created.adjust(mechanism);
}
}
}