From 4e5f8ddcda7fff527afe14ca9afa54ff807deacf Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 24 Aug 2019 14:11:23 -0400 Subject: [PATCH] More config stuff --- .../core/library/settings/Category.java | 19 ++++--- .../songoda/core/library/settings/Config.java | 55 ++++++++++++++----- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/songoda/core/library/settings/Category.java b/src/main/java/com/songoda/core/library/settings/Category.java index 4d1dee4b..49b64ee2 100644 --- a/src/main/java/com/songoda/core/library/settings/Category.java +++ b/src/main/java/com/songoda/core/library/settings/Category.java @@ -10,13 +10,13 @@ public class Category extends Narrow { protected final Map defaultSettings = new LinkedHashMap<>(); - private final List comments = new ArrayList<>(); + private final Map> comments = new HashMap<>(); public Category(Config config, String key, String... comments) { this.config = config; this.key = key; if (comments != null) - this.comments.addAll(Arrays.asList(comments)); + this.comments.put(null, Arrays.asList(comments)); } public Category(Config config, String key) { @@ -26,7 +26,7 @@ public class Category extends Narrow { public Category addAll(Category category) { addSettings(category); if (comments.size() == 0) - addComments(category.getComments()); + comments.putAll(category.getAllComments()); return this; } @@ -73,12 +73,17 @@ public class Category extends Narrow { return key; } - public void addComments(List commments) { - this.comments.addAll(commments); + public Category addComment(String key, String... comments) { + this.comments.put(key, Arrays.asList(comments)); + return this; } - public List getComments() { - return Collections.unmodifiableList(comments); + public List getComments(String key) { + return Collections.unmodifiableList(comments.get(key)); + } + + public Map> getAllComments() { + return Collections.unmodifiableMap(comments); } public Config getConfig() { diff --git a/src/main/java/com/songoda/core/library/settings/Config.java b/src/main/java/com/songoda/core/library/settings/Config.java index 501c0470..59c42a28 100644 --- a/src/main/java/com/songoda/core/library/settings/Config.java +++ b/src/main/java/com/songoda/core/library/settings/Config.java @@ -6,7 +6,10 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; import java.io.*; -import java.util.*; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; public class Config { @@ -16,7 +19,8 @@ public class Config { private FileConfiguration fileConfiguration; private File configFile; - private boolean allowUserExpansion, categorySpacing, commentSpacing = true; + private boolean allowUserExpansion = true, categorySpacing = true, + commentSpacing = true, showNullCategoryComments = true; private final Map categories = new LinkedHashMap<>(); @@ -34,7 +38,7 @@ public class Config { /** * This allows users to expand the config and create new lines as well as * remove older lines. If this is disabled the config will regenerate - * removed lines as well as add new lines that are added in the future. + * removed lines as well as add new lines that are added in the future * * @param allowUserExpansion allow users to expand config, otherwise don't * @return this class @@ -45,7 +49,7 @@ public class Config { } /** - * This will add two spaces above each category. + * This will add two spaces above each category * * @param categorySpacing add two spaces above each category, otherwise don't * @return this class @@ -57,9 +61,9 @@ public class Config { /** * This will add a single space above each commented setting. Useful when - * you don't want your comments to stand out. + * you don't want your comments to stand out * - * @param commentSpacing add a space above each comment, otherwise don't. + * @param commentSpacing add a space above each comment, otherwise don't * @return this class */ public Config commentSpacing(boolean commentSpacing) { @@ -67,6 +71,19 @@ public class Config { return this; } + /** + * This will add a single space above each commented setting. Useful when + * you don't want your comments to stand out + * + * @param showNullCategoryComments shows a placeholder comment when null, + * otherwise doesn't + * @return this class + */ + public Config showNullCategoryComments(boolean showNullCategoryComments) { + this.showNullCategoryComments = showNullCategoryComments; + return this; + } + public Category addCategory(String key, String... comments) { return addCategory(new Category(this, key, comments)); } @@ -217,14 +234,26 @@ public class Config { if (!category.contains(".")) config.append("#").append("\n"); try { - Category categoryObj = getCategory(category); - if (categoryObj != null) { - config.append(new String(new char[tabChange]).replace('\0', ' ')); - for (String l : categoryObj.getComments()) - config.append("# ").append(l).append("\n"); + String categoryStr = category; + String commentKey = null; + if (category.contains(".")) { + String[] split = category.split("\\.", 2); + commentKey = split[1]; + categoryStr = split[0]; + } + + Category categoryObj = getCategory(categoryStr); + if (categoryObj.getComments(commentKey).size() == 0) + throw new NullPointerException(); + for (String l : categoryObj.getComments(commentKey)) { + config.append(new String(new char[tabChange]).replace('\0', ' ')); + config.append("# ").append(l).append("\n"); + } + } catch (IllegalArgumentException | NullPointerException e) { + if (showNullCategoryComments) { + config.append(new String(new char[tabChange]).replace('\0', ' ')); + config.append("# ").append(category).append("\n"); } - } catch (IllegalArgumentException e) { - config.append("# ").append(category).append("\n"); } if (!category.contains(".")) config.append("#").append("\n");