SHOW:
|
|
- or go back to the newest paste.
1 | - | public class ConfigManager { |
1 | + | package myplugin.configs; |
2 | ||
3 | - | private ConfigManager() {} |
3 | + | import org.bukkit.configuration.InvalidConfigurationException; |
4 | - | private static ConfigManager manager = new ConfigManager(); |
4 | + | import org.bukkit.configuration.file.FileConfiguration; |
5 | - | public static ConfigManager getManager() {return manager;} |
5 | + | import org.bukkit.configuration.file.YamlConfiguration; |
6 | ||
7 | - | private Main plugin = Main.getPlugin(Main.class); |
7 | + | import java.io.File; |
8 | import java.io.IOException; | |
9 | - | public FileConfiguration config; |
9 | + | |
10 | import static org.bukkit.Bukkit.getLogger; | |
11 | - | public File configfile; |
11 | + | |
12 | public final class ConfigManager { | |
13 | - | public void setupFiles() { |
13 | + | public static FileConfiguration config; |
14 | - | configfile = new File(plugin.getDataFolder(), "config.yml"); |
14 | + | public static File configfile; |
15 | ||
16 | - | if (!this.configfile.exists()) { |
16 | + | public static void Init() { |
17 | - | this.configfile.getParentFile().mkdirs(); |
17 | + | |
18 | - | plugin.saveResource("config.yml", false); |
18 | + | getLogger().info("[CKL] Getting config file..."); |
19 | configfile = new File(MyPlugin.instance.getDataFolder(), "config.yml"); | |
20 | ||
21 | - | this.config = new YamlConfiguration(); |
21 | + | if (!configfile.exists()) { |
22 | getLogger().info("[CKL] Config file doesn't exist. Creating..."); | |
23 | configfile.getParentFile().mkdirs(); | |
24 | - | this.config.load(this.configfile); |
24 | + | MyPlugin.instance.saveResource("config.yml", false); |
25 | - | } catch (IOException | InvalidConfigurationException e) { |
25 | + | getLogger().info("[CKL] Successfully created config file"); |
26 | } | |
27 | ||
28 | config = new YamlConfiguration(); | |
29 | ||
30 | - | public FileConfiguration getConfig() { |
30 | + | try { |
31 | - | return config; |
31 | + | getLogger().info("[CKL] Loading config file..."); |
32 | config.load(configfile); | |
33 | } | |
34 | catch (IOException io) { | |
35 | - | public void saveConfig() { |
35 | + | getLogger().info("[CKL] IOException when loading config"); |
36 | io.printStackTrace(); | |
37 | - | this.config.save(this.configfile); |
37 | + | } |
38 | - | Bukkit.getServer().getConsoleSender().sendMessage("Config.yml has been saved"); |
38 | + | catch (InvalidConfigurationException e) { |
39 | - | } catch (IOException e) { |
39 | + | getLogger().info("[CKL] InvalidConfigurationException when loading config"); |
40 | - | Bukkit.getServer().getConsoleSender().sendMessage("Config.yml could not be saved"); |
40 | + | e.printStackTrace(); |
41 | } | |
42 | } | |
43 | catch (Exception e){ | |
44 | - | public void reloadConfig() { |
44 | + | getLogger().info("[CKL] Failed to setup config file"); |
45 | - | this.config = YamlConfiguration.loadConfiguration(this.configfile); |
45 | + | |
46 | - | } |
46 | + | |
47 | } | |
48 | ||
49 | public static void SaveConfig() { | |
50 | try { | |
51 | config.save(configfile); | |
52 | getLogger().info("[CKL] config.yml has been saved"); | |
53 | } | |
54 | catch (IOException e) { | |
55 | getLogger().info("[CKL] config.yml could not be saved"); | |
56 | } | |
57 | } | |
58 | ||
59 | public static void ReloadConfig() { | |
60 | config = YamlConfiguration.loadConfiguration(configfile); | |
61 | getLogger().info("[CKL] Successfully reloaded config"); | |
62 | } | |
63 | } |