Skip to content

Commit

Permalink
implement custom file extensions and directories with appropriate tes…
Browse files Browse the repository at this point in the history
…ts, vbump to 1.0.4
  • Loading branch information
Draylar committed Mar 25, 2021
1 parent 4e24988 commit 9e9eac0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.16.5+build.5
loader_version=0.11.2

# Mod Properties
mod_version=1.0.3
mod_version=1.0.4
maven_group=draylar
archives_base_name=omega-config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public static <T extends Config> void writeConfig(Class<T> configClass, T inst
lines.forEach(str -> res.append(String.format("%s%n", str)));

try {
Files.write(getConfigPath(instance), res.toString().getBytes());
Path configPath = getConfigPath(instance);
configPath.toFile().getParentFile().mkdirs();
Files.write(configPath, res.toString().getBytes());
} catch (IOException ioException) {
LOGGER.error(ioException);
LOGGER.info(String.format("Write error, using default values for config %s.", configClass.toString()));
Expand Down Expand Up @@ -199,7 +201,7 @@ private static String getStartingWhitespace(String input) {
}

public static Path getConfigPath(Config config) {
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), String.format("%s.json", config.getName()));
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), config.getDirectory(), String.format("%s.%s", config.getName(), config.getExtension()));
}

public static boolean configExists(Config config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,33 @@ default String getModid() {
return null;
}

default boolean hasMenu() {
return true;
/**
* Returns the file extension of this config.
*
* <p>
* The file extension is used while serializing this config to a local file.
* The primary use-case of switching this would be supporting existing config files
* when porting from other json5 config libraries.
*
* @return the file extension of this config
*/
default String getExtension() {
return "json";
}

/**
* Returns the directory of this config, assuming the base directory is the instance config directory.
*
* <p>
* By default, a config such as 'my_config' will appear at /config/my_config.json.
* If this method specifies a directory, such as 'configurations',
* the config file will appear at /config/configurations/my_config.json.
*
* Nested directories can be specified by using a string such as 'configurations/client'.
*
* @return the directory of this config
*/
default String getDirectory() {
return "";
}
}
18 changes: 18 additions & 0 deletions src/testmod/java/draylar/omegatest/NestedConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package draylar.omegatest;

import draylar.omegaconfig.api.Config;

public class NestedConfig implements Config {

public boolean test = false;

@Override
public String getName() {
return "nested";
}

@Override
public String getDirectory() {
return "test";
}
}
5 changes: 5 additions & 0 deletions src/testmod/java/draylar/omegatest/NewTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ public static class StructureChances {
public int killer_bunny_castle_seperation = 25;
public int killer_bunny_castle_spacing = 50;
}

@Override
public String getExtension() {
return "json5";
}
}
1 change: 1 addition & 0 deletions src/testmod/java/draylar/omegatest/OmegaTestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class OmegaTestMain implements ModInitializer {

public static final TestConfig CONFIG = OmegaConfig.register(TestConfig.class);
public static final NewTestConfig MO_CONFIG = OmegaConfig.register(NewTestConfig.class);
public static final NestedConfig NESTED = OmegaConfig.register(NestedConfig.class);

@Override
public void onInitialize() {
Expand Down

0 comments on commit 9e9eac0

Please sign in to comment.