/
YamlStorage.java
345 lines (298 loc) · 9.77 KB
/
YamlStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package net.citizensnpcs.api.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.google.common.io.Files;
public class YamlStorage implements FileStorage {
private final FileConfiguration config;
private final File file;
public YamlStorage(File file) {
this(file, null);
}
public YamlStorage(File file, String header) {
config = new YamlConfiguration();
tryIncreaseMaxCodepoints(config);
this.file = file;
if (!file.exists()) {
create();
if (header != null) {
config.options().header(header);
}
save();
}
}
private void create() {
try {
Bukkit.getLogger().log(Level.INFO, "Creating file: " + file.getName());
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Could not create file: " + file.getName());
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
YamlStorage other = (YamlStorage) obj;
if (!Objects.equals(file, other.file)) {
return false;
}
return true;
}
@Override
public File getFile() {
return file;
}
@Override
public YamlKey getKey(String root) {
return new YamlKey(root);
}
@Override
public int hashCode() {
return 31 + (file == null ? 0 : file.hashCode());
}
@Override
public boolean load() {
try {
config.load(file);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
private boolean pathExists(String key) {
return config.get(key) != null;
}
@Override
public void save() {
try {
Files.createParentDirs(file);
File temporaryFile = File.createTempFile(file.getName(), null, file.getParentFile());
temporaryFile.deleteOnExit();
config.save(temporaryFile);
file.delete();
temporaryFile.renameTo(file);
temporaryFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public String toString() {
return "YamlStorage {file=" + file + "}";
}
private void tryIncreaseMaxCodepoints(FileConfiguration config) {
if (SET_CODEPOINT_LIMIT == null || LOADER_OPTIONS == null)
return;
try {
SET_CODEPOINT_LIMIT.invoke(LOADER_OPTIONS.get(config), 67108864 /* ~64MB, Paper's limit */);
} catch (Exception e) {
e.printStackTrace();
}
}
public class YamlKey extends DataKey {
public YamlKey(String root) {
super(root);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj) || getClass() != obj.getClass())
return false;
YamlKey other = (YamlKey) obj;
return getOuterType().equals(other.getOuterType());
}
@Override
public boolean getBoolean(String key) {
String path = createRelativeKey(key);
if (pathExists(path)) {
if (config.getString(path) == null)
return config.getBoolean(path);
return Boolean.parseBoolean(config.getString(path));
}
return false;
}
@Override
public boolean getBoolean(String key, boolean def) {
return config.getBoolean(createRelativeKey(key), def);
}
@Override
public double getDouble(String key) {
return getDouble(key, 0);
}
@Override
public double getDouble(String key, double def) {
String path = createRelativeKey(key);
if (pathExists(path)) {
Object value = config.get(path);
if (value instanceof Number)
return ((Number) value).doubleValue();
String raw = value.toString();
if (raw.isEmpty())
return def;
return Double.parseDouble(raw);
}
return def;
}
@Override
public DataKey getFromRoot(String path) {
return new YamlKey(path);
}
@Override
public int getInt(String key) {
return getInt(key, 0);
}
@Override
public int getInt(String key, int def) {
String path = createRelativeKey(key);
if (pathExists(path)) {
Object value = config.get(path);
if (value instanceof Number)
return ((Number) value).intValue();
String raw = value.toString();
if (raw.isEmpty())
return def;
return Integer.parseInt(raw);
}
return def;
}
@Override
public long getLong(String key) {
return getLong(key, 0L);
}
@Override
public long getLong(String key, long def) {
String path = createRelativeKey(key);
if (pathExists(path)) {
Object value = config.get(path);
if (value instanceof Number)
return ((Number) value).longValue();
String raw = value.toString();
if (raw.isEmpty())
return def;
return Long.parseLong(raw);
}
return def;
}
private YamlStorage getOuterType() {
return YamlStorage.this;
}
@Override
public Object getRaw(String key) {
return config.get(createRelativeKey(key));
}
@Override
public YamlKey getRelative(String relative) {
if (relative == null || relative.isEmpty())
return this;
return new YamlKey(createRelativeKey(relative));
}
public ConfigurationSection getSection(String key) {
String path = createRelativeKey(key);
return config.getConfigurationSection(path);
}
public YamlStorage getStorage() {
return YamlStorage.this;
}
@Override
public String getString(String key) {
String path = createRelativeKey(key);
if (pathExists(path))
return config.get(path).toString();
return "";
}
@Override
public Iterable<DataKey> getSubKeys() {
ConfigurationSection section = config.getConfigurationSection(path);
if (section == null)
return Collections.emptyList();
List<DataKey> res = new ArrayList<>();
for (String key : section.getKeys(false)) {
res.add(getRelative(key));
}
return res;
}
@Override
public Map<String, Object> getValuesDeep() {
return sectionToValues(config.getConfigurationSection(path));
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime * super.hashCode() + getOuterType().hashCode();
return result;
}
@Override
public boolean keyExists(String key) {
Object value = config.get(createRelativeKey(key));
if (value == null)
return false;
return true;
}
@Override
public String name() {
int last = path.lastIndexOf('.');
return path.substring(last == 0 ? 0 : last + 1);
}
@Override
public void removeKey(String key) {
config.set(createRelativeKey(key), null);
}
@Override
public void setBoolean(String key, boolean value) {
config.set(createRelativeKey(key), value);
}
@Override
public void setDouble(String key, double value) {
config.set(createRelativeKey(key), String.valueOf(value));
}
@Override
public void setInt(String key, int value) {
config.set(createRelativeKey(key), value);
}
@Override
public void setLong(String key, long value) {
config.set(createRelativeKey(key), value);
}
@Override
public void setRaw(String key, Object value) {
config.set(createRelativeKey(key), value);
}
@Override
public void setString(String key, String value) {
config.set(createRelativeKey(key), value);
}
@Override
public String toString() {
return "YamlKey [path=" + path + "]";
}
}
private static Field LOADER_OPTIONS;
private static Method SET_CODEPOINT_LIMIT;
static {
try {
LOADER_OPTIONS = YamlConfiguration.class.getDeclaredField("yamlLoaderOptions");
LOADER_OPTIONS.setAccessible(true);
SET_CODEPOINT_LIMIT = Class.forName("org.yaml.snakeyaml.LoaderOptions").getMethod("setCodepointLimit",
int.class);
SET_CODEPOINT_LIMIT.setAccessible(true);
} catch (Exception e) {
}
}
}