mirror of
https://github.com/JonasunderscoreJones/BetterConsoleMC.git
synced 2025-10-24 14:19:19 +02:00
code cleanup
This commit is contained in:
parent
43fe28e6c4
commit
6dbaff9ef5
8 changed files with 143 additions and 145 deletions
|
|
@ -12,41 +12,40 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class BetterConsoleMC 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 String MODID = "betterconsolemc";
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);
|
||||
// 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 String MODID = "betterconsolemc";
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);
|
||||
|
||||
public static boolean ISWINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
|
||||
public static Path CONFIGDIR = FabricLoader.getInstance().getConfigDir();
|
||||
public static CommandPreRegistry COMMANDPREREGISTRY;
|
||||
public static boolean ISWINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
|
||||
public static Path CONFIGDIR = FabricLoader.getInstance().getConfigDir();
|
||||
public static CommandPreRegistry COMMANDPREREGISTRY;
|
||||
|
||||
public static void registerCommands() {
|
||||
for (CommandPreRegistry commandPreRegistry : CmdConfigHandler.FULLREG) {
|
||||
COMMANDPREREGISTRY = commandPreRegistry;
|
||||
CommandRegistrationCallback.EVENT.register(GameCommandHandler::register);
|
||||
}
|
||||
}
|
||||
public static void registerCommands() {
|
||||
for (CommandPreRegistry commandPreRegistry : CmdConfigHandler.FULLREG) {
|
||||
COMMANDPREREGISTRY = commandPreRegistry;
|
||||
CommandRegistrationCallback.EVENT.register(GameCommandHandler::register);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
ModConfigs.registerConfigs();
|
||||
CmdConfigs.registerConfigs();
|
||||
LOGGER.info("BetterConsoleMC initialized!");
|
||||
try {
|
||||
CmdConfigHandler.getCommands(String.valueOf(CONFIGDIR.resolve(BetterConsoleMC.MODID + "-commands_config.properties")));
|
||||
registerCommands();
|
||||
} catch (Exception e) {
|
||||
ModConfigs.IS_ENABLED = false;
|
||||
LOGGER.error("SYNTAX ERROR(S) IN COMMAND CONFIG FILE OF BETTERCONSOLEMC");
|
||||
LOGGER.error("For security reasons, the mod has therefore beeen disabled and all commands are therefore nonfunctional");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
ModConfigs.registerConfigs();
|
||||
CmdConfigs.registerConfigs();
|
||||
LOGGER.info("BetterConsoleMC initialized!");
|
||||
try {
|
||||
CmdConfigHandler.getCommands(String.valueOf(CONFIGDIR.resolve(BetterConsoleMC.MODID + "-commands_config.properties")));
|
||||
registerCommands();
|
||||
} catch (Exception e) {
|
||||
ModConfigs.IS_ENABLED = false;
|
||||
LOGGER.error("SYNTAX ERROR(S) IN COMMAND CONFIG FILE OF BETTERCONSOLEMC");
|
||||
LOGGER.error("For security reasons, the mod has therefore beeen disabled and all commands are therefore nonfunctional");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,8 @@ 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 me.jonasjones.betterconsolemc.util.Constants.CmdMode;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
|
|
@ -17,6 +16,7 @@ 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));
|
||||
|
|
@ -28,7 +28,7 @@ public class CmdConfigHandler {
|
|||
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("ERROR WHILE PREREGISTERING COMMAND IN CONFIG FILE IN LINE " + linecount);
|
||||
BetterConsoleMC.LOGGER.error(String.valueOf(e));
|
||||
}
|
||||
}
|
||||
|
|
@ -102,7 +102,7 @@ public class CmdConfigHandler {
|
|||
}
|
||||
|
||||
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) + " ", "")
|
||||
return configCommandDef.replaceFirst(getCommandMode(configCommandDef).name() + " " + getPermissionLevel(configCommandDef) + " " + getExecTimeout(configCommandDef) + " " + getExecRerunTiemout(configCommandDef) + " " + getBroadcastToOP(configCommandDef) + " " + getIngameCommand(configCommandDef) + " ", "")
|
||||
.replaceFirst("\"", "")
|
||||
.replaceAll("\"$", "");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -3,11 +3,9 @@ package me.jonasjones.betterconsolemc.command;
|
|||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import me.jonasjones.betterconsolemc.BetterConsoleMC;
|
||||
import me.jonasjones.betterconsolemc.cmdconfig.CmdConfigHandler;
|
||||
import me.jonasjones.betterconsolemc.system.ShellCommand;
|
||||
import me.jonasjones.betterconsolemc.util.CommandPreRegistry;
|
||||
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;
|
||||
|
|
@ -23,11 +21,15 @@ public class GameCommandHandler {
|
|||
return 1;
|
||||
})))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static Text runCommand(CommandPreRegistry commandPreRegistry, CommandContext<ServerCommandSource> context) {
|
||||
new Thread(() -> {ShellCommand.execute(commandPreRegistry, context);}).start();
|
||||
new Thread(() -> {
|
||||
ShellCommand.execute(commandPreRegistry, context);
|
||||
}).start();
|
||||
return Text.of("Return");
|
||||
}
|
||||
|
||||
public static void returnCommandOutput(String cmd, String commandFeedback, CommandContext<ServerCommandSource> context) {
|
||||
String consoleLog = " [" + cmd + "]: " + commandFeedback;
|
||||
BetterConsoleMC.LOGGER.info(consoleLog);
|
||||
|
|
|
|||
|
|
@ -7,14 +7,13 @@ import java.util.List;
|
|||
|
||||
public class ModConfigProvider implements SimpleConfig.DefaultConfig {
|
||||
|
||||
private final List<Pair> configsList = new ArrayList<>();
|
||||
private String configContents = "";
|
||||
|
||||
public List<Pair> getConfigsList() {
|
||||
return configsList;
|
||||
}
|
||||
|
||||
private final List<Pair> configsList = new ArrayList<>();
|
||||
|
||||
public void addKeyValuePair(Pair<String, ?> keyValuePair, String comment) {
|
||||
configsList.add(keyValuePair);
|
||||
configContents += keyValuePair.getFirst() + "=" + keyValuePair.getSecond() + " #"
|
||||
|
|
|
|||
|
|
@ -5,10 +5,9 @@ import me.jonasjones.betterconsolemc.BetterConsoleMC;
|
|||
|
||||
public class ModConfigs {
|
||||
public static SimpleConfig MODCONFIG;
|
||||
private static ModConfigProvider modconfigs;
|
||||
|
||||
public static boolean IS_ENABLED;
|
||||
public static boolean REUQIRE_OP;
|
||||
private static ModConfigProvider modconfigs;
|
||||
|
||||
public static void registerConfigs() {
|
||||
modconfigs = new ModConfigProvider();
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import org.apache.logging.log4j.Logger;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -41,51 +42,30 @@ public class SimpleConfig {
|
|||
private final ConfigRequest request;
|
||||
private boolean broken = false;
|
||||
|
||||
public interface DefaultConfig {
|
||||
String get( String namespace );
|
||||
private SimpleConfig(ConfigRequest request) {
|
||||
this.request = request;
|
||||
String identifier = "Config '" + request.filename + "'";
|
||||
|
||||
static String empty( String namespace ) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
if (!request.file.exists()) {
|
||||
LOGGER.info(identifier + " is missing, generating default one...");
|
||||
|
||||
public static class ConfigRequest {
|
||||
|
||||
private final File file;
|
||||
private final String filename;
|
||||
private DefaultConfig provider;
|
||||
|
||||
private ConfigRequest(File file, String filename ) {
|
||||
this.file = file;
|
||||
this.filename = filename;
|
||||
this.provider = DefaultConfig::empty;
|
||||
try {
|
||||
createConfig();
|
||||
} catch (IOException e) {
|
||||
LOGGER.error(identifier + " failed to generate!");
|
||||
LOGGER.trace(e);
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default config provider, used to generate the
|
||||
* config if it's missing.
|
||||
*
|
||||
* @param provider default config provider
|
||||
* @return current config request object
|
||||
* @see DefaultConfig
|
||||
*/
|
||||
public ConfigRequest provider( DefaultConfig provider ) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the config from the filesystem.
|
||||
*
|
||||
* @return config object
|
||||
* @see SimpleConfig
|
||||
*/
|
||||
public SimpleConfig request() {
|
||||
return new SimpleConfig( this );
|
||||
}
|
||||
|
||||
private String getConfig() {
|
||||
return provider.get( filename ) + "\n";
|
||||
if (!broken) {
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(identifier + " failed to load!");
|
||||
LOGGER.trace(e);
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -97,91 +77,63 @@ public class SimpleConfig {
|
|||
* @param filename - name of the config file
|
||||
* @return new config request object
|
||||
*/
|
||||
public static ConfigRequest of( String filename ) {
|
||||
public static ConfigRequest of(String filename) {
|
||||
Path path = FabricLoader.getInstance().getConfigDir();
|
||||
return new ConfigRequest( path.resolve( filename + ".properties" ).toFile(), filename );
|
||||
return new ConfigRequest(path.resolve(filename + ".properties").toFile(), filename);
|
||||
}
|
||||
|
||||
private void createConfig() throws IOException {
|
||||
|
||||
// try creating missing files
|
||||
request.file.getParentFile().mkdirs();
|
||||
Files.createFile( request.file.toPath() );
|
||||
Files.createFile(request.file.toPath());
|
||||
|
||||
// write default config data
|
||||
PrintWriter writer = new PrintWriter(request.file, "UTF-8");
|
||||
writer.write( request.getConfig() );
|
||||
PrintWriter writer = new PrintWriter(request.file, StandardCharsets.UTF_8);
|
||||
writer.write(request.getConfig());
|
||||
writer.close();
|
||||
|
||||
}
|
||||
|
||||
private void loadConfig() throws IOException {
|
||||
Scanner reader = new Scanner( request.file );
|
||||
for( int line = 1; reader.hasNextLine(); line ++ ) {
|
||||
parseConfigEntry( reader.nextLine(), line );
|
||||
Scanner reader = new Scanner(request.file);
|
||||
for (int line = 1; reader.hasNextLine(); line++) {
|
||||
parseConfigEntry(reader.nextLine(), line);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseConfigEntry( String entry, int line ) {
|
||||
if( !entry.isEmpty() && !entry.startsWith( "#" ) ) {
|
||||
private void parseConfigEntry(String entry, int line) {
|
||||
if (!entry.isEmpty() && !entry.startsWith("#")) {
|
||||
String[] parts = entry.split("=", 2);
|
||||
if( parts.length == 2 ) {
|
||||
if (parts.length == 2) {
|
||||
//Recognise comments after a value
|
||||
String temp = parts[1].split(" #")[0];
|
||||
config.put( parts[0], temp);
|
||||
}else{
|
||||
config.put(parts[0], temp);
|
||||
} else {
|
||||
throw new RuntimeException("Syntax error in config file on line " + line + "!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SimpleConfig( ConfigRequest request ) {
|
||||
this.request = request;
|
||||
String identifier = "Config '" + request.filename + "'";
|
||||
|
||||
if( !request.file.exists() ) {
|
||||
LOGGER.info( identifier + " is missing, generating default one..." );
|
||||
|
||||
try {
|
||||
createConfig();
|
||||
} catch (IOException e) {
|
||||
LOGGER.error( identifier + " failed to generate!" );
|
||||
LOGGER.trace( e );
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
if( !broken ) {
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error( identifier + " failed to load!" );
|
||||
LOGGER.trace( e );
|
||||
broken = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries a value from config, returns `null` if the
|
||||
* key does not exist.
|
||||
*
|
||||
* @return value corresponding to the given key
|
||||
* @see SimpleConfig#getOrDefault
|
||||
* @return value corresponding to the given key
|
||||
* @see SimpleConfig#getOrDefault
|
||||
*/
|
||||
@Deprecated
|
||||
public String get( String key ) {
|
||||
return config.get( key );
|
||||
public String get(String key) {
|
||||
return config.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string value from config corresponding to the given
|
||||
* key, or the default string if the key is missing.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public String getOrDefault( String key, String def ) {
|
||||
public String getOrDefault(String key, String def) {
|
||||
String val = get(key);
|
||||
return val == null ? def : val;
|
||||
}
|
||||
|
|
@ -190,11 +142,11 @@ public class SimpleConfig {
|
|||
* Returns integer value from config corresponding to the given
|
||||
* key, or the default integer if the key is missing or invalid.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public int getOrDefault( String key, int def ) {
|
||||
public int getOrDefault(String key, int def) {
|
||||
try {
|
||||
return Integer.parseInt( get(key) );
|
||||
return Integer.parseInt(get(key));
|
||||
} catch (Exception e) {
|
||||
return def;
|
||||
}
|
||||
|
|
@ -204,11 +156,11 @@ public class SimpleConfig {
|
|||
* Returns boolean value from config corresponding to the given
|
||||
* key, or the default boolean if the key is missing.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public boolean getOrDefault( String key, boolean def ) {
|
||||
public boolean getOrDefault(String key, boolean def) {
|
||||
String val = get(key);
|
||||
if( val != null ) {
|
||||
if (val != null) {
|
||||
return val.equalsIgnoreCase("true");
|
||||
}
|
||||
|
||||
|
|
@ -219,11 +171,11 @@ public class SimpleConfig {
|
|||
* Returns double value from config corresponding to the given
|
||||
* key, or the default string if the key is missing or invalid.
|
||||
*
|
||||
* @return value corresponding to the given key, or the default value
|
||||
* @return value corresponding to the given key, or the default value
|
||||
*/
|
||||
public double getOrDefault( String key, double def ) {
|
||||
public double getOrDefault(String key, double def) {
|
||||
try {
|
||||
return Double.parseDouble( get(key) );
|
||||
return Double.parseDouble(get(key));
|
||||
} catch (Exception e) {
|
||||
return def;
|
||||
}
|
||||
|
|
@ -246,8 +198,57 @@ public class SimpleConfig {
|
|||
* @return true if the operation was successful
|
||||
*/
|
||||
public boolean delete() {
|
||||
LOGGER.warn( "Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it." );
|
||||
LOGGER.warn("Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it.");
|
||||
return request.file.delete();
|
||||
}
|
||||
|
||||
public interface DefaultConfig {
|
||||
static String empty(String namespace) {
|
||||
return "";
|
||||
}
|
||||
|
||||
String get(String namespace);
|
||||
}
|
||||
|
||||
public static class ConfigRequest {
|
||||
|
||||
private final File file;
|
||||
private final String filename;
|
||||
private DefaultConfig provider;
|
||||
|
||||
private ConfigRequest(File file, String filename) {
|
||||
this.file = file;
|
||||
this.filename = filename;
|
||||
this.provider = DefaultConfig::empty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default config provider, used to generate the
|
||||
* config if it's missing.
|
||||
*
|
||||
* @param provider default config provider
|
||||
* @return current config request object
|
||||
* @see DefaultConfig
|
||||
*/
|
||||
public ConfigRequest provider(DefaultConfig provider) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the config from the filesystem.
|
||||
*
|
||||
* @return config object
|
||||
* @see SimpleConfig
|
||||
*/
|
||||
public SimpleConfig request() {
|
||||
return new SimpleConfig(this);
|
||||
}
|
||||
|
||||
private String getConfig() {
|
||||
return provider.get(filename) + "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
|
@ -40,6 +38,7 @@ public class CommandPreRegistry { //TODO: Add timeout before command can be ran
|
|||
public String getIngamecommand() {
|
||||
return this.ingamecommand;
|
||||
}
|
||||
|
||||
public int getExecRerunTimeout() {
|
||||
return this.execRerunTimeout;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue