-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.java
More file actions
148 lines (134 loc) · 4.62 KB
/
ConfigManager.java
File metadata and controls
148 lines (134 loc) · 4.62 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
package com.github.pinont.singularitylib.api.manager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
import static com.github.pinont.singularitylib.plugin.CorePlugin.getInstance;
/**
* Manages configuration files for the plugin.
* This class provides functionality to create, load, save, and manipulate YAML configuration files.
*/
public class ConfigManager {
private final File configFile;
private FileConfiguration config;
private final String fileName;
private final Plugin plugin = getInstance();
private boolean isFirstLoad;
/**
* Creates a ConfigManager for a configuration file in the plugin's data folder.
*
* @param fileName the name of the configuration file
*/
public ConfigManager(String fileName) {
this.fileName = fileName;
configFile = new File(plugin.getDataFolder(), fileName);
if (!configFile.exists()) {
try {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
isFirstLoad = true;
} catch (IOException e) {
Bukkit.getLogger().warning(e.getMessage());
}
} else {
isFirstLoad = false;
}
config = YamlConfiguration.loadConfiguration(configFile);
}
/**
* Checks if a configuration file exists in a specific subfolder.
*
* @param subFolder the subfolder to check in
* @param fileName the name of the configuration file
* @return true if the file exists, false otherwise
*/
public static boolean isExists(String subFolder, String fileName) {
return new File(getInstance().getDataFolder() + "/" + subFolder, fileName).exists();
}
/**
* Creates a ConfigManager for a configuration file in a specific subfolder.
*
* @param subFolder the subfolder where the configuration file should be located
* @param fileName the name of the configuration file
*/
public ConfigManager(String subFolder, String fileName) {
this.fileName = fileName;
configFile = new File(plugin.getDataFolder() + "/" + subFolder, fileName);
if (!configFile.exists()) {
try {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
isFirstLoad = true;
} catch (IOException e) {
Bukkit.getLogger().warning(e.getMessage());
}
} else {
isFirstLoad = false;
}
config = YamlConfiguration.loadConfiguration(configFile);
}
/**
* Sets a value at the specified path in the configuration.
*
* @param path the configuration path
* @param value the value to set
*/
public void set(String path, Object value) {
config.set(path, value);
}
/**
* Gets a value from the specified path in the configuration.
*
* @param path the configuration path
* @return the value at the specified path, or null if not found
*/
public Object get(String path) {
return config.get(path);
}
/**
* Saves the configuration to the file.
* This method writes all changes made to the configuration back to the file.
*/
public void saveConfig() {
try {
config.save(configFile);
config.options().copyDefaults(true);
} catch (IOException e) {
Bukkit.getLogger().warning(e.getMessage());
}
}
/**
* Gets the FileConfiguration instance.
*
* @return the FileConfiguration instance, or null if there was an error loading
*/
public FileConfiguration getConfig() {
if (config == null) {
Bukkit.getLogger().warning("An error occurred while loading the config file: " + fileName);
return null;
}
return config;
}
/**
* Reloads the configuration from disk.
* <p>
* Useful when the file is manually modified while the server is running.
*/
public void reloadConfig() {
if (configFile.exists()) {
config = YamlConfiguration.loadConfiguration(configFile);
} else {
Bukkit.getLogger().warning("Config file does not exist: " + fileName);
}
}
/**
* Checks if this is the first time the configuration file is being loaded.
*
* @return true if this is the first load (file was just created), false otherwise
*/
public boolean isFirstLoad() {
return isFirstLoad;
}
}