/
SpawnCommand.java
65 lines (49 loc) · 2.39 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
/**
* Spawn entities at a location.
* If no location is chosen, the entities are spawned at the NPC's location.
*
* @author David Cernat
*/
public class SpawnCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize necessary fields
EntityType entityType = null;
//Integer qty = null;
dLocation location = null;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesEntityType(arg)) {
entityType = aH.getEntityFrom(arg);
dB.echoDebug("...entity set to '%s'.", arg);
} else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
if (entityType == null) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ENTITY);
// Stash objects
scriptEntry.addObject("entityType", entityType);
scriptEntry.addObject("location", location);
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
final dLocation location = scriptEntry.hasObject("location") ?
(dLocation) scriptEntry.getObject("location") :
(dLocation) scriptEntry.getNPC().getLocation();
EntityType entityType = (EntityType) scriptEntry.getObject("entityType");
final Entity entity = location.getWorld().spawnEntity(
location, entityType);
}
}