From 8c55afe3e62a26b9d5d5cc9ba5bb87f421e463ca Mon Sep 17 00:00:00 2001 From: Jonas_Jones <91549607+J-onasJones@users.noreply.github.com> Date: Thu, 15 Dec 2022 21:28:03 +0100 Subject: [PATCH] Initial mod --- .gitignore | 40 +++++++ .../cmdconfig/CmdConfigHandler.java | 113 ++++++++++++++++++ .../betterconsolemc/cmdconfig/CmdConfigs.java | 39 ++++++ .../BroadcastToOpNotBoolException.java | 7 ++ .../exceptions/CommandModeException.java | 7 ++ .../exceptions/ExecTimeoutException.java | 7 ++ .../exceptions/PermissionLevelException.java | 7 ++ .../util/CommandPreRegistry.java | 50 ++++++++ .../betterconsolemc/util/Constants.java | 8 ++ 9 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigHandler.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigs.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/exceptions/BroadcastToOpNotBoolException.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/exceptions/CommandModeException.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/exceptions/ExecTimeoutException.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/exceptions/PermissionLevelException.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/util/CommandPreRegistry.java create mode 100644 src/main/java/me/jonasjones/betterconsolemc/util/Constants.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c476faf --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# gradle + +.gradle/ +build/ +out/ +classes/ + +# eclipse + +*.launch + +# idea + +.idea/ +*.iml +*.ipr +*.iws + +# vscode + +.settings/ +.vscode/ +bin/ +.classpath +.project + +# macos + +*.DS_Store + +# fabric + +run/ + +# java + +hs_err_*.log +replay_*.log +*.hprof +*.jfr diff --git a/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigHandler.java b/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigHandler.java new file mode 100644 index 0000000..cce88c2 --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigHandler.java @@ -0,0 +1,113 @@ +package me.jonasjones.betterconsolemc.cmdconfig; + +import me.jonasjones.betterconsolemc.BetterConsoleMC; +import me.jonasjones.betterconsolemc.exceptions.BroadcastToOpNotBoolException; +import me.jonasjones.betterconsolemc.exceptions.CommandModeException; +import me.jonasjones.betterconsolemc.exceptions.ExecTimeoutException; +import me.jonasjones.betterconsolemc.exceptions.PermissionLevelException; +import me.jonasjones.betterconsolemc.util.Constants.CmdMode; +import me.jonasjones.betterconsolemc.util.CommandPreRegistry; +import org.jetbrains.annotations.NotNull; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class CmdConfigHandler { + public static List FULLREG; + public static void getCommands(String path) throws IOException { + List commands = new ArrayList(); + BufferedReader reader = new BufferedReader(new FileReader(path)); + int linecount = 0; + String linecontent = reader.readLine(); + while (linecontent != null) { + if (!linecontent.startsWith("#") || linecontent.equals("")) { + System.out.println(linecontent); + try { + commands.add(preRegisterCommands(linecontent)); + } catch (Exception e) { + BetterConsoleMC.LOGGER.error("ERROR WHILE PREREGISTERING COMMAND IN CONFIG FILE IN LINE " + Integer.toString(linecount)); + BetterConsoleMC.LOGGER.error(String.valueOf(e)); + } + } + linecontent = reader.readLine(); + linecount++; + } + FULLREG = commands; + } + + public static CommandPreRegistry preRegisterCommands(String configCommandDef) throws Exception { + CommandPreRegistry preRegisteredCommand = new CommandPreRegistry(); + CmdMode commandMode = getCommandMode(configCommandDef); + int permissionLevel = getPermissionLevel(configCommandDef); + int execTimeout = getExecTimeout(configCommandDef); + int execRerunTimeout = getExecRerunTiemout(configCommandDef); + boolean broadcastToOP = doBroadcastToOP(configCommandDef); + String ingameCommand = getIngameCommand(configCommandDef); + String command = getCommand(configCommandDef); + + preRegisteredCommand.init(commandMode, command, permissionLevel, execTimeout, ingameCommand, execRerunTimeout, broadcastToOP); + + return preRegisteredCommand; + } + + private static CmdMode getCommandMode(String configCommandDef) throws Exception { + if (configCommandDef.startsWith("SIMPLE ")) { + return CmdMode.SIMPLE; + } else if (configCommandDef.startsWith("RETURN ")) { + return CmdMode.RETURN; + } else { + throw new CommandModeException(getSplitCommandPartByIndex(configCommandDef, 0)); + } + } + + private static int getPermissionLevel(String configCommandDef) throws PermissionLevelException { + if (-1 < Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 1))) { + return Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 1)); + } else { + throw new PermissionLevelException(Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 1))); + } + } + + private static int getExecTimeout(String configCommandDef) throws ExecTimeoutException { + if (-1 < Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 2))) { + return Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 2)); + } else { + throw new ExecTimeoutException(Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 2))); + } + } + + private static String getIngameCommand(String configCommandDef) { + return getSplitCommandPartByIndex(configCommandDef, 5); + } + + private static int getExecRerunTiemout(String configCommandDef) { + return Integer.parseInt(getSplitCommandPartByIndex(configCommandDef, 3)); + } + + private static String getBroadcastToOP(String configCommandDef) throws BroadcastToOpNotBoolException { + return getSplitCommandPartByIndex(configCommandDef, 4); + } + + private static boolean doBroadcastToOP(String configCommandDef) throws BroadcastToOpNotBoolException { + if (getSplitCommandPartByIndex(configCommandDef, 4).equals("true")) { + return true; + } else if (getSplitCommandPartByIndex(configCommandDef, 5).equals("false")) { + return false; + } else { + throw new BroadcastToOpNotBoolException(); + } + } + + private static String getCommand(String configCommandDef) throws Exception { + return configCommandDef.replaceFirst(getCommandMode(configCommandDef).name() + " " + Integer.toString(getPermissionLevel(configCommandDef)) + " " + Integer.toString(getExecTimeout(configCommandDef)) + " " + getExecRerunTiemout(configCommandDef) + " " + getBroadcastToOP(configCommandDef) + " " + getIngameCommand(configCommandDef) + " ", "") + .replaceFirst("\"", "") + .replaceAll("\"$", ""); + } + + private static String getSplitCommandPartByIndex(String configCommandDef, int index) { + return List.of(configCommandDef.split(" ")).get(index); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigs.java b/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigs.java new file mode 100644 index 0000000..c14230c --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/cmdconfig/CmdConfigs.java @@ -0,0 +1,39 @@ +package me.jonasjones.betterconsolemc.cmdconfig; + +import com.mojang.datafixers.util.Pair; +import me.jonasjones.betterconsolemc.BetterConsoleMC; +import me.jonasjones.betterconsolemc.modconfig.ModConfigProvider; +import me.jonasjones.betterconsolemc.modconfig.SimpleConfig; + +import static me.jonasjones.betterconsolemc.BetterConsoleMC.ISWINDOWS; + +public class CmdConfigs { + public static SimpleConfig CMDCONFIG; + private static ModConfigProvider cmdconfigs; + + public static void registerConfigs() { + cmdconfigs = new ModConfigProvider(); + createConfigs(); + + CMDCONFIG = SimpleConfig.of(BetterConsoleMC.MODID + "-commands_config").provider(cmdconfigs).request(); + + assignConfigs(); + } + + private static void createConfigs() { + cmdconfigs.addSingleLineComment("This is the command configuration file for BetterConsoleMC"); + if (ISWINDOWS) { + cmdconfigs.addSingleLineComment("This message is there to remind you that the mod is running on a WINODWS machine"); + } else { + cmdconfigs.addSingleLineComment("This message is there to remind you that the mod is running on a UNIX-LIKE machine"); + } + cmdconfigs.addSingleLineComment("The general syntax of defining a command goes like this:"); + cmdconfigs.addSingleLineComment("[Command Mode] [Permissione Level] [Execution Timeout] [Execution Block Timeout] [Broadcast to OP] [Ingame Command name] [command to execute]"); + cmdconfigs.addSingleLineComment("Documentation and examples are available at the wiki of the repository"); + } + + private static void assignConfigs() { + + SimpleConfig.LOGGER.info("All " + cmdconfigs.getConfigsList().size() + " have been set properly"); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/exceptions/BroadcastToOpNotBoolException.java b/src/main/java/me/jonasjones/betterconsolemc/exceptions/BroadcastToOpNotBoolException.java new file mode 100644 index 0000000..977477b --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/exceptions/BroadcastToOpNotBoolException.java @@ -0,0 +1,7 @@ +package me.jonasjones.betterconsolemc.exceptions; + +public class BroadcastToOpNotBoolException extends Exception { + public BroadcastToOpNotBoolException() { + super("Value of BroadcastToOP is not a boolean!"); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/exceptions/CommandModeException.java b/src/main/java/me/jonasjones/betterconsolemc/exceptions/CommandModeException.java new file mode 100644 index 0000000..465fe67 --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/exceptions/CommandModeException.java @@ -0,0 +1,7 @@ +package me.jonasjones.betterconsolemc.exceptions; + +public class CommandModeException extends Exception { + public CommandModeException(String error) { + super("Command Mode '" + error + "' is invalid!"); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/exceptions/ExecTimeoutException.java b/src/main/java/me/jonasjones/betterconsolemc/exceptions/ExecTimeoutException.java new file mode 100644 index 0000000..07d9b10 --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/exceptions/ExecTimeoutException.java @@ -0,0 +1,7 @@ +package me.jonasjones.betterconsolemc.exceptions; + +public class ExecTimeoutException extends Exception { + public ExecTimeoutException(int error) { + super("Execute Timeout '" + error + "' is invalid!"); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/exceptions/PermissionLevelException.java b/src/main/java/me/jonasjones/betterconsolemc/exceptions/PermissionLevelException.java new file mode 100644 index 0000000..55c791d --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/exceptions/PermissionLevelException.java @@ -0,0 +1,7 @@ +package me.jonasjones.betterconsolemc.exceptions; + +public class PermissionLevelException extends Exception { + public PermissionLevelException(int error) { + super("Permission level '" + Integer.toString(error) + "' is invalid!"); + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/util/CommandPreRegistry.java b/src/main/java/me/jonasjones/betterconsolemc/util/CommandPreRegistry.java new file mode 100644 index 0000000..c74bf80 --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/util/CommandPreRegistry.java @@ -0,0 +1,50 @@ +package me.jonasjones.betterconsolemc.util; + +import java.util.List; + +public class CommandPreRegistry { //TODO: Add timeout before command can be ran again (unixtimestamp) + private Constants.CmdMode commandmode; + private String command; + private int permissionLevel; + private int execTimeout; + private String ingamecommand; + private int execRerunTimeout; + private boolean broadcastToOp; + + public void init(Constants.CmdMode commandmode, String command, int permissionLevel, int execTimeout, String ingamecommand, int execRerunTimeout, boolean broadcastToOp) { + this.commandmode = commandmode; + this.command = command; + this.permissionLevel = permissionLevel; + this.execTimeout = execTimeout; + this.ingamecommand = ingamecommand; + this.execRerunTimeout = execRerunTimeout; + this.broadcastToOp = broadcastToOp; + } + + public Constants.CmdMode getCommandmode() { + return this.commandmode; + } + + public String getCommand() { + return this.command; + } + + public int getPermissionLevel() { + return this.permissionLevel; + } + + public int getExecTimeout() { + return this.execTimeout; + } + + public String getIngamecommand() { + return this.ingamecommand; + } + public int getExecRerunTimeout() { + return this.execRerunTimeout; + } + + public boolean doBroadcastToOp() { + return this.broadcastToOp; + } +} diff --git a/src/main/java/me/jonasjones/betterconsolemc/util/Constants.java b/src/main/java/me/jonasjones/betterconsolemc/util/Constants.java new file mode 100644 index 0000000..d39e8b6 --- /dev/null +++ b/src/main/java/me/jonasjones/betterconsolemc/util/Constants.java @@ -0,0 +1,8 @@ +package me.jonasjones.betterconsolemc.util; + +public class Constants { + public enum CmdMode { + SIMPLE, + RETURN + } +}