Skip to content

Commit

Permalink
Adding the possibility to initialize a config with a custom directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Sep 6, 2022
1 parent 335dc11 commit a144c13
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ maven {
url = 'https://repo.mikigal.pl/releases'
}
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.2'
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.3'
```

#### Maven
Expand All @@ -34,7 +34,7 @@ compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.2'
<dependency>
<groupId>pl.mikigal</groupId>
<artifactId>ConfigAPI</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
```
Expand Down
Empty file modified gradlew
100755 → 100644
Empty file.
9 changes: 6 additions & 3 deletions src/main/java/pl/mikigal/config/BukkitConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class BukkitConfiguration extends YamlConfiguration {
/**
* Properties of config
*/
private final File directory;
private final File file;
private final NameStyle nameStyle;
private final CommentStyle commentStyle;
Expand All @@ -42,7 +43,9 @@ public class BukkitConfiguration extends YamlConfiguration {
private final Map<String, Object> cache;
private final Map<String, String> comments;

public BukkitConfiguration(File file, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings, String configComment) {
public BukkitConfiguration(File directory, File file, NameStyle nameStyle, CommentStyle commentStyle,
boolean automaticColorStrings, String configComment) {
this.directory = directory;
this.file = file;
this.nameStyle = nameStyle;
this.commentStyle = commentStyle;
Expand Down Expand Up @@ -176,8 +179,8 @@ public void save() {
*/
private void copyDefaultConfig() {
try {
if (!ConfigAPI.getPlugin().getDataFolder().exists()) {
ConfigAPI.getPlugin().getDataFolder().mkdir();
if (!directory.exists()) {
directory.mkdir();
}

if (this.file.exists()) {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/pl/mikigal/config/ConfigAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public class ConfigAPI {
* @param nameStyle Style of config's fields names
* @param commentStyle Style of config's comments
* @param automaticColorStrings Automatic translate '&' based colors
* @param dir The config's directory.
* @param directory The config's directory.
* @param plugin Instance of your plugin
* @see NameStyle
* @see CommentStyle
* @return Instance of {@param clazz} ready to use methods
*/
public static <T extends Config> T init(Class<T> clazz, NameStyle nameStyle, CommentStyle commentStyle,
boolean automaticColorStrings, File dir, JavaPlugin plugin){
boolean automaticColorStrings, File directory, JavaPlugin plugin){

ConfigAPI.plugin = plugin;
ConfigName configName = clazz.getAnnotation(ConfigName.class);
Expand All @@ -62,9 +62,10 @@ public static <T extends Config> T init(Class<T> clazz, NameStyle nameStyle, Com
String configComment = configCommentAnnotation == null ? null : configCommentAnnotation.value();

String name = configName.value() + (configName.value().endsWith(".yml") ? "" : ".yml");
File file = new File(dir, name);
File file = new File(directory, name);

BukkitConfiguration rawConfiguration = new BukkitConfiguration(file, nameStyle, commentStyle, automaticColorStrings, configComment);
BukkitConfiguration rawConfiguration = new BukkitConfiguration(directory, file, nameStyle, commentStyle,
automaticColorStrings, configComment);
rawConfigurations.put(name, rawConfiguration);

T configuration = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz},
Expand Down Expand Up @@ -100,19 +101,20 @@ public static <T extends Config> T init(Class<T> clazz, NameStyle nameStyle, Com
* @return Instance of {@param clazz} ready to use methods
*/
public static <T extends Config> T init(Class<T> clazz, JavaPlugin plugin) {
return init(clazz, NameStyle.CAMEL_CASE, CommentStyle.ABOVE_CONTENT, true, plugin);
return init(clazz, NameStyle.CAMEL_CASE, CommentStyle.ABOVE_CONTENT,
true, plugin.getDataFolder(), plugin);
}

/**
* Initializes instance of Config with default values
* (CAMEL_CASE as NameStyle, ABOVE_CONTENT as CommentStyle, enabled automatic translation of '&' based colors)
* @param clazz Class of your Config interface
* @param dir The config's directory.
* @param directory The config's directory.
* @param plugin Instance of your plugin
* @return Instance of {@param clazz} ready to use methods
*/
public static <T extends Config> T init(Class<T> clazz, File dir, JavaPlugin plugin) {
return init(clazz, NameStyle.CAMEL_CASE, CommentStyle.ABOVE_CONTENT, true, dir, plugin);
public static <T extends Config> T init(Class<T> clazz, File directory, JavaPlugin plugin) {
return init(clazz, NameStyle.CAMEL_CASE, CommentStyle.ABOVE_CONTENT, true, directory, plugin);
}

/**
Expand Down
Empty file.
Empty file.

0 comments on commit a144c13

Please sign in to comment.