-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomConfig.java
More file actions
77 lines (67 loc) · 2.08 KB
/
CustomConfig.java
File metadata and controls
77 lines (67 loc) · 2.08 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
package wtf.nucker.spigotutilities.utils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
/**
* A class for easily creating custom config files
* @author Nucker
*/
public class CustomConfig {
private final File file;
private final YamlConfiguration yaml;
private static JavaPlugin plugin = JavaPlugin.getProvidingPlugin(getClass());
/**
* The constructor. Pass your params and the file is created and initiated.
* @param name The name of the file. EG data.yml
*
* <b>Must include the extension (.yml)</b>
*/
public CustomConfig(String name) {
if(!name.endsWith(".yml")) name = name+".yml";
File dir = plugin.getDataFolder();
if(!dir.exists()) {
dir.mkdirs();
}
file = new File(dir, name);
if(!file.exists()) {
plugin.saveResource(name, false);
}
yaml = new YamlConfiguration();
try {
yaml.load(file);
}catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
/**
* Loads the config file again updating the values that you can get in your code
* to those that have been updated in the file
*/
public void reload() {
try {
yaml.load(file);
}catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
/**
* Saves any changes made by the code to the file
* EG: You set a value in your config. Call this method to save it into your file
*/
public void save() {
try {
yaml.save(file);
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* @return Returns the {@link YamlConfiguration}
*/
public YamlConfiguration getConfig() {
return yaml;
}
}