/
RemoveCommand.java
137 lines (112 loc) · 5.41 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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import java.util.List;
public class RemoveCommand extends AbstractCommand {
// <--[command]
// @Name Remove
// @Syntax remove [<entity>|...] (world:<world>)
// @Required 1
// @Short Despawns an entity or list of entities, permanently removing any NPCs.
// @Group entity
// @Description
// Removes the selected entity. May also take a list of entities to remove.
//
// Any NPC removed this way is completely removed, as if by '/npc remove'.
// For temporary NPC removal, see <@link command despawn>.
//
// If a generic entity name is given (like 'zombie'), this will remove all entities of that type from the given world.
// Optionally, you may specify a world to target.
// (Defaults to the world of the player running the command)
//
// @Tags
// <EntityTag.is_spawned>
//
// @Usage
// Use to remove the entity the player is looking at.
// - remove <player.target>
//
// @Usage
// Use to remove all nearby entities around the player, excluding the player itself.
// - remove <player.location.find.entities.within[10].exclude[<player>]>
//
// @Usage
// Use to remove all dropped items in the world called cookies.
// - remove dropped_item world:cookies
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(WorldTag.class)) {
scriptEntry.addObject("world", arg.asType(WorldTag.class));
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
// If the world has not been specified, try to use the NPC's or player's
// world, or default to the specified world in the server properties if necessary
scriptEntry.defaultObject("world",
(Utilities.entryHasNPC(scriptEntry) && Utilities.getEntryNPC(scriptEntry).isSpawned()) ? new WorldTag(Utilities.getEntryNPC(scriptEntry).getWorld()) : null,
(Utilities.entryHasPlayer(scriptEntry) && Utilities.getEntryPlayer(scriptEntry).isOnline()) ? new WorldTag(Utilities.getEntryPlayer(scriptEntry).getWorld()) : null,
new WorldTag(Bukkit.getWorlds().get(0)));
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
WorldTag world = (WorldTag) scriptEntry.getObject("world");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("entities", entities));
}
// Go through all of our entities and remove them
for (EntityTag entity : entities) {
// If this is a specific spawned entity, and all
// other applicable conditions are met, remove it
if (!entity.isGeneric()) {
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getCitizen().destroy();
}
else {
entity.remove();
}
}
// If this is a generic unspawned entity, remove
// all entities of this type from the world (as
// long as they meet all other conditions)
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 EntityTag, and all other applicable
// conditions are met, remove it
if (entity.getEntityType().equals(DenizenEntityType.getByEntity(worldEntity))) {
worldEntity.remove();
}
}
}
}
}
}