Skip to content

Commit

Permalink
Add filename and subdirectory overrides to Config class annotation (#33)
Browse files Browse the repository at this point in the history
* Add filename and subdirectory overrides

* spotless

* Fix nit

* Nother nit
  • Loading branch information
Caedis authored Feb 26, 2024
1 parent 389101e commit f6cfa5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
*/
String category() default "general";

/**
* The subdirectory of the config directory to use. Defaults to none (config/). If you want to use a subdirectory,
* you must specify it as a relative path (e.g. "myMod").
*/
String configSubDirectory();

/**
* The name of the configuration file. Defaults to the modid. The file extension (.cfg) is added automatically.
*/
String filename();

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.TYPE })
@interface LangKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ public static void registerConfig(Class<?> configClass) throws ConfigException {
val category = Optional.of(cfg.category().trim()).map((cat) -> cat.length() == 0 ? null : cat).orElseThrow(
() -> new ConfigException("Config class " + configClass.getName() + " has an empty category!"));
val rawConfig = configs.computeIfAbsent(cfg.modid(), (ignored) -> {
val c = new Configuration(configDir.resolve(cfg.modid() + ".cfg").toFile());
Path newConfigDir = configDir;
if (cfg.configSubDirectory() != null) {
newConfigDir = newConfigDir.resolve(cfg.configSubDirectory());
}
String fileName;
if (cfg.filename() != null) {
fileName = cfg.filename();
} else {
fileName = cfg.modid();
}
val c = new Configuration(newConfigDir.resolve(fileName + ".cfg").toFile());
c.load();
return c;
});
Expand Down

0 comments on commit f6cfa5c

Please sign in to comment.