SubServers-2/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Config/YAMLConfig.java

110 lines
2.9 KiB
Java
Raw Normal View History

2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee.Library.Config;
2016-12-05 04:21:04 +01:00
import net.ME1312.SubServers.Bungee.Library.Util;
2016-12-05 04:21:04 +01:00
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
2017-01-08 20:42:40 +01:00
import java.io.*;
2017-11-22 22:58:33 +01:00
import java.util.LinkedHashMap;
2016-12-05 04:21:04 +01:00
2017-01-07 20:06:54 +01:00
/**
* YAML Config Class
*/
2016-12-05 04:21:04 +01:00
@SuppressWarnings("unused")
public class YAMLConfig {
private File file;
private Yaml yaml;
private YAMLSection config;
2017-01-07 20:06:54 +01:00
/**
* Creates/Loads a YAML Formatted Config
*
* @param file
* @throws IOException
* @throws YAMLException
*/
2016-12-05 04:21:04 +01:00
@SuppressWarnings("unchecked")
public YAMLConfig(File file) throws IOException, YAMLException {
if (Util.isNull(file)) throw new NullPointerException();
2017-01-08 20:42:40 +01:00
this.file = file;
this.yaml = new Yaml(getDumperOptions());
2016-12-05 04:21:04 +01:00
if (file.exists()) {
InputStream stream = new FileInputStream(file);
this.config = new YAMLSection((LinkedHashMap<String, ?>) yaml.loadAs(stream, LinkedHashMap.class), null, null, yaml);
stream.close();
2016-12-05 04:21:04 +01:00
} else {
this.config = new YAMLSection(null, null, null, yaml);
}
}
2017-01-07 20:06:54 +01:00
/**
* Get Config Contents
*
* @return Config Contents
*/
2016-12-05 04:21:04 +01:00
public YAMLSection get() {
return config;
}
2017-01-07 20:06:54 +01:00
/**
* Set Config Contents
*
* @param value Value
*/
public void set(YAMLSection value) {
if (Util.isNull(value)) throw new NullPointerException();
2017-01-07 20:06:54 +01:00
config = value;
2016-12-05 04:21:04 +01:00
}
2017-01-07 20:06:54 +01:00
/**
* Reload Config Contents
*
* @throws IOException
*/
2016-12-05 04:21:04 +01:00
@SuppressWarnings("unchecked")
public void reload() throws IOException {
2017-11-22 22:58:33 +01:00
if (file.exists()) {
InputStream stream = new FileInputStream(file);
this.config = new YAMLSection((LinkedHashMap<String, ?>) yaml.loadAs(stream, LinkedHashMap.class), null, null, yaml);
stream.close();
2017-11-22 22:58:33 +01:00
} else {
this.config = new YAMLSection(null, null, null, yaml);
}
2016-12-05 04:21:04 +01:00
}
2017-01-07 20:06:54 +01:00
/**
* Save Config Contents
*
* @throws IOException
*/
2016-12-05 04:21:04 +01:00
public void save() throws IOException {
2017-04-24 19:16:57 +02:00
if (!file.exists()) file.createNewFile();
2016-12-05 04:21:04 +01:00
FileWriter writer = new FileWriter(file);
yaml.dump(config.map, writer);
writer.close();
}
2018-01-05 23:30:01 +01:00
@Override
public boolean equals(Object object) {
if (object instanceof YAMLConfig) {
return get().equals(((YAMLConfig) object).get());
} else {
return super.equals(object);
}
}
2016-12-05 04:21:04 +01:00
@Override
public String toString() {
return yaml.dump(config.map);
}
protected static DumperOptions getDumperOptions() {
DumperOptions options = new DumperOptions();
options.setAllowUnicode(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setIndent(4);
return options;
}
}