-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
SpawnCommand.java
138 lines (102 loc) · 5.29 KB
/
SpawnCommand.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package net.aufdemrand.denizen.scripts.commands.entity;
import java.util.List;
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.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.Location;
/**
* Spawn entities at a location. If no location is chosen,
* the entities are spawned at the NPC or player's location.
*
* @author David Cernat
*/
public class SpawnCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentType(dEntity.class)
&& arg.matchesPrefix("target")) {
scriptEntry.addObject("target", arg.asType(dEntity.class));
}
else if (!scriptEntry.hasObject("spread")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("spread", arg.asElement());
}
else if (!scriptEntry.hasObject("persistent")
&& arg.matches("persistent")) {
scriptEntry.addObject("persistent", "");
}
else dB.echoError(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}
// Use the NPC or player's locations as the location if one is not specified
scriptEntry.defaultObject("location",
scriptEntry.hasNPC() ? scriptEntry.getNPC().getLocation() : null,
scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getLocation() : null);
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("entities"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dLocation location = (dLocation) scriptEntry.getObject("location");
dEntity target = (dEntity) scriptEntry.getObject("target");
Element spread = scriptEntry.getElement("spread");
boolean persistent = scriptEntry.hasObject("persistent");
// Report to dB
dB.report(getName(), aH.debugObj("entities", entities.toString()) +
location.debug() +
(spread != null ?spread.debug() : "") +
(target != null ? target.debug() : "") +
(persistent == true ? aH.debugObj("persistent", persistent) : ""));
// Keep a dList of entities that can be called using <entry[name].spawned_entities>
// later in the script queue
dList entityList = new dList();
// Go through all the entities and spawn them or teleport them,
// then set their targets if applicable
for (dEntity entity : entities) {
Location loc = location.clone();
if (spread != null) {
loc.add(Utilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt(),
0,
Utilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt());
}
entity.spawnAt(loc);
// Only add to entityList after the entities have been
// spawned, otherwise you'll get something like "e@skeleton"
// instead of "e@57" on it
entityList.add(entity.toString());
if (persistent && entity.isLivingEntity()) {
entity.getLivingEntity().setRemoveWhenFarAway(false);
}
if (target != null && target.isLivingEntity()) {
entity.target(target.getLivingEntity());
}
}
// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("spawned_entities", entityList);
}
}