/
ExecuteCommand.java
224 lines (216 loc) · 11 KB
/
ExecuteCommand.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.denizenscript.denizen.scripts.commands.server;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.command.scripted.DenizenCommandSender;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.PlayerHelper;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ExecuteCommand extends AbstractCommand {
public ExecuteCommand() {
setName("execute");
setSyntax("execute [as_player/as_op/as_npc/as_server] [<Bukkit-command>] (silent)");
setRequiredArguments(2, 3);
isProcedural = false;
}
// <--[command]
// @Name Execute
// @Syntax execute [as_player/as_op/as_npc/as_server] [<Bukkit-command>] (silent)
// @Required 2
// @Maximum 3
// @Short Executes an arbitrary server command as if the player, NPC, or server typed it in.
// @Group server
//
// @Description
// Allows the execution of server commands through a Denizen script.
// Commands can be executed as the server, as an npc, as an opped player, or as a player, as though it was typed by the respective source.
//
// Note that you should generally avoid using 'as_op', which is only meant for very specific special cases. 'as_server' is usually a better option.
//
// Note: do not include the slash at the start. A slash at the start will be interpreted equivalent to typing two slashes at the front in-game.
//
// Note that this is a Denizen script command that executes Bukkit commands.
// This can be considered the inverse of '/ex' (a Bukkit command that executes Denizen script commands).
//
// The 'silent' option can be specified with 'as_server' to hide the output. Note that 'silent' might or might not work with different plugins depending on how they operate.
//
// Generally, you should never use this to execute a vanilla command, there is almost always a script command that should be used instead.
// Usually the 'execute' command should be reserved for interacting with external plugins.
//
// @Tags
// <entry[saveName].output> returns the output to an as_server sender.
//
// @Usage
// Use to execute the save-all command as the server.
// - execute as_server "save-all"
//
// @Usage
// Use to make the linked (non-op) player execute a command that normally only ops can use.
// - execute as_op "staffsay hi"
// -->
enum Type {AS_SERVER, AS_NPC, AS_PLAYER, AS_OP}
public DenizenCommandSender dcs = new DenizenCommandSender();
public static final List<UUID> silencedPlayers = new ArrayList<>();
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matches("asplayer", "as_player")
&& !scriptEntry.hasObject("type")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must have a Player link when using AS_PLAYER.");
}
scriptEntry.addObject("type", new ElementTag("AS_PLAYER"));
}
else if (arg.matches("asop", "as_op")
&& !scriptEntry.hasObject("type")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must have a Player link when using AS_OP.");
}
scriptEntry.addObject("type", new ElementTag("AS_OP"));
}
else if (arg.matches("asnpc", "as_npc")
&& !scriptEntry.hasObject("type")) {
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("Must have a NPC link when using AS_NPC.");
}
scriptEntry.addObject("type", new ElementTag("AS_NPC"));
}
else if (arg.matches("asserver", "as_server")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", new ElementTag("AS_SERVER"));
}
else if (!scriptEntry.hasObject("silent")
&& arg.matches("silent")) {
scriptEntry.addObject("silent", new ElementTag("true"));
}
else if (!scriptEntry.hasObject("command")) {
scriptEntry.addObject("command", new ElementTag(arg.getRawValue()));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("type")) {
throw new InvalidArgumentsException("Missing execution type!");
}
if (!scriptEntry.hasObject("command")) {
throw new InvalidArgumentsException("Missing command text!");
}
scriptEntry.defaultObject("silent", new ElementTag("false"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag cmd = scriptEntry.getElement("command");
ElementTag type = scriptEntry.getElement("type");
ElementTag silent = scriptEntry.getElement("silent");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
type.debug()
+ cmd.debug()
+ silent.debug());
}
String command = cmd.asString();
switch (Type.valueOf(type.asString())) {
case AS_PLAYER:
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(Utilities.getEntryPlayer(scriptEntry).getPlayerEntity(), "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
boolean silentBool = silent.asBoolean();
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
if (silentBool) {
NetworkInterceptHelper.enable();
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ?
pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silentBool) {
silencedPlayers.remove(player.getUniqueId());
}
}
}
catch (Throwable e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as player.");
Debug.echoError(scriptEntry.getResidingQueue(), e);
}
break;
case AS_OP:
if (CoreUtilities.equalsIgnoreCase(command, "stop")) {
Debug.echoError("Please use as_server to execute 'stop'.");
return;
}
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
PlayerHelper playerHelper = NMSHandler.getPlayerHelper();
boolean isOp = player.isOp();
if (!isOp) {
playerHelper.setTemporaryOp(player, true);
}
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
boolean silentBool = silent.asBoolean();
if (silentBool) {
NetworkInterceptHelper.enable();
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ?
pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silentBool) {
silencedPlayers.remove(player.getUniqueId());
}
}
}
catch (Throwable e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as OP.");
Debug.echoError(scriptEntry.getResidingQueue(), e);
}
if (!isOp) {
playerHelper.setTemporaryOp(player, false);
}
break;
case AS_NPC:
if (!Utilities.getEntryNPC(scriptEntry).isSpawned()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
return;
}
if (Utilities.getEntryNPC(scriptEntry).getEntity().getType() != EntityType.PLAYER) {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
return;
}
Utilities.getEntryNPC(scriptEntry).getEntity().setOp(true);
try {
((Player) Utilities.getEntryNPC(scriptEntry).getEntity()).performCommand(command);
}
catch (Throwable e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as NPC-OP.");
Debug.echoError(scriptEntry.getResidingQueue(), e);
}
Utilities.getEntryNPC(scriptEntry).getEntity().setOp(false);
break;
case AS_SERVER:
dcs.clearOutput();
dcs.silent = silent.asBoolean();
ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
Bukkit.getPluginManager().callEvent(sce);
Denizen.getInstance().getServer().dispatchCommand(dcs, sce.getCommand());
scriptEntry.addObject("output", new ListTag(dcs.getOutput()));
break;
}
}
}