-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigReader.java
99 lines (90 loc) · 2.6 KB
/
ConfigReader.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
/**
* Spigot-CustomConfig
*
* Copyright Katorly Lab
* (github.com/katorlys)
*
* License under GPLv3
*/
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ConfigReader {
private File file;
private File filepath;
private final JavaPlugin plugin;
private final String name;
private final String path;
private FileConfiguration config;
/**
* Create the default config if the config does not exist.
*
*/
public void saveDefaultConfig() {
if (!filepath.exists()) {
boolean success = filepath.mkdirs();
if (!success)
Bukkit.getLogger().severe("Error creating the config. Please try again.");
}
if (!file.exists())
this.plugin.saveResource(path + name, false);
}
/**
* Get values in the config.
*
* @return
*/
public FileConfiguration getConfig() {
if (!filepath.exists())
this.saveDefaultConfig();
if (config == null)
this.reloadConfig();
return config;
}
/**
* Reload the config. This will remove all the comments in it.
*
*/
public void reloadConfig() {
if (filepath == null)
filepath = new File(plugin.getDataFolder(), path);
if (file == null)
file = new File(filepath, name);
config = YamlConfiguration.loadConfiguration(file);
InputStream stream = plugin.getResource(name);
if (stream != null) {
YamlConfiguration YmlFile = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
config.setDefaults(YmlFile);
}
}
/**
* Save the config to apply changes.
*
*/
public void saveConfig() {
try {
config.save(file);
} catch (Throwable t) {
Bukkit.getLogger().severe("Error saving the config. Please try again.");
}
}
public ConfigReader(JavaPlugin plugin, String pathname, String filename) {
this.plugin = plugin;
this.path = pathname;
this.name = filename;
this.filepath = new File(plugin.getDataFolder(), path);
this.file = new File(filepath, name);
}
/**
* Excute the function of saveConfig() and reloadConfig()
*
*/
public static void save(ConfigReader config) {
config.saveConfig();
config.reloadConfig();
}
}