add static config accessor class, add more config methods

This commit is contained in:
jascotty2 2019-08-30 15:53:44 -05:00
parent 167e8c0faf
commit 147f43b38a
3 changed files with 198 additions and 8 deletions

View File

@ -60,7 +60,8 @@ public class Config extends SongodaConfigurationSection {
protected static final String COMMENT_PREFIX = "# ";
protected static final String BLANK_CONFIG = "{}\n";
final File file;
protected File file;
final String dirName, fileName;
final Plugin plugin;
final DumperOptions yamlOptions = new DumperOptions();
final Representer yamlRepresenter = new YamlRepresenter();
@ -106,24 +107,36 @@ public class Config extends SongodaConfigurationSection {
public Config(@NotNull File file) {
this.plugin = null;
this.file = file.getAbsoluteFile();
dirName = null;
fileName = null;
}
public Config(@NotNull Plugin plugin) {
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), "config.yml");
dirName = null;
fileName = null;
}
public Config(@NotNull Plugin plugin, @NotNull String file) {
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), file);
dirName = null;
fileName = file;
}
public Config(@NotNull Plugin plugin, @NotNull String directory, @NotNull String file) {
this.plugin = plugin;
this.file = new File(plugin.getDataFolder() + directory, file);
dirName = directory;
fileName = file;
}
public File getFile() {
if (file == null) {
if (dirName != null) {
this.file = new File(plugin.getDataFolder() + dirName, fileName != null ? fileName : "config.yml");
} else {
this.file = new File(plugin.getDataFolder(), fileName != null ? fileName : "config.yml");
}
}
return file;
}
@ -285,8 +298,7 @@ public class Config extends SongodaConfigurationSection {
}
public void load() throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
FileInputStream stream = new FileInputStream(file);
FileInputStream stream = new FileInputStream(getFile());
this.load(new InputStreamReader((InputStream) stream, Charsets.UTF_16));
}
@ -363,7 +375,7 @@ public class Config extends SongodaConfigurationSection {
public void delaySave() {
// save async even if no plugin or if plugin disabled
if (changed && saveTask == null) {
autosaveTimer = new Timer((plugin != null ? plugin.getName() + "-ConfigSave-" : "ConfigSave-") + file.getName());
autosaveTimer = new Timer((plugin != null ? plugin.getName() + "-ConfigSave-" : "ConfigSave-") + getFile().getName());
autosaveTimer.schedule(saveTask = new SaveTask(), autosaveInterval * 1000L);
}
}
@ -391,7 +403,7 @@ public class Config extends SongodaConfigurationSection {
saveTask = null;
autosaveTimer = null;
}
return save(file);
return save(getFile());
}
public boolean save(@NotNull String file) {

View File

@ -0,0 +1,127 @@
package com.songoda.core.settingsv2;
import com.songoda.core.compatibility.LegacyMaterials;
import java.util.List;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ConfigSetting {
final Config config;
final String key;
public ConfigSetting(@NotNull Config config, @NotNull String key) {
this.config = config;
this.key = key;
}
public ConfigSetting(@NotNull Config config, @NotNull String key, @NotNull Object defaultValue, String ... comment) {
this.config = config;
this.key = key;
config.setDefault(key, defaultValue, comment);
}
public ConfigSetting(@NotNull Config config, @NotNull String key, @NotNull Object defaultValue, ConfigFormattingRules.CommentStyle commentStyle, String ... comment) {
this.config = config;
this.key = key;
config.setDefault(key, defaultValue, commentStyle, comment);
}
@NotNull
public String getKey() {
return key;
}
public List<Integer> getIntegerList() {
return config.getIntegerList(key);
}
public List<String> getStringList() {
return config.getStringList(key);
}
public boolean getBoolean() {
return config.getBoolean(key);
}
public boolean getBoolean(boolean def) {
return config.getBoolean(key, def);
}
public int getInt() {
return config.getInt(key);
}
public int getInt(int def) {
return config.getInt(key, def);
}
public long getLong() {
return config.getLong(key);
}
public long getLong(long def) {
return config.getLong(key, def);
}
public double getDouble() {
return config.getDouble(key);
}
public double getDouble(double def) {
return config.getDouble(key, def);
}
public String getString() {
return config.getString(key);
}
public String getString(String def) {
return config.getString(key, def);
}
public Object getObject() {
return config.get(key);
}
public Object getObject(Object def) {
return config.get(key, def);
}
public <T> T getObject(@NotNull Class<T> clazz) {
return config.getObject(key, clazz);
}
public <T> T getObject(@NotNull Class<T> clazz, @Nullable T def) {
return config.getObject(key, clazz, def);
}
public char getChar() {
return config.getChar(key);
}
public char getChar(char def) {
return config.getChar(key, def);
}
@NotNull
public Material getMaterial() {
Material m = getMaterial(null);
return m != null ? m : Material.STONE;
}
@Nullable
public Material getMaterial(@Nullable LegacyMaterials def) {
//return config.getMaterial(key, def);
String val = config.getString(key);
LegacyMaterials mat = val != null ? LegacyMaterials.getMaterial(val) : null;
if (mat == null) {
System.out.println(String.format("Config value \"%s\" has an invalid material name: \"%s\"", key, val));
}
return mat != null ? mat.getMaterial() : (def != null ? def.getMaterial() : null);
}
}

View File

@ -1,5 +1,6 @@
package com.songoda.core.settingsv2;
import com.songoda.core.compatibility.LegacyMaterials;
import com.songoda.core.settingsv2.adapters.ConfigDefaultsAdapter;
import com.songoda.core.settingsv2.adapters.ConfigOptionsAdapter;
import java.util.Arrays;
@ -11,6 +12,7 @@ import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bukkit.Material;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.MemoryConfiguration;
import org.jetbrains.annotations.NotNull;
@ -139,6 +141,19 @@ public class SongodaConfigurationSection extends MemoryConfiguration {
return this;
}
@NotNull
public SongodaConfigurationSection setDefaultComment(@NotNull String path, ConfigFormattingRules.CommentStyle commentStyle, String... lines) {
return setDefaultComment(path, commentStyle, lines.length == 0 ? (List) null : Arrays.asList(lines));
}
@NotNull
public SongodaConfigurationSection setDefaultComment(@NotNull String path, ConfigFormattingRules.CommentStyle commentStyle, @Nullable List<String> lines) {
synchronized (root.lock) {
root.defaultComments.put(fullPath + path, new Comment(commentStyle, lines));
}
return this;
}
@Nullable
public Comment getComment(@NotNull String path) {
Comment result = root.configComments.get(fullPath + path);
@ -364,6 +379,18 @@ public class SongodaConfigurationSection extends MemoryConfiguration {
return setDefaultComment(path, comment);
}
@NotNull
public SongodaConfigurationSection setDefault(@NotNull String path, @Nullable Object value, ConfigFormattingRules.CommentStyle commentStyle, String ... comment) {
addDefault(path, value);
return setDefaultComment(path, commentStyle, comment);
}
@NotNull
public SongodaConfigurationSection setDefault(@NotNull String path, @Nullable Object value, ConfigFormattingRules.CommentStyle commentStyle, List<String> comment) {
addDefault(path, value);
return setDefaultComment(path, commentStyle, comment);
}
@NotNull
@Override
public SongodaConfigurationSection createSection(@NotNull String path) {
@ -436,6 +463,16 @@ public class SongodaConfigurationSection extends MemoryConfiguration {
return result != null ? result.toString() : def;
}
public char getChar(@NotNull String path) {
Object result = get(path);
return result != null && !result.toString().isEmpty() ? result.toString().charAt(0) : '\0';
}
public char getChar(@NotNull String path, char def) {
Object result = get(path);
return result != null && !result.toString().isEmpty() ? result.toString().charAt(0) : def;
}
@Override
public int getInt(@NotNull String path) {
Object result = get(path);
@ -498,6 +535,20 @@ public class SongodaConfigurationSection extends MemoryConfiguration {
return result instanceof List ? (List) result : def;
}
@Nullable
public Material getMaterial(@NotNull String path) {
String val = getString(path);
LegacyMaterials mat = val != null ? LegacyMaterials.getMaterial(val) : null;
return mat != null ? mat.getMaterial() : null;
}
@Nullable
public Material getMaterial(@NotNull String path, @Nullable LegacyMaterials def) {
String val = getString(path);
LegacyMaterials mat = val != null ? LegacyMaterials.getMaterial(val) : null;
return mat != null ? mat.getMaterial() : (def != null ? def.getMaterial() : null);
}
@Nullable
@Override
public <T> T getObject(@NotNull String path, @NotNull Class<T> clazz) {