-
Notifications
You must be signed in to change notification settings - Fork 0
/
OFile.java
281 lines (227 loc) · 8.86 KB
/
OFile.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
package kodia.kodia;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
/** Represents a custom and accessible YAML file manager.
*
* @author Antoine Blondon
* @since 1.0
*
*/
public class OFile {
private File file;
/** Creates an OFile with the specified name as its title.
*
* @param path A String representing the title of the file.
*/
public OFile(String path) {
File file = new File(String.format("plugins/General/%s.yml", path));
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
this.file = file;
}
/** Creates an OFile with the specified string as its title in the specified folder.
* Dir: plugins/{folder}/{name}.yml
*
* @param folder The folder of the file.
* @param path A String representing the name of the file.
*/
public OFile(String folder, String path) {
String f = folder.toLowerCase();
String n = path.toLowerCase();
File file = new File("plugins/"+ f +"/" + n + ".yml");
if (!file.exists()) {
try {
new File(file.getParent()).mkdirs();
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
this.file = file;
}
/** Sets the element at the path of this OFile to a new value.
*
* @param path A String representing the path of the element to change.
* @param value An Object representing the new value of the changed element.
*/
public void set(String path, Object value) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
conf.set(path, value);
try {
conf.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/** Gets an int at the specified path of this OFile.
*
* @param path A String representing the path of the int to get.
* @return The int located at the specified path in this OFile.
*/
public int getInt(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
if(conf.get(path) == null) {
set(path, 0);
}
return conf.getInt(path);
}
/** Gets a String at the specified path of this OFile.
*
* @param path A String representing the path of the String to get.
* @return The String located at the specified path in this OFile.
*/
public String getString(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
if(conf.get(path) == null) {
set(path, "");
}
return conf.getString(path);
}
/** Gets a boolean at the specified path of this OFile.
*
* @param path A String representing the path of the boolean to get.
* @return The boolean located at the specified path in this OFile.
*/
public boolean getBoolean(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
if(conf.get(path) == null) {
set(path, false);
}
return conf.getBoolean(path);
}
/** Gets a List of unknown data type at the specified path of this OFile.
*
* @param path A String representing the path of the List to get.
* @return The List of unknown data type located at the specified path in this OFile.
*/
public List<?> getList(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
if(conf.get(path) == null) {
set(path, new ArrayList<>());
}
return conf.getList(path);
}
/** Gets a Map of unknown data types at the specified path of this OFile.
*
* @param path A String representing the path of the Map to get.
* @return The Map of unknown data types located at the specified path in this OFile.
*/
public Map<?, ?> getMap(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
if(conf.get(path) == null) {
set(path, new HashMap<>());
}
return conf.getMapList(path).get(0);
}
/** Gets an Object at the specified path of this OFile.
*
* @param path A String representing the path of the Object to get.
* @return The Object located at the specified path in this OFile.
*/
public Object get(String path) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
return conf.get(path);
}
/** Checks if the element located at the specified path of this OFile is null.
*
* @param path A String representing the path of the element to check.
* @return True if the element is null. False otherwise.
*/
public boolean isNull(String path) {
return YamlConfiguration.loadConfiguration(file).get(path) == null;
}
/** Adds a specified value to the int located at the specified path of this OFile.
*
* @param path A String representing the path of the int to increment.
* @param value An int representing the value to add.
*/
public void addToInt(String path, int value) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
conf.set(path, conf.getInt(path)+value);
try {
conf.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/** Adds an element to the List located at the specified path of this OFile.
*
* @param path A String representing the path of the List.
* @param element An Object to add to the specified List.
*/
@SuppressWarnings("unchecked")
public void addToList(String path, Object element) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
List<Object> list = new ArrayList<>();
if(conf.get(path) != null) {
list = (List<Object>) conf.getList(path);
}
list.add(element);
conf.set(path, list);
try {
conf.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/** Removes an element from the List located at the specified path of this OFile.
*
* @param path A String representing the path of the List.
* @param o An Object to remove from the specified List.
*/
@SuppressWarnings("unchecked")
public void removeFromList(String path, Object o) {
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
List<Object> list = new ArrayList<>();
if(conf.get(path) != null) {
list = (List<Object>) conf.getList(path);
}
list.remove(o);
conf.set(path, list);
try {
conf.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/** Gets the File from this OFile.
*
* @return A File with the path of this OFile.
*/
public File toFile() {
return file;
}
/** Gets the YamlConfiguration from this OFile.
*
* @return A YamlConfiguration got from the File of this OFile.
*/
public YamlConfiguration toYaml() {
return YamlConfiguration.loadConfiguration(file);
}
/**
* Retrieves a set of keys from the YAML configuration section at the specified path.
* This method can operate in two modes, determined by the `deep` parameter: if `deep` is true,
* it will return all keys within the configuration section, including nested ones; if `deep` is false,
* only first-level keys are returned.
*
* @param path A string representing the path to the configuration section from which the keys are to be fetched.
* If the path is null or does not exist, an empty set is returned.
* @param deep A boolean indicating whether to fetch keys recursively (`true`) or only at the first level (`false`).
* @return A set of strings containing the keys found at the specified path. If the specified path does not
* lead to a valid configuration section or is null, an empty set is returned. This ensures that the method
* call is safe and will not result in a NullPointerException.
*/
public Set<String> keys(String path, boolean deep) {
ConfigurationSection config = toYaml().getConfigurationSection(path);
if(config == null) return new HashSet<>();
return config.getKeys(deep);
}
}