- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.java
More file actions
Latest commit
150 lines (132 loc) · 4.15 KB
/
Copy pathSettingsManager.java
File metadata and controls
150 lines (132 loc) · 4.15 KB
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
packageio.github.ricardormdev.paperserveranalytics;
importorg.apache.commons.io.FileUtils;
importorg.bukkit.configuration.ConfigurationSection;
importorg.bukkit.configuration.file.FileConfiguration;
importorg.bukkit.configuration.file.YamlConfiguration;
importjava.io.File;
importjava.io.InputStream;
importjava.util.Set;
/**
* Manager for spigot/bukkit configuration files.
*
* @author RichTheLord (RicardrmDev)
* @author Someone else that I don't remember who was.
* @version 3.0
*/
publicclassSettingsManager {
privatestaticfinalSettingsManager
c = newSettingsManager("config");
publicstaticSettingsManagergetConfig() {
returnc;
}
privateFilefile;
privateFileConfigurationconfig;
privateStringfileName;
/**
* Create a configuration file.
*
* @param fileName Name of the configuration file.
*/
privateSettingsManager(StringfileName) {
this.fileName = fileName;
if (!PaperServerAnalytics.getInstance().getDataFolder().exists()) {
if(!PaperServerAnalytics.getInstance().getDataFolder().mkdir()) {
PaperServerAnalytics.getInstance().getLogger().severe("CONFIGURATION FILE NOT GENERATED.");
}
}
file = newFile(PaperServerAnalytics.getInstance().getDataFolder(), fileName + ".yml");
if (!file.exists()) {
try {
if(!file.createNewFile())
PaperServerAnalytics.getInstance().getLogger().severe("CONFIGURATION FILE NOT GENERATED.");
InputStreamis = PaperServerAnalytics.class.getResourceAsStream("/" + fileName + ".yml");
if (is != null) {
FileUtils.copyInputStreamToFile(is, file);
}
} catch (Exceptione) {
e.printStackTrace();
}
}
config = YamlConfiguration.loadConfiguration(file);
}
/**
* Get something from the configuration file.
*
* @param path Path
* @param <T> Type of the object you need to return.
* @return The object from the configuration.
*/
@SuppressWarnings("unchecked")
public <T> Tget(Stringpath) {
return (T) config.get(path);
}
/**
* Get the "bare" keys on the configuration.
*
* @return All the keys.
*/
publicSet<String> getKeys() {
returnconfig.getKeys(false);
}
/**
* Set an object on the configuration.
*
* @param path Where in the config will be configurated
* @param value Object.
*/
publicvoidset(Stringpath, Objectvalue) {
config.set(path, value);
save();
}
/**
* Check if the configuration files contains that key.
*
* @param path
* @return
*/
publicbooleancontains(Stringpath) {
returnconfig.contains(path);
}
/**
* Create a section in the configuration.
*
* @param path Where does this needs to be stored.
* @return The created section.
*/
publicConfigurationSectioncreateSection(Stringpath) {
ConfigurationSectionsection = config.createSection(path);
save();
returnsection;
}
/**
* Reload the configuration.
*/
publicvoidreload() {
if (!PaperServerAnalytics.getInstance().getDataFolder().exists()) {
PaperServerAnalytics.getInstance().getDataFolder().mkdir();
}
file = newFile(PaperServerAnalytics.getInstance().getDataFolder(), fileName + ".yml");
if (!file.exists()) {
try {
file.createNewFile();
InputStreamis = PaperServerAnalytics.class.getResourceAsStream(fileName + ".yml");
if (is != null) {
FileUtils.copyInputStreamToFile(is, file);
}
} catch (Exceptione) {
e.printStackTrace();
}
}
config = YamlConfiguration.loadConfiguration(file);
}
/**
* Save the configuration.
*/
publicvoidsave() {
try {
config.save(file);
} catch (Exceptione) {
e.printStackTrace();
}
}
}