Add typesafe library to maven, cleanup config directory code

This commit is contained in:
nossr50 2019-03-08 22:29:02 -08:00
parent 55b1da0341
commit 8bc49e8bfb
3 changed files with 89 additions and 11 deletions

View File

@ -102,6 +102,7 @@
<include>org.spongepowered:configurate-hocon</include>
<include>org.spongepowered:configurate-yaml</include>
<include>org.spongepowered:configurate-core</include>
<include>com.typesafe:config</include>
<!--<include>com.flowpowered:flow-math</include>-->
</includes>
</artifactSet>
@ -173,6 +174,11 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.2</version>
</dependency>
<!--<dependency>
<groupId>com.flowpowered</groupId>
<artifactId>flow-math</artifactId>

View File

@ -63,6 +63,8 @@ public abstract class Config implements VersionedConfig, Unload {
}*/
public Config(String fileName, File pathToParentFolder, String relativePath, boolean generateDefaults, boolean mergeNewKeys, boolean copyDefaults, boolean removeOldKeys) {
mkdirDefaults(); // Make our default config dir
/*
* These must be at the top
*/
@ -71,7 +73,7 @@ public abstract class Config implements VersionedConfig, Unload {
this.copyDefaults = copyDefaults;
this.mergeNewKeys = mergeNewKeys; //Whether or not we add new keys when they are found
this.removeOldKeys = removeOldKeys;
mkdirDefaults(); // Make our default config dir
DIRECTORY_DATA_FOLDER = pathToParentFolder; //Data Folder for our plugin
FILE_RELATIVE_PATH = relativePath + fileName + HOCON_FILE_EXTENSION; //Relative path to config from a parent folder
@ -173,13 +175,25 @@ public abstract class Config implements VersionedConfig, Unload {
*/
private File generateDefaultFile()
{
mcMMO.p.getLogger().info("Attempting to create a default config for "+fileName);
//Not sure if this will work properly...
Path potentialFile = Paths.get(getDefaultConfigCopyRelativePath());
ConfigurationLoader<CommentedConfigurationNode> generation_loader
= HoconConfigurationLoader.builder().setPath(potentialFile).build();
try {
mcMMO.p.getLogger().info("Config File Full Path: "+getDefaultConfigFile().getAbsolutePath());
//Delete any existing default config
if(getDefaultConfigFile().exists())
getDefaultConfigFile().delete();
//Load the config
defaultRootNode = generation_loader.load();
//Save to a new file
getDefaultConfigFile().createNewFile();
generation_loader.save(defaultRootNode);
mcMMO.p.getLogger().info("Generated a default file for "+fileName);
} catch(IOException e) {
mcMMO.p.getLogger().severe("Error when trying to generate a default configuration file for " + getDefaultConfigCopyRelativePath());
@ -277,7 +291,7 @@ public abstract class Config implements VersionedConfig, Unload {
* @return the path to the defaults directory
*/
private String getDefaultConfigCopyRelativePath() {
return DIRECTORY_DEFAULTS + File.separator + FILE_RELATIVE_PATH;
return getDefaultConfigFile().getPath();
}
/**
@ -286,18 +300,14 @@ public abstract class Config implements VersionedConfig, Unload {
* @return the copy of the default config file, stored in the defaults directory
*/
private File getDefaultConfigFile() {
return new File(DIRECTORY_DATA_FOLDER, DIRECTORY_DEFAULTS + File.separator + FILE_RELATIVE_PATH);
return new File(ConfigConstants.getDefaultsFolder(), FILE_RELATIVE_PATH);
}
/**
* Creates the defaults directory
*/
private void mkdirDefaults() {
//Make Default Subdirectory
File defaultsDir = new File (DIRECTORY_DATA_FOLDER, "defaults");
if(!defaultsDir.exists())
defaultsDir.mkdir();
ConfigConstants.makeAllConfigDirectories();
}
/**

View File

@ -12,6 +12,7 @@ public class ConfigConstants {
public static final String FOLDER_NAME_CONFIG = "config";
public static final String FOLDER_NAME_SKILLS = "skills";
public static final String FOLDER_NAME_EXPERIENCE = "Experience Settings";
public static final String FOLDER_NAME_DEFAULTS = "defaults";
/* RELATIVE PATHS */
public final static String RELATIVE_PATH_CONFIG_DIR = File.separator + FOLDER_NAME_CONFIG + File.separator;
@ -26,4 +27,65 @@ public class ConfigConstants {
{
return mcMMO.p.getDataFolder();
}
public static File getConfigFolder() {
return new File(getDataFolder(), FOLDER_NAME_CONFIG);
}
public static File getDefaultsFolder() {
return new File(getConfigFolder().getAbsolutePath(), FOLDER_NAME_DEFAULTS);
}
public static File getDefaultsConfigFolder() {
return new File(getDefaultsFolder().getAbsolutePath(), FOLDER_NAME_CONFIG);
}
public static File getDefaultsSkillFolder() {
return new File(getDefaultsConfigFolder().getAbsolutePath(), FOLDER_NAME_SKILLS);
}
public static File getDefaultsXPFolder() {
return new File(getDefaultsConfigFolder().getAbsolutePath(), FOLDER_NAME_EXPERIENCE);
}
public static File getConfigSkillFolder() {
return new File(getConfigFolder().getAbsolutePath(), FOLDER_NAME_SKILLS);
}
public static File getConfigXPFolder() {
return new File(getConfigFolder().getAbsolutePath(), FOLDER_NAME_EXPERIENCE);
}
/**
* Creates all directories used by mcMMO config files
*/
public static void makeAllConfigDirectories()
{
/* CONFIG DIRECTORY */
if(!getConfigFolder().exists())
getConfigFolder().mkdirs();
/* DEFAULT DIRECTORIES */
if(!getDefaultsFolder().exists())
getDefaultsFolder().mkdirs();
if(!getDefaultsConfigFolder().exists())
getDefaultsConfigFolder().mkdirs();
if(!getDefaultsSkillFolder().exists())
getDefaultsSkillFolder().mkdirs();
if(!getDefaultsXPFolder().exists())
getDefaultsXPFolder().mkdirs();
/* CONFIG SUBDIRECTORIES */
if(!getConfigSkillFolder().exists())
getConfigSkillFolder().mkdirs();
if(!getConfigXPFolder().exists())
getConfigXPFolder().mkdirs();
}
}