|
27 | 27 | */
|
28 | 28 | public class BukkitConfiguration extends YamlConfiguration {
|
29 | 29 |
|
30 |
| - private static final Field yamlOptionsField; |
31 |
| - private static final Field yamlRepresenterField; |
32 |
| - private static final Field yamlField; |
33 |
| - |
34 |
| - static { |
35 |
| - try { |
36 |
| - yamlOptionsField = YamlConfiguration.class.getDeclaredField("yamlOptions"); |
37 |
| - yamlOptionsField.setAccessible(true); |
38 |
| - |
39 |
| - yamlRepresenterField = YamlConfiguration.class.getDeclaredField("yamlRepresenter"); |
40 |
| - yamlRepresenterField.setAccessible(true); |
41 |
| - |
42 |
| - yamlField = YamlConfiguration.class.getDeclaredField("yaml"); |
43 |
| - yamlField.setAccessible(true); |
44 |
| - } catch (NoSuchFieldException e) { |
45 |
| - throw new InvalidConfigException("Could not find Fields of YamlConfiguration", e); |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 | 30 | /**
|
50 | 31 | * Properties of config
|
51 | 32 | */
|
52 | 33 | private final File file;
|
53 | 34 | private final NameStyle nameStyle;
|
54 | 35 | private final CommentStyle commentStyle;
|
55 | 36 | private final boolean automaticColorStrings;
|
| 37 | + private final String configComment; |
56 | 38 |
|
57 | 39 | /**
|
58 | 40 | * Caches
|
59 | 41 | */
|
60 | 42 | private final Map<String, Object> cache;
|
61 | 43 | private final Map<String, String> comments;
|
62 | 44 |
|
63 |
| - /** |
64 |
| - * Internal objects from YamlConfiguration, needed for handling comments |
65 |
| - * @see YamlConfiguration |
66 |
| - */ |
67 |
| - private final DumperOptions yamlOptions; |
68 |
| - private final YamlRepresenter yamlRepresenter; |
69 |
| - private final Yaml yaml; |
70 |
| - |
71 |
| - public BukkitConfiguration(File file, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings) { |
| 45 | + public BukkitConfiguration(File file, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings, String configComment) { |
72 | 46 | this.file = file;
|
73 | 47 | this.nameStyle = nameStyle;
|
74 | 48 | this.commentStyle = commentStyle;
|
75 | 49 | this.automaticColorStrings = automaticColorStrings;
|
76 | 50 | this.cache = new HashMap<>();
|
77 | 51 | this.comments = new HashMap<>();
|
78 |
| - |
79 |
| - try { |
80 |
| - this.yamlOptions = (DumperOptions) yamlOptionsField.get(this); |
81 |
| - this.yamlRepresenter = (YamlRepresenter) yamlRepresenterField.get(this); |
82 |
| - this.yaml = (Yaml) yamlField.get(this); |
83 |
| - } catch (IllegalAccessException e) { |
84 |
| - throw new InvalidConfigException("Could not get values of Fields in YamlConfiguration", e); |
85 |
| - } |
| 52 | + this.configComment = configComment; |
86 | 53 |
|
87 | 54 | this.copyDefaultConfig();
|
88 | 55 | this.load();
|
@@ -138,40 +105,38 @@ public Object get(String path) {
|
138 | 105 |
|
139 | 106 | /**
|
140 | 107 | * Workaround for writing comments to .yml file, Bukkit does to allow to do it
|
141 |
| - * @return yml content as String |
| 108 | + * @return Content of config parsed to YAML |
142 | 109 | */
|
143 | 110 | @Override
|
144 | 111 | public String saveToString() {
|
145 |
| - this.yamlOptions.setIndent(this.options().indent()); |
146 |
| - this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
147 |
| - this.yamlOptions.setAllowUnicode(true); |
148 |
| - this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
149 |
| - |
150 |
| - String header = this.buildHeader(); |
151 |
| - String dump = yaml.dump(this.getValues(false)); |
152 |
| - if (dump.equals("{}\n")) { |
153 |
| - dump = ""; |
154 |
| - } |
| 112 | + String yaml = super.saveToString(); |
155 | 113 |
|
156 | 114 | List<String> lines = new ArrayList<>();
|
157 |
| - for (String line : (header + dump).split("\n")) { |
158 |
| - if (!line.contains(":") || line.startsWith(" ")) { |
| 115 | + if (this.configComment != null) { |
| 116 | + lines.add(this.configComment); |
| 117 | + } |
| 118 | + |
| 119 | + for (String line : yaml.split("\n")) { |
| 120 | + // It's not line with new field |
| 121 | + if (!line.contains(":") || line.startsWith(" ") || line.startsWith("\t")) { |
159 | 122 | lines.add(line);
|
160 | 123 | continue;
|
161 | 124 | }
|
162 | 125 |
|
163 |
| - String key = line.split(":")[0]; |
164 |
| - String comment = this.comments.get(key); |
165 |
| - if (comment != null) { |
166 |
| - if (this.commentStyle == CommentStyle.ABOVE_CONTENT) { |
167 |
| - lines.add("# " + comment); |
168 |
| - } else { |
169 |
| - lines.add(line + " # " + comment); |
170 |
| - } |
| 126 | + String configFieldName = line.split(":")[0]; |
| 127 | + String comment = this.comments.get(configFieldName); |
| 128 | + if (comment == null) { |
| 129 | + lines.add(line); |
| 130 | + continue; |
| 131 | + } |
171 | 132 |
|
| 133 | + if (this.commentStyle == CommentStyle.INLINE) { |
| 134 | + lines.add(line + " # " + comment); |
172 | 135 | continue;
|
173 | 136 | }
|
174 | 137 |
|
| 138 | + // ABOVE_CONTENT |
| 139 | + lines.add("# " + comment); |
175 | 140 | lines.add(line);
|
176 | 141 | }
|
177 | 142 |
|
|
0 commit comments