mirror of
https://github.com/JonasunderscoreJones/ConsoleMC.git
synced 2025-10-23 02:39:17 +02:00
Implemented new command execution method
This commit is contained in:
parent
09d4128df5
commit
8efeb1cc5b
11 changed files with 57 additions and 24 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -8,9 +8,6 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class ConsoleMC implements ModInitializer {
|
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 final Logger LOGGER = LoggerFactory.getLogger("consolemc");
|
||||||
public static String MOD_ID = "consolemc";
|
public static String MOD_ID = "consolemc";
|
||||||
|
|
||||||
|
@ -18,9 +15,15 @@ public class ConsoleMC implements ModInitializer {
|
||||||
CommandRegistrationCallback.EVENT.register(RunCommand::register);
|
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
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
ModConfigs.registerConfigs();
|
ModConfigs.registerConfigs();
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
LOGGER.info("ConsoleMC initialized!");
|
||||||
|
LOGGER.info("Server running on " + OS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package me.jonasjones.consolemc.command;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import me.jonasjones.consolemc.ConsoleMC;
|
import me.jonasjones.consolemc.ConsoleMC;
|
||||||
|
import me.jonasjones.consolemc.system.ShellCommand;
|
||||||
import net.minecraft.command.CommandRegistryAccess;
|
import net.minecraft.command.CommandRegistryAccess;
|
||||||
import net.minecraft.command.argument.EntityArgumentType;
|
import net.minecraft.command.argument.EntityArgumentType;
|
||||||
import net.minecraft.command.argument.MessageArgumentType;
|
import net.minecraft.command.argument.MessageArgumentType;
|
||||||
|
@ -75,27 +76,7 @@ public class RunCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static int runCommand(String cmd, CommandContext<ServerCommandSource> context) {
|
public static int runCommand(String cmd, CommandContext<ServerCommandSource> context) {
|
||||||
Process process = null;
|
return ShellCommand.execute(cmd, context);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> context) {
|
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> context) {
|
||||||
String consoleLog = " [" + cmd + "]: " + commandFeedback;
|
String consoleLog = " [" + cmd + "]: " + commandFeedback;
|
||||||
|
|
|
@ -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<ServerCommandSource> 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;
|
||||||
|
}
|
||||||
|
}
|
BIN
src/main/resources/assets/consolemc/icon-large.png
Normal file
BIN
src/main/resources/assets/consolemc/icon-large.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
BIN
src/main/resources/assets/consolemc/icon-medium.png
Normal file
BIN
src/main/resources/assets/consolemc/icon-medium.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 605 B |
Binary file not shown.
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 173 B |
Binary file not shown.
Before Width: | Height: | Size: 453 B |
Loading…
Add table
Add a link
Reference in a new issue