More config stuff

This commit is contained in:
Brianna 2019-08-24 14:11:23 -04:00
parent 3fd34357f6
commit 4e5f8ddcda
2 changed files with 54 additions and 20 deletions

View File

@ -10,13 +10,13 @@ public class Category extends Narrow {
protected final Map<String, FoundSetting> defaultSettings = new LinkedHashMap<>();
private final List<String> comments = new ArrayList<>();
private final Map<String, List<String>> 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<String> commments) {
this.comments.addAll(commments);
public Category addComment(String key, String... comments) {
this.comments.put(key, Arrays.asList(comments));
return this;
}
public List<String> getComments() {
return Collections.unmodifiableList(comments);
public List<String> getComments(String key) {
return Collections.unmodifiableList(comments.get(key));
}
public Map<String, List<String>> getAllComments() {
return Collections.unmodifiableMap(comments);
}
public Config getConfig() {

View File

@ -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<String, Category> 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");