-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigurableNode.java
407 lines (357 loc) · 10 KB
/
ConfigurableNode.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package com.github.sanctum.labyrinth.data;
import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.api.Service;
import com.github.sanctum.labyrinth.task.TaskScheduler;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import org.bukkit.inventory.ItemStack;
/**
* @author Hempfest
* @version 1.0
*/
final class ConfigurableNode implements Node, Primitive, Primitive.Bukkit {
private final Configurable config;
private final String key;
protected ConfigurableNode(String key, Configurable configuration) {
this.config = configuration;
this.key = key;
}
@Override
public boolean isNode(String key) {
return config.isNode(this.key + "." + key);
}
@Override
public Node getNode(String node) {
return (Node) Optional.ofNullable(config.memory.get(this.key + "." + node)).orElseGet(() -> {
ConfigurableNode n = new ConfigurableNode(key + "." + node, config);
config.memory.put(n.getPath(), n);
return n;
});
}
@Override
public Object get() {
return config.get(key);
}
@Override
public Primitive toPrimitive() {
return this;
}
@Override
public Bukkit toBukkit() {
return this;
}
@Override
public String getString() {
return config.getString(this.key);
}
@Override
public int getInt() {
return config.getInt(this.key);
}
@Override
public boolean getBoolean() {
return config.getBoolean(this.key);
}
@Override
public double getDouble() {
return config.getDouble(this.key);
}
@Override
public float getFloat() {
return config.getFloat(this.key);
}
@Override
public long getLong() {
return config.getLong(this.key);
}
@Override
public List<?> getList() {
return config.getList(this.key);
}
@Override
public Map<?, ?> getMap() {
return config.getMap(this.key);
}
@Override
public List<String> getStringList() {
return config.getStringList(this.key);
}
@Override
public List<Integer> getIntegerList() {
return config.getIntegerList(this.key);
}
@Override
public List<Double> getDoubleList() {
return config.getDoubleList(this.key);
}
@Override
public List<Float> getFloatList() {
return config.getFloatList(this.key);
}
@Override
public List<Long> getLongList() {
return config.getLongList(this.key);
}
@Override
public boolean isString() {
return config.isString(this.key);
}
@Override
public boolean isBoolean() {
return config.isBoolean(this.key);
}
@Override
public boolean isInt() {
return config.isInt(this.key);
}
@Override
public boolean isDouble() {
return config.isDouble(this.key);
}
@Override
public boolean isFloat() {
return config.isFloat(this.key);
}
@Override
public boolean isLong() {
return config.isLong(this.key);
}
@Override
public boolean isList() {
return config.isList(this.key);
}
@Override
public boolean isStringList() {
return config.isStringList(this.key);
}
@Override
public boolean isFloatList() {
return config.isFloatList(this.key);
}
@Override
public boolean isDoubleList() {
return config.isDoubleList(this.key);
}
@Override
public boolean isIntegerList() {
return config.isIntegerList(this.key);
}
@Override
public boolean isLongList() {
return config.isLongList(this.key);
}
@Override
public <T> T get(Class<T> type) {
if (type.isAssignableFrom(ConfigurationSection.class)) {
if (config instanceof YamlConfiguration) {
if (get() instanceof MemorySection) {
return (T) get();
}
}
}
Object o = config.get(this.key, type);
if (o != null) {
return (T) o;
}
return null;
}
@Override
public String getPath() {
return this.key;
}
@Override
public boolean delete() {
if (config.isNode(this.key)) {
config.set(this.key, null);
TaskScheduler.of(() -> config.memory.remove(this.key)).schedule();
return true;
} else {
return false;
}
}
@Override
public void reload() {
config.reload();
}
@Override
public boolean create() {
if (!config.exists()) {
try {
config.create();
} catch (IOException ex) {
LabyrinthProvider.getService(Service.MESSENGER).getNewMessage().error("- An issue occurred while attempting to create the backing file for the '" + config.getName() + "' configuration.");
ex.printStackTrace();
}
}
if (config.getType() == FileType.JSON) {
set(new Object());
} else {
((YamlConfiguration) config).getConfig().createSection(this.key);
}
save();
return false;
}
@Override
public boolean exists() {
return isNode(this.key) || get() != null;
}
@Override
public boolean save() {
return config.save();
}
@Override
public void set(Object o) {
config.set(this.key, o);
}
@Override
public Node getParent() {
String[] k = this.key.split("//.");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < k.length - 1; i++) {
builder.append(k[i]).append(".");
}
String key = builder.toString();
if (key.endsWith(".")) {
key = key.substring(0, builder.length() - 1);
}
if (key.equals(this.key)) return this;
return getNode(key);
}
@Override
public String toJson() {
return JsonAdapter.getJsonBuilder().setPrettyPrinting().enableComplexMapKeySerialization().serializeNulls().setLenient().serializeSpecialFloatingPointValues().create().toJson(get());
}
@Override
public Set<String> getKeys(boolean deep) {
if (config instanceof YamlConfiguration) {
return get(ConfigurationSection.class).getKeys(deep);
} else {
Set<String> keys = new HashSet<>();
JsonConfiguration json = (JsonConfiguration) config;
if (json.get(this.key) instanceof Map) {
Map<String, Object> map1 = (Map<String, Object>) json.get(this.key);
if (deep) {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
keys.add(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey());
}
} else {
keys.add(entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey());
}
}
} else {
keys.add(entry.getKey() + "." + entry2.getKey());
}
}
}
keys.add(entry.getKey());
}
} else {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
keys.add(entry.getKey());
}
}
} else {
keys.add(this.key);
}
return keys;
}
}
@Override
public Map<String, Object> getValues(boolean deep) {
if (config instanceof YamlConfiguration) {
return get(ConfigurationSection.class).getValues(deep);
} else {
Map<String, Object> map = new HashMap<>();
JsonConfiguration json = (JsonConfiguration) config;
if (json.get(this.key) instanceof Map) {
Map<String, Object> map1 = (Map<String, Object>) json.get(this.key);
if (deep) {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
map.put(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey(), entry4.getValue());
}
} else {
map.put(entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey(), entry3.getValue());
}
}
} else {
map.put(entry.getKey() + "." + entry2.getKey(), entry2.getValue());
}
}
}
map.put(entry.getKey(), entry.getValue());
}
} else {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
map.put(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey(), entry4.getValue());
}
} else {
map.put(entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey(), entry3.getValue());
}
}
} else {
map.put(entry.getKey() + "." + entry2.getKey(), entry2.getValue());
}
}
} else {
map.put(entry.getKey(), entry.getValue());
}
}
}
} else {
map.put(this.key, get());
}
return map;
}
}
@Override
public boolean isLocation() {
return config.isLocation(this.key);
}
@Override
public boolean isItemStack() {
return config.isItemStack(this.key);
}
@Override
public Location getLocation() {
return config.getLocation(this.key);
}
@Override
public ItemStack getItemStack() {
return config.getItemStack(this.key);
}
}