diff --git a/production/ConsoleMC.main/me/jonasjones/consolemc/ConsoleMC.class b/production/ConsoleMC.main/me/jonasjones/consolemc/ConsoleMC.class index 28ecc87..7958a5e 100644 Binary files a/production/ConsoleMC.main/me/jonasjones/consolemc/ConsoleMC.class and b/production/ConsoleMC.main/me/jonasjones/consolemc/ConsoleMC.class differ diff --git a/production/ConsoleMC.main/me/jonasjones/consolemc/command/RunCommand.class b/production/ConsoleMC.main/me/jonasjones/consolemc/command/RunCommand.class index a96c267..d6e1646 100644 Binary files a/production/ConsoleMC.main/me/jonasjones/consolemc/command/RunCommand.class and b/production/ConsoleMC.main/me/jonasjones/consolemc/command/RunCommand.class differ diff --git a/production/ConsoleMC.main/me/jonasjones/consolemc/config/ModConfigs.class b/production/ConsoleMC.main/me/jonasjones/consolemc/config/ModConfigs.class index 69de50a..1f5c105 100644 Binary files a/production/ConsoleMC.main/me/jonasjones/consolemc/config/ModConfigs.class and b/production/ConsoleMC.main/me/jonasjones/consolemc/config/ModConfigs.class differ diff --git a/production/ConsoleMC.main/me/jonasjones/consolemc/system/ShellCommand.class b/production/ConsoleMC.main/me/jonasjones/consolemc/system/ShellCommand.class new file mode 100644 index 0000000..a25c7f1 Binary files /dev/null and b/production/ConsoleMC.main/me/jonasjones/consolemc/system/ShellCommand.class differ diff --git a/src/main/java/me/jonasjones/consolemc/ConsoleMC.java b/src/main/java/me/jonasjones/consolemc/ConsoleMC.java index a54297c..b1fa63a 100644 --- a/src/main/java/me/jonasjones/consolemc/ConsoleMC.java +++ b/src/main/java/me/jonasjones/consolemc/ConsoleMC.java @@ -8,9 +8,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ConsoleMC implements ModInitializer { - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("consolemc"); public static String MOD_ID = "consolemc"; @@ -18,9 +15,15 @@ public class ConsoleMC implements ModInitializer { CommandRegistrationCallback.EVENT.register(RunCommand::register); } + //find out if operating system is windows + public static boolean ISWINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows"); + public static String OS = System.getProperty("os.name"); + @Override public void onInitialize() { ModConfigs.registerConfigs(); registerCommands(); + LOGGER.info("ConsoleMC initialized!"); + LOGGER.info("Server running on " + OS); } } diff --git a/src/main/java/me/jonasjones/consolemc/command/RunCommand.java b/src/main/java/me/jonasjones/consolemc/command/RunCommand.java index 5f3462a..9e480ba 100644 --- a/src/main/java/me/jonasjones/consolemc/command/RunCommand.java +++ b/src/main/java/me/jonasjones/consolemc/command/RunCommand.java @@ -3,6 +3,7 @@ package me.jonasjones.consolemc.command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import me.jonasjones.consolemc.ConsoleMC; +import me.jonasjones.consolemc.system.ShellCommand; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.MessageArgumentType; @@ -75,27 +76,7 @@ public class RunCommand { } } public static int runCommand(String cmd, CommandContext context) { - Process process = null; - try { - process = Runtime.getRuntime().exec(cmd); - - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - String s = null; - while ((s = stdInput.readLine()) != null) { - returnCommandOutput(cmd, s, context); - } - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - while ((s = stdError.readLine()) != null) { - returnCommandOutput(cmd, s, context); - } - return 1; - } catch (IOException e) { - //e.printStackTrace(); - String errorLog = "Error: \"" + cmd + "\", No such file or directory!"; - ConsoleMC.LOGGER.info(errorLog); - broadcastToOP(errorLog, context); - return -1; - } + return ShellCommand.execute(cmd, context); } 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 new file mode 100644 index 0000000..ea90424 --- /dev/null +++ b/src/main/java/me/jonasjones/consolemc/system/ShellCommand.java @@ -0,0 +1,49 @@ +package me.jonasjones.consolemc.system; + +import com.mojang.brigadier.context.CommandContext; +import me.jonasjones.consolemc.ConsoleMC; +import me.jonasjones.consolemc.command.RunCommand; +import net.minecraft.server.command.ServerCommandSource; + +import java.io.BufferedReader; +import java.io.IOException; +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); + } 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 { + ConsoleMC.LOGGER.error("Exited with error code : " + exitCode); + } + + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + return 1; + } +} diff --git a/src/main/resources/assets/consolemc/icon-large.png b/src/main/resources/assets/consolemc/icon-large.png new file mode 100644 index 0000000..6c68202 Binary files /dev/null and b/src/main/resources/assets/consolemc/icon-large.png differ diff --git a/src/main/resources/assets/consolemc/icon-medium.png b/src/main/resources/assets/consolemc/icon-medium.png new file mode 100644 index 0000000..6c8ace2 Binary files /dev/null and b/src/main/resources/assets/consolemc/icon-medium.png differ diff --git a/src/main/resources/assets/consolemc/icon.png b/src/main/resources/assets/consolemc/icon.png index 41a0a24..e01cf33 100644 Binary files a/src/main/resources/assets/consolemc/icon.png and b/src/main/resources/assets/consolemc/icon.png differ diff --git a/src/main/resources/assets/consolemc/icon.png~ b/src/main/resources/assets/consolemc/icon.png~ deleted file mode 100644 index 047b91f..0000000 Binary files a/src/main/resources/assets/consolemc/icon.png~ and /dev/null differ