wip alias implementation

This commit is contained in:
Jonas_Jones 2024-09-15 02:33:51 +02:00
parent 9f3750b52d
commit 16c45c7cf5
7 changed files with 83 additions and 2 deletions

View file

@ -8,7 +8,7 @@ yarn_mappings=1.21+build.2
loader_version=0.15.11
# Mod Properties
mod_version = 1.0.2
mod_version = 1.0.2-featuresnapshot01
maven_group = me.jonasjones
archives_base_name = betterconsolemc

View file

@ -6,10 +6,12 @@ import me.jonasjones.betterconsolemc.modconfig.ModConfigs;
import me.jonasjones.betterconsolemc.util.CommandPreRegistry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.file.Path;
import java.util.Map;
import static me.jonasjones.betterconsolemc.command.GameCommandHandler.registerCommands;

View file

@ -59,6 +59,10 @@ public class CmdConfigHandler {
return CmdMode.SIMPLE;
} else if (configCommandDef.startsWith("RETURN ")) {
return CmdMode.RETURN;
} else if (configCommandDef.startsWith("GLOBALALIAS ")) {
return CmdMode.GLOBALALIAS;
} else if (configCommandDef.startsWith("SELFALIAS ")) {
return CmdMode.SELFALIAS;
} else {
throw new CommandModeException(getSplitCommandPartByIndex(configCommandDef, 0));
}

View file

@ -0,0 +1,35 @@
package me.jonasjones.betterconsolemc.command;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
public class CapturingCommandSource extends ServerCommandSource {
private final StringBuilder outputBuilder;
public CapturingCommandSource(MinecraftServer server) {
super(server,
new Vec3d(0, 0, 0), // No specific entity
new Vec2f(0, 0), // No specific entity
server.getCommandSource().getWorld(),
server.getCommandSource().getServer().getOpPermissionLevel(),
"Server",
Text.literal("Server"),
server,
null
);
this.outputBuilder = new StringBuilder();
}
@Override
public void sendMessage(Text message) {
outputBuilder.append(message.getString()).append("\n");
}
public String getCapturedOutput() {
return outputBuilder.toString();
}
}

View file

@ -8,7 +8,9 @@ import me.jonasjones.betterconsolemc.system.ShellCommand;
import me.jonasjones.betterconsolemc.util.CommandPreRegistry;
import me.jonasjones.betterconsolemc.util.Constants;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
@ -17,6 +19,8 @@ import static me.jonasjones.betterconsolemc.BetterConsoleMC.COMMANDPREREGISTRY;
public class GameCommandHandler {
private static MinecraftServer MC_SERVER;
public static void registerCommands() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
for (CommandPreRegistry commandPreRegistry : CmdConfigHandler.FULLREG) {
@ -28,6 +32,18 @@ public class GameCommandHandler {
public static void register(CommandDispatcher<ServerCommandSource> serverCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment registrationEnvironment, CommandPreRegistry commandPreRegistry) {
if (commandPreRegistry.getCommandmode() == Constants.CmdMode.GLOBALALIAS || commandPreRegistry.getCommandmode() == Constants.CmdMode.SELFALIAS) {
serverCommandSourceCommandDispatcher.register((CommandManager.literal(commandPreRegistry.getIngamecommand()).requires(source -> source.hasPermissionLevel(commandPreRegistry.getPermissionLevel()))
.executes((context -> {
runIngameCommand(commandPreRegistry, context);
return 1;
})))
);
// if a command is an alias type, register the tick event
startServerHandler();
return;
}
serverCommandSourceCommandDispatcher.register((CommandManager.literal(commandPreRegistry.getIngamecommand()).requires(source -> source.hasPermissionLevel(commandPreRegistry.getPermissionLevel()))
.executes((context -> {
if (commandPreRegistry.getCommandmode() == Constants.CmdMode.RETURN) {
@ -43,8 +59,24 @@ public class GameCommandHandler {
return Text.of(ShellCommand.execute(commandPreRegistry, context));
}
public static void runIngameCommand(CommandPreRegistry commandPreRegistry, CommandContext<ServerCommandSource> context) {
if (commandPreRegistry.getCommandmode() == Constants.CmdMode.GLOBALALIAS) {
MC_SERVER.getCommandManager().executeWithPrefix(MC_SERVER.getCommandSource(), commandPreRegistry.getCommand());
} else {
MC_SERVER.getCommandManager().executeWithPrefix(context.getSource(), commandPreRegistry.getCommand());
}
}
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> context) {
String consoleLog = " [" + cmd + "]: " + commandFeedback;
BetterConsoleMC.LOGGER.info(consoleLog);
}
private static void startServerHandler() {
ServerTickEvents.END_SERVER_TICK.register(server -> {
if (server.isRunning()) {
MC_SERVER = server;
}
});
}
}

View file

@ -5,14 +5,20 @@ import me.jonasjones.betterconsolemc.BetterConsoleMC;
import me.jonasjones.betterconsolemc.command.GameCommandHandler;
import me.jonasjones.betterconsolemc.modconfig.ModConfigs;
import me.jonasjones.betterconsolemc.util.CommandPreRegistry;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.command.CommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ShellCommand {
public static String execute(CommandPreRegistry command, CommandContext<ServerCommandSource> context) {
StringBuilder output = new StringBuilder();

View file

@ -3,6 +3,8 @@ package me.jonasjones.betterconsolemc.util;
public class Constants {
public enum CmdMode {
SIMPLE,
RETURN
RETURN,
GLOBALALIAS,
SELFALIAS
}
}