From 643f9497ea9382d091cee1e9257e4933f05b87ca Mon Sep 17 00:00:00 2001 From: Jonas_Jones <91549607+J-onasJones@users.noreply.github.com> Date: Tue, 26 Jul 2022 09:13:44 +0200 Subject: [PATCH] Code cleanup moved threading to parent scope added issues link to mod config --- .../consolemc/command/RunCommand.java | 3 +- .../consolemc/system/ShellCommand.java | 59 +++++++++---------- src/main/resources/fabric.mod.json | 3 +- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/main/java/me/jonasjones/consolemc/command/RunCommand.java b/src/main/java/me/jonasjones/consolemc/command/RunCommand.java index 9e480ba..cf40aff 100644 --- a/src/main/java/me/jonasjones/consolemc/command/RunCommand.java +++ b/src/main/java/me/jonasjones/consolemc/command/RunCommand.java @@ -76,7 +76,8 @@ public class RunCommand { } } public static int runCommand(String cmd, CommandContext context) { - return ShellCommand.execute(cmd, context); + new Thread(() -> {ShellCommand.execute(cmd, context);}).start(); + return 1; } public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext context) { String consoleLog = " [" + cmd + "]: " + commandFeedback; diff --git a/src/main/java/me/jonasjones/consolemc/system/ShellCommand.java b/src/main/java/me/jonasjones/consolemc/system/ShellCommand.java index ea90424..78e65ba 100644 --- a/src/main/java/me/jonasjones/consolemc/system/ShellCommand.java +++ b/src/main/java/me/jonasjones/consolemc/system/ShellCommand.java @@ -11,39 +11,38 @@ import java.io.InputStreamReader; public class ShellCommand { public static int execute(String command, CommandContext 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; } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index d603c36..6bc086c 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -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",