235 lines
7.2 KiB
Java
235 lines
7.2 KiB
Java
package cc.fascinated.utils.io;
|
|
|
|
import lombok.Getter;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
@Getter
|
|
public class Config {
|
|
|
|
private final JavaPlugin plugin;
|
|
private final String configName;
|
|
private final File file;
|
|
private FileConfiguration configuration;
|
|
|
|
public Config(JavaPlugin plugin, String configName, String folderName) {
|
|
this.plugin = plugin;
|
|
this.configName = (folderName == null ? "" : folderName + File.separator) + configName;
|
|
this.file = new File(plugin.getDataFolder(), (folderName == null ? "" : File.separator) + this.configName);
|
|
this.saveDefaultConfig();
|
|
}
|
|
|
|
/**
|
|
* @return {@link #configuration}
|
|
*/
|
|
public FileConfiguration getFileConfiguration() {
|
|
if (this.configuration == null) {
|
|
this.reloadConfig();
|
|
}
|
|
return this.configuration;
|
|
}
|
|
|
|
/**
|
|
* Reload this config file from disk
|
|
*/
|
|
public void reloadConfig() {
|
|
this.configuration = YamlConfiguration.loadConfiguration(this.file);
|
|
InputStream defConfigStream = this.plugin.getResource(this.configName);
|
|
if (defConfigStream == null) {
|
|
throw new NullPointerException("Resource is null for " + configName);
|
|
}
|
|
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream));
|
|
this.configuration.setDefaults(defConfig);
|
|
}
|
|
|
|
/**
|
|
* Save this configuration file to disk
|
|
*/
|
|
public void saveConfig() {
|
|
if (this.configuration != null && this.file != null) {
|
|
try {
|
|
this.getFileConfiguration().save(this.file);
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save the embedded config file shipped with the jar
|
|
*/
|
|
public void saveDefaultConfig() {
|
|
if (!this.file.exists()) {
|
|
this.plugin.saveResource(this.configName, false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the {@link ConfigurationSection} at the provided path
|
|
*
|
|
* @param path the path of the configuration section to fetch
|
|
* @param createIfAbsent whether the configuration section should be created if it is not present
|
|
* @return the configuration section at the provided path
|
|
*/
|
|
public ConfigurationSection getConfigurationSection(String path, boolean createIfAbsent) {
|
|
FileConfiguration file = this.getFileConfiguration();
|
|
ConfigurationSection section = file.getConfigurationSection(path);
|
|
if (section == null && createIfAbsent) {
|
|
section = file.createSection(path);
|
|
}
|
|
return section;
|
|
}
|
|
|
|
/**
|
|
* Set a {@link String} at the provided path
|
|
*
|
|
* @param path path for the node to be placed at
|
|
* @param value value of the node
|
|
* @param save whether the file should be saved after setting
|
|
*/
|
|
public void setString(String path, String value, boolean save) {
|
|
this.getFileConfiguration().set(path, value);
|
|
if (save) {
|
|
this.saveConfig();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the {@link String} at the provided path
|
|
* Returns null if the path is not present,
|
|
*
|
|
* @param path the path to fetch the string at
|
|
* @return the string at the provided path
|
|
* @see #getString(String, boolean, String) for a non null return
|
|
*/
|
|
public String getString(String path) {
|
|
return this.getFileConfiguration().getString(path);
|
|
}
|
|
|
|
/**
|
|
* Return the {@link String} at the provided path
|
|
* If {@param createIfAbsent} is true, {@param defaultValue} will be placed at {@param path} if no value is present already
|
|
* Saves the file if a default value is placed
|
|
*
|
|
* @param path path for the node to be placed at
|
|
* @param createIfAbsent whether a default value should be placed if no value is found
|
|
* @param defaultValue the default value to return and place into the file if desired
|
|
* @return the value at the provided path or the default provided value if applicable
|
|
*/
|
|
public String getString(String path, boolean createIfAbsent, String defaultValue) {
|
|
String value = this.getString(path);
|
|
if (value == null && createIfAbsent) {
|
|
this.setString(path, defaultValue, true);
|
|
return defaultValue;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @see #setString(String, String, boolean)
|
|
*/
|
|
public void setInt(String path, int value, boolean save) {
|
|
this.getFileConfiguration().set(path, value);
|
|
if (save) {
|
|
this.saveConfig();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see #getString(String)
|
|
*/
|
|
public int getInt(String path) {
|
|
return this.getFileConfiguration().getInt(path);
|
|
}
|
|
|
|
/**
|
|
* @see #getString(String, boolean, String)
|
|
*/
|
|
public int getInt(String path, boolean createIfAbsent, int defaultValue) {
|
|
FileConfiguration file = this.getFileConfiguration();
|
|
if (file.contains(path)) {
|
|
return this.getInt(path);
|
|
} else if (createIfAbsent) {
|
|
this.setInt(path, defaultValue, true);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* @see #setString(String, String, boolean)
|
|
*/
|
|
public void setDouble(String path, double value, boolean save) {
|
|
this.getFileConfiguration().set(path, value);
|
|
if (save) {
|
|
this.saveConfig();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see #getString(String)
|
|
*/
|
|
public double getDouble(String path) {
|
|
return this.getFileConfiguration().getDouble(path);
|
|
}
|
|
|
|
/**
|
|
* Default to -1
|
|
*
|
|
* @see #getString(String, boolean, String)
|
|
*/
|
|
public double getDouble(String path, boolean createIfAbsent, double defaultValue) {
|
|
FileConfiguration file = this.getFileConfiguration();
|
|
if (file.contains(path)) {
|
|
return this.getDouble(path);
|
|
} else if (createIfAbsent) {
|
|
this.setDouble(path, defaultValue, true);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* @see #setString(String, String, boolean)
|
|
*/
|
|
public void setBoolean(String path, boolean value, boolean save) {
|
|
this.getFileConfiguration().set(path, value);
|
|
if (save) {
|
|
this.saveConfig();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see #getString(String)
|
|
*/
|
|
public boolean getBoolean(String path) {
|
|
return this.getFileConfiguration().getBoolean(path);
|
|
}
|
|
|
|
/**
|
|
* Default to false
|
|
*
|
|
* @see #getString(String, boolean, String)
|
|
*/
|
|
public boolean getBoolean(String path, boolean createIfAbsent, boolean defaultValue) {
|
|
FileConfiguration file = this.getFileConfiguration();
|
|
if (file.contains(path)) {
|
|
return this.getBoolean(path);
|
|
} else if (createIfAbsent) {
|
|
this.setBoolean(path, defaultValue, true);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Set a value at a path
|
|
*/
|
|
public void set(String path, Object value) {
|
|
this.getFileConfiguration().set(path, value);
|
|
}
|
|
} |