valueEntry : map.get(entryValues.getKey()).entrySet()) {
+ valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
+ allSkipped = false;
+ }
+ if (!allSkipped) {
+ reallyAllSkipped = false;
+ valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
+ }
+ }
+ if (reallyAllSkipped) {
+ // Null = skip the chart
+ return null;
+ }
+ return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
+ }
+ }
+
+ /**
+ * An extremely simple JSON builder.
+ *
+ * While this class is neither feature-rich nor the most performant one, it's sufficient enough
+ * for its use-case.
+ */
+ public static class JsonObjectBuilder {
+
+ private StringBuilder builder = new StringBuilder();
+
+ private boolean hasAtLeastOneField = false;
+
+ public JsonObjectBuilder() {
+ builder.append("{");
+ }
+
+ /**
+ * Appends a null field to the JSON.
+ *
+ * @param key The key of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendNull(String key) {
+ appendFieldUnescaped(key, "null");
+ return this;
+ }
+
+ /**
+ * Appends a string field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String value) {
+ if (value == null) {
+ throw new IllegalArgumentException("JSON value must not be null");
+ }
+ appendFieldUnescaped(key, "\"" + escape(value) + "\"");
+ return this;
+ }
+
+ /**
+ * Appends an integer field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int value) {
+ appendFieldUnescaped(key, String.valueOf(value));
+ return this;
+ }
+
+ /**
+ * Appends an object to the JSON.
+ *
+ * @param key The key of the field.
+ * @param object The object.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject object) {
+ if (object == null) {
+ throw new IllegalArgumentException("JSON object must not be null");
+ }
+ appendFieldUnescaped(key, object.toString());
+ return this;
+ }
+
+ /**
+ * Appends a string array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The string array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values)
+ .map(value -> "\"" + escape(value) + "\"")
+ .collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an integer array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an object array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends a field to the object.
+ *
+ * @param key The key of the field.
+ * @param escapedValue The escaped value of the field.
+ */
+ private void appendFieldUnescaped(String key, String escapedValue) {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ if (key == null) {
+ throw new IllegalArgumentException("JSON key must not be null");
+ }
+ if (hasAtLeastOneField) {
+ builder.append(",");
+ }
+ builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
+ hasAtLeastOneField = true;
+ }
+
+ /**
+ * Builds the JSON string and invalidates this builder.
+ *
+ * @return The built JSON string.
+ */
+ public JsonObject build() {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ JsonObject object = new JsonObject(builder.append("}").toString());
+ builder = null;
+ return object;
+ }
+
+ /**
+ * Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
+ *
+ *
This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
+ * Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
+ *
+ * @param value The value to escape.
+ * @return The escaped value.
+ */
+ private static String escape(String value) {
+ final StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < value.length(); i++) {
+ char c = value.charAt(i);
+ if (c == '"') {
+ builder.append("\\\"");
+ } else if (c == '\\') {
+ builder.append("\\\\");
+ } else if (c <= '\u000F') {
+ builder.append("\\u000").append(Integer.toHexString(c));
+ } else if (c <= '\u001F') {
+ builder.append("\\u00").append(Integer.toHexString(c));
+ } else {
+ builder.append(c);
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * A super simple representation of a JSON object.
+ *
+ *
This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
+ * allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
+ * JsonObject)}.
+ */
+ public static class JsonObject {
+
+ private final String value;
+
+ private JsonObject(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public String toString() {
+ return value;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java
new file mode 100644
index 0000000..5a057ef
--- /dev/null
+++ b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java
@@ -0,0 +1,190 @@
+//
+// Source code recreated from a .class file by IntelliJ IDEA
+// (powered by FernFlower decompiler)
+//
+
+package org.bukkit.configuration.file;
+
+import org.apache.commons.lang.Validate;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.Configuration;
+import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.configuration.InvalidConfigurationException;
+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 java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.logging.Level;
+
+public class YamlConfiguration extends FileConfiguration {
+ protected static final String COMMENT_PREFIX = "# ";
+ protected static final String BLANK_CONFIG = "{}\n";
+ private final DumperOptions yamlOptions = new DumperOptions();
+ private final Representer yamlRepresenter = new YamlRepresenter();
+ private final Yaml yaml;
+
+ public YamlConfiguration() {
+ this.yaml = new Yaml(new YamlConstructor(), this.yamlRepresenter, this.yamlOptions);
+ }
+
+ 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.yaml.dump(this.getValues(false));
+ if (dump.equals("{}\n")) {
+ dump = "";
+ }
+
+ return header + dump;
+ }
+
+ public void loadFromString(String contents) throws InvalidConfigurationException {
+ Validate.notNull(contents, "Contents cannot be null");
+
+ Map input;
+ try {
+ input = (Map) this.yaml.load(contents);
+ } catch (YAMLException var4) {
+ throw new InvalidConfigurationException(var4);
+ } catch (ClassCastException var5) {
+ throw new InvalidConfigurationException("Top level is not a Map.");
+ }
+
+ String header = this.parseHeader(contents);
+ if (header.length() > 0) {
+ this.options().header(header);
+ }
+
+ if (input != null) {
+ this.convertMapsToSections(input, this);
+ }
+
+ }
+
+ protected void convertMapsToSections(Map, ?> input, ConfigurationSection section) {
+ Iterator var4 = input.entrySet().iterator();
+
+ while (var4.hasNext()) {
+ Entry, ?> entry = (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);
+ }
+ }
+
+ }
+
+ 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();
+ }
+
+ protected String buildHeader() {
+ String header = this.options().header();
+ if (this.options().copyHeader()) {
+ Configuration def = this.getDefaults();
+ if (def != null && def instanceof FileConfiguration) {
+ FileConfiguration filedefaults = (FileConfiguration) def;
+ String defaultsHeader = filedefaults.buildHeader();
+ if (defaultsHeader != null && defaultsHeader.length() > 0) {
+ return defaultsHeader;
+ }
+ }
+ }
+
+ if (header == null) {
+ return "";
+ } else {
+ StringBuilder builder = new StringBuilder();
+ String[] lines = header.split("\r?\n", -1);
+ boolean startedHeader = false;
+
+ for (int i = lines.length - 1; i >= 0; --i) {
+ builder.insert(0, "\n");
+ if (startedHeader || lines[i].length() != 0) {
+ builder.insert(0, lines[i]);
+ builder.insert(0, "# ");
+ startedHeader = true;
+ }
+ }
+
+ return builder.toString();
+ }
+ }
+
+ public YamlConfigurationOptions options() {
+ if (this.options == null) {
+ this.options = new YamlConfigurationOptions(this);
+ }
+
+ return (YamlConfigurationOptions) this.options;
+ }
+
+ public static YamlConfiguration loadConfiguration(File file) {
+ Validate.notNull(file, "File cannot be null");
+ YamlConfiguration config = new YamlConfiguration();
+
+ try {
+ config.load(file);
+ } catch (FileNotFoundException var3) {
+ } catch (IOException var4) {
+ Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, var4);
+ } catch (InvalidConfigurationException var5) {
+ Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, var5);
+ }
+
+ return config;
+ }
+
+ public static YamlConfiguration loadConfiguration(Reader reader) {
+ Validate.notNull(reader, "Stream cannot be null");
+ YamlConfiguration config = new YamlConfiguration();
+
+ try {
+ config.load(reader);
+ } catch (IOException var3) {
+ Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", var3);
+ } catch (InvalidConfigurationException var4) {
+ Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", var4);
+ }
+
+ return config;
+ }
+}
diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java
index 3cf5944..4e8ddbf 100644
--- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java
+++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java
@@ -1,21 +1,21 @@
package pw.yumc.MiaoChat.config;
+import org.bukkit.entity.Player;
+import pw.yumc.YumCore.bukkit.Log;
+import pw.yumc.YumCore.bukkit.P;
+import pw.yumc.YumCore.config.AbstractConfig;
+import pw.yumc.YumCore.config.FileConfig;
+
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
-import org.bukkit.entity.Player;
-
-import pw.yumc.YumCore.bukkit.Log;
-import pw.yumc.YumCore.bukkit.P;
-import pw.yumc.YumCore.config.FileConfig;
-
/**
* @author 喵♂呜
* @since 2016年9月9日 下午4:40:50
*/
-public class ChatConfig {
+public class ChatConfig extends AbstractConfig {
private static String F = "Formats";
private Map formats;
private LinkedList rules;
@@ -48,7 +48,9 @@ public class ChatConfig {
*/
public ChatRule getChatRule(Player player) {
for (ChatRule cr : rules) {
- if (cr.check(player)) { return cr; }
+ if (cr.check(player)) {
+ return cr;
+ }
}
return null;
}
diff --git a/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java
new file mode 100644
index 0000000..206bec3
--- /dev/null
+++ b/src/main/java/pw/yumc/YumCore/config/AbstractConfig.java
@@ -0,0 +1,203 @@
+//
+// 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;
+ }
+}