This commit is contained in:
Brianna 2019-08-24 03:02:05 -04:00
parent 0ec3530cc9
commit 3fd34357f6
7 changed files with 355 additions and 87 deletions

View File

@ -1,55 +1,92 @@
package com.songoda.core.library.settings;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class Category {
public class Category extends Narrow {
private final Config config;
private final String key;
private final String[] comments;
private final Map<String, Setting> settings = new HashMap<>();
protected final Map<String, FoundSetting> defaultSettings = new LinkedHashMap<>();
private final List<String> comments = new ArrayList<>();
public Category(Config config, String key, String... comments) {
this.config = config;
this.key = key;
this.comments = comments;
if (comments != null)
this.comments.addAll(Arrays.asList(comments));
}
public Category(Config config, String key) {
this(config, key, null);
}
public Category addSetting(String key, Object defaultValue, String... comments) {
this.settings.put(key, new Setting(this, key, defaultValue, comments));
public Category addAll(Category category) {
addSettings(category);
if (comments.size() == 0)
addComments(category.getComments());
return this;
}
public Set<Setting> getSettings() {
return new HashSet<>(settings.values());
public Category addSetting(String key, Object defaultValue, String... comments) {
addSetting(new FoundSetting(this, key, defaultValue, comments));
return this;
}
public Setting getSetting(String setting) {
for (String string : settings.keySet())
if (string.equalsIgnoreCase(setting))
return settings.get(string);
return null;
public Category addDefaultSetting(String key, Object defaultValue, String... comments) {
addDefaultSetting(new FoundSetting(this, key, defaultValue, comments));
return this;
}
public Category addDefaultSetting(FoundSetting setting) {
this.defaultSettings.put(setting.getKey(), setting);
return this;
}
public Category addSettings(FoundSetting... settings) {
for (FoundSetting setting : settings)
this.settings.put(setting.getKey(), setting);
return this;
}
public Category addSettings(Category category) {
for (FoundSetting setting : category.getSettings())
this.settings.put(setting.getKey(), setting);
return this;
}
public Setting getDefaultSetting(String setting) {
for (String string : defaultSettings.keySet())
if (string.equalsIgnoreCase(setting))
return defaultSettings.get(string);
return new Setting();
}
public List<FoundSetting> getDefaultSettings() {
return new ArrayList<>(defaultSettings.values());
}
public String getKey() {
return key;
}
public void addComments(List<String> commments) {
this.comments.addAll(commments);
}
public String[] getComments() {
return comments;
public List<String> getComments() {
return Collections.unmodifiableList(comments);
}
public Config getConfig() {
return config;
}
@Override
public String toString() {
return key;
}
}

View File

@ -6,10 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class Config {
@ -19,16 +16,68 @@ public class Config {
private FileConfiguration fileConfiguration;
private File configFile;
private final Map<String, Category> categories = new HashMap<>();
private boolean allowUserExpansion, categorySpacing, commentSpacing = true;
private final Map<String, Category> categories = new LinkedHashMap<>();
public Config(JavaPlugin plugin, String folderName, String fileName) {
this.plugin = plugin;
this.folderName = folderName;
this.fileName = fileName;
this.reload();
}
public Config(JavaPlugin plugin, String fileName) {
this(plugin, "", fileName);
}
/**
* 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.
*
* @param allowUserExpansion allow users to expand config, otherwise don't
* @return this class
*/
public Config allowUserExpansion(boolean allowUserExpansion) {
this.allowUserExpansion = allowUserExpansion;
return this;
}
/**
* This will add two spaces above each category.
*
* @param categorySpacing add two spaces above each category, otherwise don't
* @return this class
*/
public Config categorySpacing(boolean categorySpacing) {
this.categorySpacing = categorySpacing;
return this;
}
/**
* This will add a single space above each commented setting. Useful when
* you don't want your comments to stand out.
*
* @param commentSpacing add a space above each comment, otherwise don't.
* @return this class
*/
public Config commentSpacing(boolean commentSpacing) {
this.commentSpacing = commentSpacing;
return this;
}
public Category addCategory(String key, String... comments) {
return categories.put(key, new Category(this, key, comments));
return addCategory(new Category(this, key, comments));
}
public Category addCategory(Category category) {
if (categories.containsKey(category.getKey()))
return categories.get(category.getKey()).addAll(category);
else {
categories.put(category.getKey(), category);
return category;
}
}
public Category getCategory(String key) {
@ -38,46 +87,86 @@ public class Config {
return null;
}
public boolean hasCategory(String key) {
return getCategory(key) != null;
}
public Setting getSetting(String key) {
String[] split = key.split(".", 2);
String[] split = key.split("\\.", 2);
if (split.length != 2) return null;
Category category = getCategory(split[0]);
if (category == null) return null;
return category.getSetting(split[1]);
}
public Set<Setting> getSettings() {
Set<Setting> settings = new HashSet<>();
public Setting getDefaultSetting(String key) {
String[] split = key.split("\\.", 2);
if (split.length != 2) return null;
Category category = getCategory(split[0]);
if (category == null) return null;
return category.getDefaultSetting(split[1]);
}
public List<FoundSetting> getSettings() {
List<FoundSetting> settings = new ArrayList<>();
for (Category category : categories.values()) {
settings.addAll(category.getSettings());
}
return settings;
}
public void reload() {
if (configFile == null) {
configFile = new File(plugin.getDataFolder() + folderName, fileName);
public List<FoundSetting> getDefaultSettings() {
List<FoundSetting> settings = new ArrayList<>();
for (Category category : categories.values()) {
settings.addAll(category.getDefaultSettings());
}
fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
this.setup();
return settings;
}
private void loadExisting() {
this.categories.clear();
for (String categoryStr : fileConfiguration.getKeys(false)) {
Category category = new Category(this, categoryStr);
for (String settingStr : fileConfiguration.getConfigurationSection(categoryStr).getKeys(true)) {
category.addSetting(settingStr, fileConfiguration.get(categoryStr + "." + settingStr));
}
addCategory(category);
}
}
public void reload() {
if (this.configFile == null)
this.configFile = new File(plugin.getDataFolder() + folderName, fileName);
this.fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
if (allowUserExpansion)
this.loadExisting();
}
public void setup() {
FileConfiguration config = plugin.getConfig();
for (Setting setting : getSettings()) {
config.addDefault(setting.getCompleteKey(), setting.getDefaultValue());
if (fileConfiguration.getKeys(false).size() == 0 || !allowUserExpansion) {
for (FoundSetting setting : getDefaultSettings()) {
fileConfiguration.addDefault(setting.getCompleteKey(), setting.getDefaultValue());
setting.getCategory().addSetting(setting);
}
}
plugin.getConfig().options().copyDefaults(true);
fileConfiguration.options().copyDefaults(true);
save();
}
void save() {
public void save() {
// Delete old config values.
for (String line : fileConfiguration.getConfigurationSection("").getKeys(true)) {
if (line.contains(".") && getSetting(line) == null)
fileConfiguration.set(line, null);
else if (!line.contains(".")) {
if (((MemorySection) fileConfiguration.get(line)).getKeys(true).size() == 0)
if (!allowUserExpansion) {
for (String line : fileConfiguration.getKeys(true)) {
if (line.contains(".") && getDefaultSetting(line) == null)
fileConfiguration.set(line, null);
else if (!line.contains(".")) {
if (((MemorySection) fileConfiguration.get(line)).getKeys(true).size() == 0)
fileConfiguration.set(line, null);
}
}
}
@ -119,7 +208,8 @@ public class Config {
currentTab = tabChange + 2;
if (!first) {
config.append("\n\n");
if (categorySpacing)
config.append("\n\n");
} else {
first = false;
}
@ -128,10 +218,11 @@ public class Config {
config.append("#").append("\n");
try {
Category categoryObj = getCategory(category);
config.append(new String(new char[tabChange]).replace('\0', ' '));
for (String l : categoryObj.getComments())
config.append("# ").append(l).append("\n");
if (categoryObj != null) {
config.append(new String(new char[tabChange]).replace('\0', ' '));
for (String l : categoryObj.getComments())
config.append("# ").append(l).append("\n");
}
} catch (IllegalArgumentException e) {
config.append("# ").append(category).append("\n");
}
@ -150,9 +241,10 @@ public class Config {
}
String key = category + "." + (line.split(":")[0].trim());
for (Setting setting : getSettings()) {
if (!setting.getCompleteKey().equals(key) || setting.getComments() == null) continue;
config.append(" ").append("\n");
for (FoundSetting setting : getSettings()) {
if (!setting.getCompleteKey().equals(key) || setting.getComments().length == 0) continue;
if (commentSpacing)
config.append(" ").append("\n");
for (String l : setting.getComments()) {
config.append(new String(new char[currentTab]).replace('\0', ' '));
config.append("# ").append(l).append("\n");
@ -167,8 +259,7 @@ public class Config {
try {
if (!plugin.getDataFolder().exists())
plugin.getDataFolder().mkdir();
BufferedWriter writer =
new BufferedWriter(new FileWriter(new File(plugin.getDataFolder() + File.separator + "config.yml")));
BufferedWriter writer = new BufferedWriter(new FileWriter(configFile));
writer.write(config.toString());
writer.flush();
writer.close();

View File

@ -0,0 +1,49 @@
package com.songoda.core.library.settings;
import org.bukkit.configuration.file.FileConfiguration;
public class FoundSetting extends Setting {
private final Category category;
private final String key;
private final Object defaultValue;
private final String[] comments;
public FoundSetting(Category category, String key, Object defaultValue, String... comments) {
this.category = category;
this.key = key;
this.defaultValue = defaultValue;
this.comments = comments;
}
@Override
public String getKey() {
return key;
}
@Override
public String getCompleteKey() {
return category.getKey() + "." + key;
}
public Object getDefaultValue() {
return defaultValue;
}
public String[] getComments() {
if (comments.length == 0 && category.getDefaultSetting(key) != null
&& category.getDefaultSetting(key) instanceof FoundSetting)
return ((FoundSetting)category.getDefaultSetting(key)).comments;
return comments;
}
@Override
public FileConfiguration getConfig() {
return category.getConfig().getFileConfiguration();
}
public Category getCategory() {
return category;
}
}

View File

@ -0,0 +1,52 @@
package com.songoda.core.library.settings;
import java.util.*;
public class Narrow {
protected final Map<String, FoundSetting> settings = new LinkedHashMap<>();
public Narrow() {
}
public Narrow(Set<FoundSetting> settings) {
for (FoundSetting setting : settings)
this.settings.put(setting.getKey(), setting);
}
public Narrow narrow(String key) {
Set<FoundSetting> settings = new HashSet<>();
for (FoundSetting setting : this.settings.values()) {
if (setting.getKey().startsWith(key))
settings.add(setting);
}
return new Narrow(settings);
}
public Collection<Section> getSection() {
Map<String, Section> sections = new HashMap<>();
for (FoundSetting setting : settings.values()) {
String section = setting.getKey().contains(".") ? setting.getKey().split("\\.")[0] : setting.getKey();
if (!sections.containsKey(section))
sections.put(section, new Section(section));
sections.get(section).getNarrow().addSetting(setting);
}
return sections.values();
}
public void addSetting(FoundSetting setting) {
this.settings.put(setting.getKey(), setting);
}
public List<FoundSetting> getSettings() {
return new ArrayList<>(settings.values());
}
public Setting getSetting(String setting) {
for (String string : settings.keySet())
if (string.equalsIgnoreCase(setting))
return settings.get(string);
return new Setting();
}
}

View File

@ -0,0 +1,23 @@
package com.songoda.core.library.settings;
public class Section {
private final String key;
private final Narrow narrow = new Narrow();
public Section(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public Narrow getNarrow() {
return narrow;
}
public Setting narrow(String setting) {
return narrow.getSetting(key + "." + setting);
}
}

View File

@ -3,72 +3,88 @@ package com.songoda.core.library.settings;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.ArrayList;
import java.util.List;
public class Setting {
private final Category category;
private final String key;
private final Object defaultValue;
private final String comments[];
public Setting(Category category, String key, Object defaultValue, String... comments) {
this.category = category;
this.key = key;
this.defaultValue = defaultValue;
this.comments = comments;
}
public String getKey() {
return key;
return "";
}
public String getCompleteKey() {
return category.getKey() + "." + key;
}
public Object getDefaultValue() {
return defaultValue;
}
public String[] getComments() {
return comments;
return "";
}
public List<Integer> getIntegerList() {
if (getConfig() == null) return new ArrayList<>();
return getConfig().getIntegerList(getCompleteKey());
}
public List<String> getStringList() {
if (getConfig() == null) return new ArrayList<>();
return getConfig().getStringList(getCompleteKey());
}
public boolean getBoolean() {
return getConfig().getBoolean(getCompleteKey());
return getBoolean(false);
}
public boolean getBoolean(boolean def) {
if (getConfig() == null) return def;
return getConfig().getBoolean(getCompleteKey(), def);
}
public int getInt() {
return getConfig().getInt(getCompleteKey());
return getInt(0);
}
public int getInt(int def) {
if (getConfig() == null) return def;
return getConfig().getInt(getCompleteKey(), def);
}
public long getLong() {
return getConfig().getLong(getCompleteKey());
return getLong(0L);
}
public long getLong(long def) {
if (getConfig() == null) return def;
return getConfig().getLong(getCompleteKey(), def);
}
public double getDouble() {
return getDouble(0D);
}
public double getDouble(double def) {
if (getConfig() == null) return def;
return getConfig().getDouble(getCompleteKey(), def);
}
public String getString() {
return getString(null);
}
public String getString(String def) {
if (getConfig() == null) return def;
return getConfig().getString(getCompleteKey());
}
public char getChar() {
return getConfig().getString(getCompleteKey()).charAt(0);
return getChar('0');
}
public double getDouble() {
return getConfig().getDouble(getCompleteKey());
public char getChar(char def) {
if (getConfig() == null) return def;
return getConfig().getString(getCompleteKey()).charAt(def);
}
public Material getMaterial() {
return getMaterial(Material.STONE);
}
public Material getMaterial(Material def) {
String materialStr = getConfig().getString(getCompleteKey());
Material material = Material.getMaterial(materialStr);
@ -76,10 +92,10 @@ public class Setting {
System.out.println(String.format("Config value \"%s\" has an invalid material name: \"%s\"", getCompleteKey(), materialStr));
}
return material;
return material != null ? material : def;
}
public FileConfiguration getConfig() {
return category.getConfig().getFileConfiguration();
return null;
}
}

View File

@ -160,10 +160,10 @@ public class SettingsManager implements Listener {
Setting setting = this.config.getSetting(fKey);
if (setting != null && setting.getComments() != null) {
if (setting instanceof FoundSetting && ((FoundSetting)setting).getComments() != null) {
lore.add("");
String comment = String.join(" ", setting.getComments());
String comment = String.join(" ", ((FoundSetting)setting).getComments());
int lastIndex = 0;
for (int n = 0; n < comment.length(); n++) {