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,23 @@
package me.jonasjones.consolemc;
import me.jonasjones.consolemc.command.RunCommand;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
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");
private static void registerCommands() {
CommandRegistrationCallback.EVENT.register(RunCommand::register);
}
@Override
public void onInitialize() {
registerCommands();
}
}

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);
}
}

View file

@ -0,0 +1,16 @@
package me.jonasjones.consolemc.mixin;
import me.jonasjones.consolemc.ConsoleMC;
import net.minecraft.client.gui.screen.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(TitleScreen.class)
public class ExampleMixin {
@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 B

View file

@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8",
"package": "me.jonasjones.consolemc.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
],
"client": [
"ExampleMixin"
],
"injectors": {
"defaultRequire": 1
}
}

View file

@ -0,0 +1,38 @@
{
"schemaVersion": 1,
"id": "consolemc",
"version": "v0.0.1-alpha1",
"name": "ConsoleMC",
"description": "A mod that allows you to run console commands on the server from the minecraft chat.",
"authors": [
"Jonas_Jones"
],
"contact": {
"homepage": "http://jonasjones.me/consolemc",
"sources": "https://github.com/J-onasJones/ConsoleMC"
},
"license": "CC0-1.0",
"icon": "assets/consolemc/icon.png",
"environment": "server",
"entrypoints": {
"main": [
"me.jonasjones.consolemc.ConsoleMC"
]
},
"mixins": [
"consolemc.mixins.json"
],
"depends": {
"fabricloader": ">=0.14.6",
"fabric": "*",
"minecraft": "~1.19",
"java": ">=17"
},
"suggests": {
"another-mod": "*"
}
}