forked from circlecloud/MiaoChat
204 lines
6.5 KiB
Java
204 lines
6.5 KiB
Java
//
|
|
// Source code recreated from a .class file by IntelliJ IDEA
|
|
// (powered by FernFlower decompiler)
|
|
//
|
|
|
|
package pw.yumc.YumCore.config;
|
|
|
|
import com.google.common.io.Files;
|
|
import org.apache.commons.lang.Validate;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.yaml.snakeyaml.DumperOptions;
|
|
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
|
import org.yaml.snakeyaml.Yaml;
|
|
import org.yaml.snakeyaml.error.YAMLException;
|
|
import org.yaml.snakeyaml.representer.Representer;
|
|
import pw.yumc.YumCore.bukkit.Log;
|
|
import pw.yumc.YumCore.bukkit.P;
|
|
import pw.yumc.YumCore.config.yaml.BukkitConstructor;
|
|
import pw.yumc.YumCore.config.yaml.BukkitRepresenter;
|
|
|
|
import java.io.*;
|
|
import java.nio.charset.Charset;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
public abstract class AbstractConfig extends YamlConfiguration {
|
|
private static String CONTENT_NOT_BE_NULL = "内容不能为 null";
|
|
protected static Charset UTF_8 = Charset.forName("UTF-8");
|
|
protected static String FILE_NOT_BE_NULL = "文件不能为 NULL";
|
|
protected static String CREATE_NEW_CONFIG = "配置: 创建新的文件 %s ...";
|
|
protected static String newLine = "\n";
|
|
protected static Plugin plugin = P.instance;
|
|
protected DumperOptions yamlOptions = new DumperOptions();
|
|
protected Representer yamlRepresenter;
|
|
protected Yaml yamlz;
|
|
protected Map contentsMap;
|
|
protected String data;
|
|
|
|
public AbstractConfig() {
|
|
this.yamlRepresenter = BukkitRepresenter.DEFAULT;
|
|
this.yamlz = new Yaml(BukkitConstructor.DEFAULT, this.yamlRepresenter, this.yamlOptions);
|
|
}
|
|
|
|
public Map getContentMap() {
|
|
return this.contentsMap;
|
|
}
|
|
|
|
public void load(File file) throws IOException, InvalidConfigurationException {
|
|
Validate.notNull(file, FILE_NOT_BE_NULL);
|
|
FileInputStream stream = new FileInputStream(file);
|
|
this.load((Reader) (new InputStreamReader(stream, UTF_8)));
|
|
}
|
|
|
|
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
|
StringBuilder builder = new StringBuilder();
|
|
BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
|
|
Throwable var4 = null;
|
|
|
|
try {
|
|
String line;
|
|
try {
|
|
while ((line = input.readLine()) != null) {
|
|
builder.append(line);
|
|
builder.append(newLine);
|
|
}
|
|
} catch (Throwable var13) {
|
|
var4 = var13;
|
|
throw var13;
|
|
}
|
|
} finally {
|
|
if (input != null) {
|
|
if (var4 != null) {
|
|
try {
|
|
input.close();
|
|
} catch (Throwable var12) {
|
|
var4.addSuppressed(var12);
|
|
}
|
|
} else {
|
|
input.close();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
this.loadFromString(builder.toString());
|
|
}
|
|
|
|
public void loadFromString(String contents) throws InvalidConfigurationException {
|
|
Validate.notNull(contents, CONTENT_NOT_BE_NULL);
|
|
|
|
try {
|
|
this.contentsMap = (Map) this.yamlz.load(contents);
|
|
} catch (ClassCastException | YAMLException var3) {
|
|
throw new InvalidConfigurationException(var3);
|
|
}
|
|
|
|
String header = this.parseHeader(contents);
|
|
if (header.length() > 0) {
|
|
this.options().header(header);
|
|
}
|
|
|
|
if (this.contentsMap != null) {
|
|
this.convertMapsToSections(this.contentsMap, this);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
protected String parseHeader(String input) {
|
|
String[] lines = input.split("\r?\n", -1);
|
|
StringBuilder result = new StringBuilder();
|
|
boolean readingHeader = true;
|
|
boolean foundHeader = false;
|
|
|
|
for (int i = 0; i < lines.length && readingHeader; ++i) {
|
|
String line = lines[i];
|
|
if (line.startsWith("# ")) {
|
|
if (i > 0) {
|
|
result.append("\n");
|
|
}
|
|
|
|
if (line.length() > "# ".length()) {
|
|
result.append(line.substring("# ".length()));
|
|
}
|
|
|
|
foundHeader = true;
|
|
} else if (foundHeader && line.length() == 0) {
|
|
result.append("\n");
|
|
} else if (foundHeader) {
|
|
readingHeader = false;
|
|
}
|
|
}
|
|
|
|
return result.toString();
|
|
}
|
|
|
|
@Override
|
|
protected void convertMapsToSections(Map<?, ?> input, ConfigurationSection section) {
|
|
Iterator var4 = input.entrySet().iterator();
|
|
|
|
while (var4.hasNext()) {
|
|
Map.Entry<?, ?> entry = (Map.Entry) var4.next();
|
|
String key = entry.getKey().toString();
|
|
Object value = entry.getValue();
|
|
if (value instanceof Map) {
|
|
this.convertMapsToSections((Map) value, section.createSection(key));
|
|
} else {
|
|
section.set(key, value);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void save(File file) throws IOException {
|
|
Validate.notNull(file, FILE_NOT_BE_NULL);
|
|
Files.createParentDirs(file);
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
Log.i(CREATE_NEW_CONFIG, new Object[]{file.toPath()});
|
|
}
|
|
|
|
Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF_8);
|
|
Throwable var3 = null;
|
|
|
|
try {
|
|
writer.write(this.data);
|
|
} catch (Throwable var12) {
|
|
var3 = var12;
|
|
throw var12;
|
|
} finally {
|
|
if (writer != null) {
|
|
if (var3 != null) {
|
|
try {
|
|
writer.close();
|
|
} catch (Throwable var11) {
|
|
var3.addSuppressed(var11);
|
|
}
|
|
} else {
|
|
writer.close();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public String saveToString() {
|
|
this.yamlOptions.setIndent(this.options().indent());
|
|
this.yamlOptions.setDefaultFlowStyle(FlowStyle.BLOCK);
|
|
this.yamlRepresenter.setDefaultFlowStyle(FlowStyle.BLOCK);
|
|
String header = this.buildHeader();
|
|
String dump = this.yamlz.dump(this.getValues(false));
|
|
if (dump.equals("{}\n")) {
|
|
dump = "";
|
|
}
|
|
|
|
this.data = header + dump;
|
|
return this.data;
|
|
}
|
|
}
|