Added Basic Functionality

This commit is contained in:
J-onasJones 2022-06-12 00:06:06 +02:00
parent a1444e4276
commit c215bc41dd
27 changed files with 844 additions and 0 deletions

View file

@ -0,0 +1,63 @@
package me.jonasjones.consolemc.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.jonasjones.consolemc.ConsoleMC;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.MessageArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RunCommand {
public static void register(CommandDispatcher<ServerCommandSource> serverCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
serverCommandSourceCommandDispatcher.register(CommandManager.literal("cmd")
.requires(source -> source.hasPermissionLevel(4)) //requires OP
.then(CommandManager.argument("Console Command", MessageArgumentType.message())
.executes((ctx -> {
Text broadcastText = Text.of("Ran Console Command \"" + MessageArgumentType.getMessage(ctx, "Console Command").getString() + "\"");
ctx.getSource().sendFeedback(broadcastText, true);
if (MessageArgumentType.getMessage(ctx, "Console Command").getString() != "") {
run(MessageArgumentType.getMessage(ctx, "Console Command").getString(), ctx);
return 1;
} else {
return -1;
}
}))));
}
public static void run(String command, CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
runCommand(command, ctx);
}
public static void runCommand(String cmd, CommandContext<ServerCommandSource> ctx) {
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, ctx);
}
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((s = stdError.readLine()) != null) {returnCommandOutput(cmd, s, ctx);
}
} catch (IOException e) {
//e.printStackTrace();
String errorLog = "Error: \"" + cmd + "\", No such file or directory!";
ConsoleMC.LOGGER.info(errorLog);
ctx.getSource().sendFeedback(Text.of(errorLog), false);
}
}
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> ctx) {
String consoleLog = " [" + cmd + "]: " + commandFeedback;
ctx.getSource().sendFeedback(Text.of(consoleLog), false);
ConsoleMC.LOGGER.info(consoleLog);
}
}