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-hocon</include>
<include>org.spongepowered:configurate-yaml</include> <include>org.spongepowered:configurate-yaml</include>
<include>org.spongepowered:configurate-core</include> <include>org.spongepowered:configurate-core</include>
<include>com.typesafe:config</include>
<!--<include>com.flowpowered:flow-math</include>--> <!--<include>com.flowpowered:flow-math</include>-->
</includes> </includes>
</artifactSet> </artifactSet>
@ -173,6 +174,11 @@
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.2</version>
</dependency>
<!--<dependency> <!--<dependency>
<groupId>com.flowpowered</groupId> <groupId>com.flowpowered</groupId>
<artifactId>flow-math</artifactId> <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) { 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 * These must be at the top
*/ */
@ -71,7 +73,7 @@ public abstract class Config implements VersionedConfig, Unload {
this.copyDefaults = copyDefaults; this.copyDefaults = copyDefaults;
this.mergeNewKeys = mergeNewKeys; //Whether or not we add new keys when they are found this.mergeNewKeys = mergeNewKeys; //Whether or not we add new keys when they are found
this.removeOldKeys = removeOldKeys; this.removeOldKeys = removeOldKeys;
mkdirDefaults(); // Make our default config dir
DIRECTORY_DATA_FOLDER = pathToParentFolder; //Data Folder for our plugin 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 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() private File generateDefaultFile()
{ {
mcMMO.p.getLogger().info("Attempting to create a default config for "+fileName);
//Not sure if this will work properly... //Not sure if this will work properly...
Path potentialFile = Paths.get(getDefaultConfigCopyRelativePath()); Path potentialFile = Paths.get(getDefaultConfigCopyRelativePath());
ConfigurationLoader<CommentedConfigurationNode> generation_loader ConfigurationLoader<CommentedConfigurationNode> generation_loader
= HoconConfigurationLoader.builder().setPath(potentialFile).build(); = HoconConfigurationLoader.builder().setPath(potentialFile).build();
try { 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(); defaultRootNode = generation_loader.load();
//Save to a new file
getDefaultConfigFile().createNewFile();
generation_loader.save(defaultRootNode); generation_loader.save(defaultRootNode);
mcMMO.p.getLogger().info("Generated a default file for "+fileName); mcMMO.p.getLogger().info("Generated a default file for "+fileName);
} catch(IOException e) { } catch(IOException e) {
mcMMO.p.getLogger().severe("Error when trying to generate a default configuration file for " + getDefaultConfigCopyRelativePath()); 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 * @return the path to the defaults directory
*/ */
private String getDefaultConfigCopyRelativePath() { 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 * @return the copy of the default config file, stored in the defaults directory
*/ */
private File getDefaultConfigFile() { 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 * Creates the defaults directory
*/ */
private void mkdirDefaults() { private void mkdirDefaults() {
//Make Default Subdirectory ConfigConstants.makeAllConfigDirectories();
File defaultsDir = new File (DIRECTORY_DATA_FOLDER, "defaults");
if(!defaultsDir.exists())
defaultsDir.mkdir();
} }
/** /**

View File

@ -9,9 +9,10 @@ import java.io.File;
*/ */
public class ConfigConstants { public class ConfigConstants {
/* FOLDER NAMES */ /* FOLDER NAMES */
public static final String FOLDER_NAME_CONFIG = "config"; public static final String FOLDER_NAME_CONFIG = "config";
public static final String FOLDER_NAME_SKILLS = "skills"; public static final String FOLDER_NAME_SKILLS = "skills";
public static final String FOLDER_NAME_EXPERIENCE = "Experience Settings"; public static final String FOLDER_NAME_EXPERIENCE = "Experience Settings";
public static final String FOLDER_NAME_DEFAULTS = "defaults";
/* RELATIVE PATHS */ /* RELATIVE PATHS */
public final static String RELATIVE_PATH_CONFIG_DIR = File.separator + FOLDER_NAME_CONFIG + File.separator; 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(); 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();
}
} }