Initial mod

This commit is contained in:
Jonas_Jones 2022-12-15 21:28:03 +01:00
parent 7684fb621f
commit 8c55afe3e6
9 changed files with 278 additions and 0 deletions

40
.gitignore vendored Normal file
View file

@ -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

View file

@ -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<CommandPreRegistry> FULLREG;
public static void getCommands(String path) throws IOException {
List<CommandPreRegistry> commands = new ArrayList<CommandPreRegistry>();
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);
}
}

View file

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

View file

@ -0,0 +1,7 @@
package me.jonasjones.betterconsolemc.exceptions;
public class BroadcastToOpNotBoolException extends Exception {
public BroadcastToOpNotBoolException() {
super("Value of BroadcastToOP is not a boolean!");
}
}

View file

@ -0,0 +1,7 @@
package me.jonasjones.betterconsolemc.exceptions;
public class CommandModeException extends Exception {
public CommandModeException(String error) {
super("Command Mode '" + error + "' is invalid!");
}
}

View file

@ -0,0 +1,7 @@
package me.jonasjones.betterconsolemc.exceptions;
public class ExecTimeoutException extends Exception {
public ExecTimeoutException(int error) {
super("Execute Timeout '" + error + "' is invalid!");
}
}

View file

@ -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!");
}
}

View file

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

View file

@ -0,0 +1,8 @@
package me.jonasjones.betterconsolemc.util;
public class Constants {
public enum CmdMode {
SIMPLE,
RETURN
}
}