/
RemoveCommand.java
89 lines (68 loc) · 2.75 KB
/
RemoveCommand.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
package net.aufdemrand.denizen.scripts.commands.entity;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.WorldGuardUtilities;
/**
* Safely removes an NPC.
*
* To be expanded to use multiple NPCs and possibly other entities as well.
*
*/
public class RemoveCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
EntityType entityType = null;
String region = null;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesEntityType(arg)) {
entityType = aH.getEntityTypeFrom(arg);
dB.echoDebug("...will remove all '%s'.", arg);
}
else if (aH.matchesValueArg("region", arg, ArgumentType.String)) {
region = aH.getStringFrom(arg);
dB.echoDebug("...in region " + region);
}
}
scriptEntry.addObject("entityType", entityType);
scriptEntry.addObject("region", region);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
EntityType entityType = (EntityType) scriptEntry.getObject("entityType");
String region = (String) scriptEntry.getObject("region");
// If no entity type or region chosen, remove this NPC
if (entityType == null && region == null) {
scriptEntry.getNPC().getCitizen().destroy();
dB.echoDebug("...have removed NPC '%s'.", String.valueOf(scriptEntry.getNPC().getCitizen().getId()));
}
// Else, remove regular entities
else {
for (World world: Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (region != null) {
if (WorldGuardUtilities.checkWGRegion(entity.getLocation(), region)) {
if (entityType == null) {
entity.remove();
}
else if (entity.getType().equals(entityType)) {
entity.remove();
}
}
}
else if (entity.getType().equals(entityType)) {
entity.remove();
}
}
}
}
}
}