-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.java
More file actions
150 lines (132 loc) · 4.15 KB
/
SettingsManager.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
package io.github.ricardormdev.paperserveranalytics;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.InputStream;
import java.util.Set;
/**
* Manager for spigot/bukkit configuration files.
*
* @author RichTheLord (RicardrmDev)
* @author Someone else that I don't remember who was.
* @version 3.0
*/
public class SettingsManager {
private static final SettingsManager
c = new SettingsManager("config");
public static SettingsManager getConfig() {
return c;
}
private File file;
private FileConfiguration config;
private String fileName;
/**
* Create a configuration file.
*
* @param fileName Name of the configuration file.
*/
private SettingsManager(String fileName) {
this.fileName = fileName;
if (!PaperServerAnalytics.getInstance().getDataFolder().exists()) {
if(!PaperServerAnalytics.getInstance().getDataFolder().mkdir()) {
PaperServerAnalytics.getInstance().getLogger().severe("CONFIGURATION FILE NOT GENERATED.");
}
}
file = new File(PaperServerAnalytics.getInstance().getDataFolder(), fileName + ".yml");
if (!file.exists()) {
try {
if(!file.createNewFile())
PaperServerAnalytics.getInstance().getLogger().severe("CONFIGURATION FILE NOT GENERATED.");
InputStream is = PaperServerAnalytics.class.getResourceAsStream("/" + fileName + ".yml");
if (is != null) {
FileUtils.copyInputStreamToFile(is, file);
}
} catch (Exception e) {
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> T get(String path) {
return (T) config.get(path);
}
/**
* Get the "bare" keys on the configuration.
*
* @return All the keys.
*/
public Set<String> getKeys() {
return config.getKeys(false);
}
/**
* Set an object on the configuration.
*
* @param path Where in the config will be configurated
* @param value Object.
*/
public void set(String path, Object value) {
config.set(path, value);
save();
}
/**
* Check if the configuration files contains that key.
*
* @param path
* @return
*/
public boolean contains(String path) {
return config.contains(path);
}
/**
* Create a section in the configuration.
*
* @param path Where does this needs to be stored.
* @return The created section.
*/
public ConfigurationSection createSection(String path) {
ConfigurationSection section = config.createSection(path);
save();
return section;
}
/**
* Reload the configuration.
*/
public void reload() {
if (!PaperServerAnalytics.getInstance().getDataFolder().exists()) {
PaperServerAnalytics.getInstance().getDataFolder().mkdir();
}
file = new File(PaperServerAnalytics.getInstance().getDataFolder(), fileName + ".yml");
if (!file.exists()) {
try {
file.createNewFile();
InputStream is = PaperServerAnalytics.class.getResourceAsStream(fileName + ".yml");
if (is != null) {
FileUtils.copyInputStreamToFile(is, file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
config = YamlConfiguration.loadConfiguration(file);
}
/**
* Save the configuration.
*/
public void save() {
try {
config.save(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}