Allow reading values from config
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-21 23:36:42 +01:00
parent c650caa090
commit b6bf366822
3 changed files with 30 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import net.dv8tion.jda.api.interactions.commands.Command;
import org.jetbrains.annotations.Nullable;
import wtf.beatrice.hidekobot.database.DatabaseManager;
import wtf.beatrice.hidekobot.listeners.MessageLogger;
import wtf.beatrice.hidekobot.utils.ConfigurationManager;
import java.awt.*;
import java.time.LocalDateTime;
@ -14,7 +15,7 @@ import java.util.List;
public class Cache
{
private static ConfigurationManager configManager = null;
private static DatabaseManager dbManager = null;
private static boolean verbose = false;
private static MessageLogger verbosityLogger;
@ -235,4 +236,10 @@ public class Cache
}
public static String getExecPath() { return execPath; }
public static ConfigurationManager getConfigurationManager()
{ return configManager; }
public static void setConfigManager(ConfigurationManager configurationManager)
{ configManager = configurationManager; }
}

View File

@ -14,7 +14,7 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask;
import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask;
import wtf.beatrice.hidekobot.runnables.HeartBeatTask;
import wtf.beatrice.hidekobot.utils.ConfigurationUtil;
import wtf.beatrice.hidekobot.utils.ConfigurationManager;
import wtf.beatrice.hidekobot.utils.Logger;
import wtf.beatrice.hidekobot.utils.SlashCommandUtil;
@ -114,9 +114,9 @@ public class HidekoBot
// load configuration
String configFilePath = Cache.getExecPath() + File.separator + "config.yml";
ConfigurationUtil configurationUtil = new ConfigurationUtil(configFilePath);
configurationUtil.initConfig();
ConfigurationManager configurationManager = new ConfigurationManager(configFilePath);
configurationManager.initConfig();
Cache.setConfigManager(configurationManager);
// connect to database
logger.log("Connecting to database...");

View File

@ -7,13 +7,15 @@ import wtf.beatrice.hidekobot.HidekoBot;
import java.io.*;
import java.util.LinkedHashMap;
public class ConfigurationUtil
public class ConfigurationManager
{
private final LinkedHashMap<String, Object> configurationEntries = new LinkedHashMap<>();
private final Logger logger;
private final String configFilePath;
public ConfigurationUtil(String configFilePath)
public ConfigurationManager(String configFilePath)
{
this.configFilePath = configFilePath;
logger = new Logger(getClass());
@ -114,7 +116,21 @@ public class ConfigurationUtil
return;
}
// finally, dump all entries to cache.
loadConfig(filledEntries);
} else {
// if no key is missing, just cache all entries and values from filesystem.
loadConfig(fsConfigContents);
}
}
private void loadConfig(LinkedHashMap<String, Object> configurationEntries)
{
configurationEntries.putAll(configurationEntries);
}
private Object getConfigValue(String key)
{
return configurationEntries.get(key);
}
}