Skip to content

Commit 9a6d9f1

Browse files
committed
Capture output of commands
1 parent b2a3043 commit 9a6d9f1

3 files changed

Lines changed: 378 additions & 18 deletions

File tree

src/main/java/com/shweit/serverapi/endpoints/v1/ServerAPI.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.shweit.serverapi.MinecraftServerAPI;
44
import com.shweit.serverapi.WebServer;
5+
import com.shweit.serverapi.handlers.BetterCommandExecutor;
56
import com.shweit.serverapi.handlers.CommandOutputCapture;
67
import com.shweit.serverapi.handlers.LogHandler;
78
import com.shweit.serverapi.listeners.ChatListener;
@@ -26,7 +27,10 @@
2627
import java.nio.file.FileStore;
2728
import java.nio.file.FileSystems;
2829
import java.nio.file.Files;
30+
import java.util.ArrayList;
2931
import java.util.Base64;
32+
import java.util.HashMap;
33+
import java.util.List;
3034
import java.util.Map;
3135
import java.util.Properties;
3236
import java.util.concurrent.atomic.AtomicBoolean;
@@ -227,11 +231,10 @@ public NanoHTTPD.Response execCommand(final Map<String, String> params) {
227231
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.BAD_REQUEST, "application/json", "{\"error\":\"Invalid Command.\"}");
228232
}
229233

230-
AtomicBoolean success = new AtomicBoolean(false);
231-
CommandOutputCapture outputCapture = new CommandOutputCapture();
234+
final BetterCommandExecutor.CommandResult[] result = new BetterCommandExecutor.CommandResult[1];
232235

233236
BukkitTask t1 = Bukkit.getScheduler().runTask(MinecraftServerAPI.getInstance(), () -> {
234-
success.set(Bukkit.getServer().dispatchCommand(outputCapture, command));
237+
result[0] = BetterCommandExecutor.executeCommand(command);
235238
});
236239

237240
while (Bukkit.getScheduler().isCurrentlyRunning(t1.getTaskId()) || Bukkit.getScheduler().isQueued(t1.getTaskId())) {
@@ -246,8 +249,13 @@ public NanoHTTPD.Response execCommand(final Map<String, String> params) {
246249
}
247250

248251
JSONObject jsonResponse = new JSONObject();
249-
jsonResponse.put("success", success.get());
250-
jsonResponse.put("output", outputCapture.getOutputMessages());
252+
if (result[0] != null) {
253+
jsonResponse.put("success", result[0].isSuccess());
254+
jsonResponse.put("output", result[0].getOutput());
255+
} else {
256+
jsonResponse.put("success", false);
257+
jsonResponse.put("output", new ArrayList<>());
258+
}
251259

252260
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/json", jsonResponse.toString());
253261
}
@@ -346,11 +354,10 @@ public NanoHTTPD.Response execMultipleCommands(final Map<String, String> params)
346354
for (int i = 0; i < commands.length(); i++) {
347355
String command = commands.getString(i);
348356

349-
AtomicBoolean success = new AtomicBoolean(false);
350-
CommandOutputCapture outputCapture = new CommandOutputCapture();
357+
final BetterCommandExecutor.CommandResult[] result = new BetterCommandExecutor.CommandResult[1];
351358

352359
BukkitTask task = Bukkit.getScheduler().runTask(MinecraftServerAPI.getInstance(), () -> {
353-
success.set(Bukkit.getServer().dispatchCommand(outputCapture, command));
360+
result[0] = BetterCommandExecutor.executeCommand(command);
354361
});
355362

356363
while (Bukkit.getScheduler().isCurrentlyRunning(task.getTaskId()) ||
@@ -364,11 +371,13 @@ public NanoHTTPD.Response execMultipleCommands(final Map<String, String> params)
364371
}
365372
}
366373

367-
JSONObject commandResult = new JSONObject();
368-
commandResult.put("command", command);
369-
commandResult.put("success", success.get());
370-
commandResult.put("output", outputCapture.getOutputMessages());
371-
results.put(commandResult);
374+
if (result[0] != null) {
375+
JSONObject commandResult = new JSONObject();
376+
commandResult.put("command", result[0].getCommand());
377+
commandResult.put("success", result[0].isSuccess());
378+
commandResult.put("output", result[0].getOutput());
379+
results.put(commandResult);
380+
}
372381
}
373382

374383
JSONObject response = new JSONObject();

0 commit comments

Comments
 (0)