/
RemoveCommand.java
148 lines (114 loc) · 5.12 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
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
139
140
141
142
143
144
145
146
147
148
package net.aufdemrand.denizen.scripts.commands.entity;
import java.util.List;
import org.bukkit.entity.Entity;
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.dWorld;
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.debugging.dB.Messages;
import net.aufdemrand.denizen.utilities.depends.WorldGuardUtilities;
/**
* Delete certain entities or all entities of a type.
* Can permanently remove NPCs if used on them.
*
* @author David Cernat
*/
public class RemoveCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesPrefix("entity, entities, e")) {
// Entity arg
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
else if (!scriptEntry.hasObject("region")
&& arg.matchesPrefix("region, r")) {
// Location arg
scriptEntry.addObject("region", arg.asElement());
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
// add world
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
}
// Check to make sure required arguments have been filled
if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary
if (!scriptEntry.hasObject("world")) {
if ((scriptEntry.hasNPC())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getNPC().getWorld()));
}
else if ((scriptEntry.hasPlayer())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getPlayer().getWorld()));
}
else {
scriptEntry.addObject("world", dWorld.valueOf("world"));
}
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dWorld world = (dWorld) scriptEntry.getObject("world");
Element region = (Element) scriptEntry.getObject("region");
// Report to dB
dB.report(getName(), aH.debugObj("entities", entities.toString()) +
(region != null ? aH.debugObj("region", region) : ""));
boolean conditionsMet;
// Go through all of our entities and remove them
for (dEntity entity : entities) {
conditionsMet = true;
// If this is a specific spawned entity, and all
// other applicable conditions are met, remove it
if (entity.isGeneric() == false) {
if (region != null) {
conditionsMet = WorldGuardUtilities.inRegion
(entity.getBukkitEntity().getLocation(),
region.asString());
}
if (conditionsMet == true) {
if (entity.isNPC()) {
entity.getNPC().destroy();
}
else {
entity.remove();
}
}
}
// If this is a generic unspawned entity, remove
// all entities of this type from the world
else {
// Note: getting the entities from each loaded chunk
// in the world (like in Essentials' /killall) has the
// exact same effect as the below
for (Entity worldEntity : world.getEntities()) {
// If this entity from the world is of the same type
// as our current dEntity, and all other applicable
// conditions are met, remove it
if (entity.getEntityType().equals(worldEntity.getType())) {
if (region != null) {
conditionsMet = WorldGuardUtilities.inRegion
(worldEntity.getLocation(),
region.asString());
}
if (conditionsMet == true) {
worldEntity.remove();
}
}
}
}
}
}
}