Skip to content

Commit

Permalink
Fix yaml options and proper comment
Browse files Browse the repository at this point in the history
Signed-off-by: DevDrizzy <[email protected]>
  • Loading branch information
DevDrizzy committed Aug 15, 2023
1 parent a78ac09 commit 545a1b5
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/main/java/xyz/refinedev/api/storage/YamlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import org.bukkit.plugin.java.JavaPlugin;

import org.simpleyaml.configuration.ConfigurationSection;
import org.simpleyaml.configuration.comments.format.PrettyYamlCommentFormatter;
import org.simpleyaml.configuration.comments.format.YamlCommentFormat;
import org.simpleyaml.configuration.file.YamlConfiguration;
import org.simpleyaml.configuration.file.YamlConfigurationOptions;
import org.simpleyaml.configuration.file.YamlFile;
import org.simpleyaml.configuration.implementation.api.QuoteStyle;

import org.simpleyaml.configuration.implementation.snakeyaml.lib.comments.CommentType;
import xyz.refinedev.api.storage.annotations.ConfigValue;

import java.io.File;
Expand Down Expand Up @@ -49,7 +54,10 @@ public YamlStorage(JavaPlugin plugin, String name, boolean saveResource) {

this.name = name;
this.config = new YamlFile(file);
this.config.options().charset(Charsets.UTF_8);

// Default options of this Yaml Storage
// User can set their own by overriding this method
this.setupConfigOptions(this.config.options());

if (!file.exists()) {
try {
Expand All @@ -66,7 +74,6 @@ public YamlStorage(JavaPlugin plugin, String name, boolean saveResource) {

try {
if (saveResource) {
this.config.options().useComments(true);
this.config.load(file);
} else {
this.config.load();
Expand Down Expand Up @@ -108,7 +115,8 @@ public void readConfig() {
// Don't go adding empty comments, they'll just create empty lines
// between different keys, making config look awful
if (configValue.comment().length() > 0) {
this.config.setComment(configValue.path(), configValue.comment());
this.config.path(configValue.path()).comment(configValue.comment()).blankLine();
//this.config.setComment(configValue.path(), "\n#" + configValue.comment(), YamlCommentFormat.DEFAULT);
}

} catch (IllegalArgumentException | IllegalAccessException ex) {
Expand All @@ -120,6 +128,22 @@ public void readConfig() {
this.saveConfig();
}

/**
* Reload this config
*/
public void reload() {
try {
this.config.options().useComments(true);
this.config.load();
} catch (IOException ex) {
LOGGER.error("[Storage] Could not load " + name + ".yml, please correct your syntax errors!");
LOGGER.error("[Storage] Error: " + ex.getMessage());
}

this.readConfig();
this.writeConfig();
}

/**
* Write our config values to the config
*/
Expand Down Expand Up @@ -167,6 +191,20 @@ public void clearConfig() {
this.saveConfig();
}

/**
* Here you can set config options on your own
*
* @param options {@link YamlConfigurationOptions options}
*/
public void setupConfigOptions(YamlConfigurationOptions options) {
this.config.setCommentFormat(YamlCommentFormat.PRETTY);

options.charset(Charsets.UTF_8);
options.useComments(true);
options.quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);
options.header(String.join("\n", this.getHeader()));
}

/**
* Comments that are not by config values but added
* in paths that are separate.
Expand Down

0 comments on commit 545a1b5

Please sign in to comment.