1212
1313/**
1414 * Manages configuration files for the plugin.
15- * This class provides functionality to create, load, save, and manipulate YAML configuration files.
15+ * <p>
16+ * This class provides functionality to create, load, save, reload, and manipulate YAML configuration files
17+ * in the plugin's data folder or subfolders.
1618 */
1719public class ConfigManager {
1820
21+ /** The configuration file on disk */
1922 private final File configFile ;
20- private final FileConfiguration config ;
23+
24+ /** The in-memory representation of the YAML configuration */
25+ private FileConfiguration config ;
26+
27+ /** The name of the configuration file */
2128 private final String fileName ;
29+
30+ /** Reference to the plugin instance */
2231 private final Plugin plugin = getInstance ();
32+
33+ /** True if the config file was just created (first load) */
2334 private boolean isFirstLoad ;
2435
2536 /**
@@ -29,59 +40,56 @@ public class ConfigManager {
2940 */
3041 public ConfigManager (String fileName ) {
3142 this .fileName = fileName ;
32- configFile = new File (plugin .getDataFolder (), fileName );
33- if (!configFile .exists ()) {
34- try {
35- configFile .getParentFile ().mkdirs ();
36- configFile .createNewFile ();
37- isFirstLoad = true ;
38- } catch (IOException e ) {
39- Bukkit .getLogger ().warning (e .getMessage ());
40- }
41- } else {
42- isFirstLoad = false ;
43- }
44- config = YamlConfiguration .loadConfiguration (configFile );
43+ this .configFile = new File (plugin .getDataFolder (), fileName );
44+ initializeFile ();
45+ this .config = YamlConfiguration .loadConfiguration (configFile );
46+ }
47+
48+ /**
49+ * Creates a ConfigManager for a configuration file in a specific subfolder.
50+ *
51+ * @param subFolder the subfolder where the configuration file should be located
52+ * @param fileName the name of the configuration file
53+ */
54+ public ConfigManager (String subFolder , String fileName ) {
55+ this .fileName = fileName ;
56+ this .configFile = new File (plugin .getDataFolder () + "/" + subFolder , fileName );
57+ initializeFile ();
58+ this .config = YamlConfiguration .loadConfiguration (configFile );
4559 }
4660
4761 /**
4862 * Checks if a configuration file exists in a specific subfolder.
4963 *
5064 * @param subFolder the subfolder to check in
51- * @param fileName the name of the configuration file
65+ * @param fileName the name of the configuration file
5266 * @return true if the file exists, false otherwise
5367 */
5468 public static boolean isExists (String subFolder , String fileName ) {
5569 return new File (getInstance ().getDataFolder () + "/" + subFolder , fileName ).exists ();
5670 }
5771
5872 /**
59- * Creates a ConfigManager for a configuration file in a specific subfolder.
60- *
61- * @param subFolder the subfolder where the configuration file should be located
62- * @param fileName the name of the configuration file
73+ * Initializes the configuration file by creating it if it does not exist.
6374 */
64- public ConfigManager (String subFolder , String fileName ) {
65- this .fileName = fileName ;
66- configFile = new File (plugin .getDataFolder () + "/" + subFolder , fileName );
75+ private void initializeFile () {
6776 if (!configFile .exists ()) {
6877 try {
6978 configFile .getParentFile ().mkdirs ();
7079 configFile .createNewFile ();
7180 isFirstLoad = true ;
7281 } catch (IOException e ) {
73- Bukkit .getLogger ().warning (e .getMessage ());
82+ Bukkit .getLogger ().warning ("Failed to create config file: " + e .getMessage ());
7483 }
7584 } else {
7685 isFirstLoad = false ;
7786 }
78- config = YamlConfiguration .loadConfiguration (configFile );
7987 }
8088
8189 /**
8290 * Sets a value at the specified path in the configuration.
8391 *
84- * @param path the configuration path
92+ * @param path the configuration path
8593 * @param value the value to set
8694 */
8795 public void set (String path , Object value ) {
@@ -100,14 +108,27 @@ public Object get(String path) {
100108
101109 /**
102110 * Saves the configuration to the file.
103- * This method writes all changes made to the configuration back to the file.
111+ * <p>
112+ * Writes all changes made to the configuration back to the disk.
104113 */
105114 public void saveConfig () {
106115 try {
107116 config .save (configFile );
108- config .options ().copyDefaults (true );
109117 } catch (IOException e ) {
110- Bukkit .getLogger ().warning (e .getMessage ());
118+ Bukkit .getLogger ().warning ("Failed to save config file: " + e .getMessage ());
119+ }
120+ }
121+
122+ /**
123+ * Reloads the configuration from disk.
124+ * <p>
125+ * Useful when the file is manually modified while the server is running.
126+ */
127+ public void reloadConfig () {
128+ if (configFile .exists ()) {
129+ config = YamlConfiguration .loadConfiguration (configFile );
130+ } else {
131+ Bukkit .getLogger ().warning ("Config file does not exist: " + fileName );
111132 }
112133 }
113134
@@ -119,7 +140,6 @@ public void saveConfig() {
119140 public FileConfiguration getConfig () {
120141 if (config == null ) {
121142 Bukkit .getLogger ().warning ("An error occurred while loading the config file: " + fileName );
122- return null ;
123143 }
124144 return config ;
125145 }
@@ -132,4 +152,4 @@ public FileConfiguration getConfig() {
132152 public boolean isFirstLoad () {
133153 return isFirstLoad ;
134154 }
135- }
155+ }
0 commit comments