1212
1313/**
1414 * Manages configuration files for the plugin.
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.
15+ * This class provides functionality to create, load, save, and manipulate YAML configuration files.
1816 */
1917public class ConfigManager {
2018
21- /** The configuration file on disk */
2219 private final File configFile ;
23-
24- /** The in-memory representation of the YAML configuration */
2520 private FileConfiguration config ;
26-
27- /** The name of the configuration file */
2821 private final String fileName ;
29-
30- /** Reference to the plugin instance */
3122 private final Plugin plugin = getInstance ();
32-
33- /** True if the config file was just created (first load) */
3423 private boolean isFirstLoad ;
3524
3625 /**
@@ -40,56 +29,59 @@ public class ConfigManager {
4029 */
4130 public ConfigManager (String fileName ) {
4231 this .fileName = fileName ;
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 );
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 );
5945 }
6046
6147 /**
6248 * Checks if a configuration file exists in a specific subfolder.
6349 *
6450 * @param subFolder the subfolder to check in
65- * @param fileName the name of the configuration file
51+ * @param fileName the name of the configuration file
6652 * @return true if the file exists, false otherwise
6753 */
6854 public static boolean isExists (String subFolder , String fileName ) {
6955 return new File (getInstance ().getDataFolder () + "/" + subFolder , fileName ).exists ();
7056 }
7157
7258 /**
73- * Initializes the configuration file by creating it if it does not exist.
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
7463 */
75- private void initializeFile () {
64+ public ConfigManager (String subFolder , String fileName ) {
65+ this .fileName = fileName ;
66+ configFile = new File (plugin .getDataFolder () + "/" + subFolder , fileName );
7667 if (!configFile .exists ()) {
7768 try {
7869 configFile .getParentFile ().mkdirs ();
7970 configFile .createNewFile ();
8071 isFirstLoad = true ;
8172 } catch (IOException e ) {
82- Bukkit .getLogger ().warning ("Failed to create config file: " + e .getMessage ());
73+ Bukkit .getLogger ().warning (e .getMessage ());
8374 }
8475 } else {
8576 isFirstLoad = false ;
8677 }
78+ config = YamlConfiguration .loadConfiguration (configFile );
8779 }
8880
8981 /**
9082 * Sets a value at the specified path in the configuration.
9183 *
92- * @param path the configuration path
84+ * @param path the configuration path
9385 * @param value the value to set
9486 */
9587 public void set (String path , Object value ) {
@@ -108,15 +100,28 @@ public Object get(String path) {
108100
109101 /**
110102 * Saves the configuration to the file.
111- * <p>
112- * Writes all changes made to the configuration back to the disk.
103+ * This method writes all changes made to the configuration back to the file.
113104 */
114105 public void saveConfig () {
115106 try {
116107 config .save (configFile );
108+ config .options ().copyDefaults (true );
117109 } catch (IOException e ) {
118- Bukkit .getLogger ().warning ("Failed to save config file: " + e .getMessage ());
110+ Bukkit .getLogger ().warning (e .getMessage ());
111+ }
112+ }
113+
114+ /**
115+ * Gets the FileConfiguration instance.
116+ *
117+ * @return the FileConfiguration instance, or null if there was an error loading
118+ */
119+ public FileConfiguration getConfig () {
120+ if (config == null ) {
121+ Bukkit .getLogger ().warning ("An error occurred while loading the config file: " + fileName );
122+ return null ;
119123 }
124+ return config ;
120125 }
121126
122127 /**
@@ -132,18 +137,6 @@ public void reloadConfig() {
132137 }
133138 }
134139
135- /**
136- * Gets the FileConfiguration instance.
137- *
138- * @return the FileConfiguration instance, or null if there was an error loading
139- */
140- public FileConfiguration getConfig () {
141- if (config == null ) {
142- Bukkit .getLogger ().warning ("An error occurred while loading the config file: " + fileName );
143- }
144- return config ;
145- }
146-
147140 /**
148141 * Checks if this is the first time the configuration file is being loaded.
149142 *
@@ -152,4 +145,4 @@ public FileConfiguration getConfig() {
152145 public boolean isFirstLoad () {
153146 return isFirstLoad ;
154147 }
155- }
148+ }
0 commit comments