Code cleanup

moved threading to parent scope
added issues link to mod config
This commit is contained in:
Jonas_Jones 2022-07-26 09:13:44 +02:00
parent 8efeb1cc5b
commit 643f9497ea
3 changed files with 33 additions and 32 deletions

View file

@ -76,7 +76,8 @@ public class RunCommand {
}
}
public static int runCommand(String cmd, CommandContext<ServerCommandSource> context) {
return ShellCommand.execute(cmd, context);
new Thread(() -> {ShellCommand.execute(cmd, context);}).start();
return 1;
}
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> context) {
String consoleLog = " [" + cmd + "]: " + commandFeedback;

View file

@ -11,39 +11,38 @@ import java.io.InputStreamReader;
public class ShellCommand {
public static int execute(String command, CommandContext<ServerCommandSource> context) {
new Thread(() -> {
ProcessBuilder processBuilder = new ProcessBuilder();
if (ConsoleMC.ISWINDOWS) {
processBuilder.command("cmd.exe", "/c", command);
ProcessBuilder processBuilder = new ProcessBuilder();
if (ConsoleMC.ISWINDOWS) {
processBuilder.command("cmd.exe", "/c", command);
} else {
processBuilder.command("bash", "-c", command);
}
try {
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
RunCommand.returnCommandOutput(command, line, context);
}
int exitCode = process.waitFor();
if (exitCode == 0) {
ConsoleMC.LOGGER.info("Exited with error code : " + exitCode);
} else {
processBuilder.command("bash", "-c", command);
ConsoleMC.LOGGER.error("Exited with error code : " + exitCode);
}
try {
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
RunCommand.returnCommandOutput(command, line, context);
}
int exitCode = process.waitFor();
if (exitCode == 0) {
ConsoleMC.LOGGER.info("Exited with error code : " + exitCode);
} else {
ConsoleMC.LOGGER.error("Exited with error code : " + exitCode);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}
}

View file

@ -10,7 +10,8 @@
],
"contact": {
"homepage": "http://jonasjones.me/consolemc",
"sources": "https://github.com/J-onasJones/ConsoleMC"
"sources": "https://github.com/J-onasJones/ConsoleMC",
"issues": "https://github.com/J-onasJones/ConsoleMC/issues"
},
"license": "CC0-1.0",