@@ -52,8 +52,30 @@ private void updateLayoutFile(String layoutName, File layoutFile, String fileNam
5252 String configVersionStr = currentConfig .getString (GUI_LAYOUT_VERSION_KEY , "0.0.0" );
5353
5454 if (configVersionStr .equals ("0.0.0" )) {
55- plugin .debug ("No version found in " + layoutName + "/" + fileName + ", creating default layout file with header" );
56- createDefaultLayoutFileWithHeader (layoutName , layoutFile , fileName );
55+ plugin .debug ("No version found in " + layoutName + "/" + fileName + ", backing up and adding version header" );
56+
57+ // Create backup of the user's file before overwriting
58+ File backupFile = new File (layoutFile .getParent (), fileName .replace (".yml" , "_backup_" + configVersionStr + ".yml" ));
59+ Files .copy (layoutFile .toPath (), backupFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
60+ plugin .getLogger ().info ("Layout backup created at " + backupFile .getName ());
61+
62+ // Get user's current values
63+ Map <String , Object > userValues = flattenConfig (currentConfig );
64+
65+ // Create temp file with default layout
66+ File tempFile = new File (layoutFile .getParent (), fileName .replace (".yml" , "_new.yml" ));
67+ createDefaultLayoutFileWithHeader (layoutName , tempFile , fileName );
68+
69+ // Load the default config and set version
70+ FileConfiguration newConfig = YamlConfiguration .loadConfiguration (tempFile );
71+ newConfig .set (GUI_LAYOUT_VERSION_KEY , currentVersion );
72+
73+ // Apply user values to preserve their customizations
74+ applyUserValues (newConfig , userValues );
75+
76+ // Save final config to user's file
77+ newConfig .save (layoutFile );
78+ tempFile .delete ();
5779 return ;
5880 }
5981
@@ -161,10 +183,14 @@ private void applyUserValues(FileConfiguration newConfig, Map<String, Object> us
161183
162184 if (path .equals (GUI_LAYOUT_VERSION_KEY )) continue ;
163185
164- if (newConfig .contains (path )) {
165- newConfig .set (path , value );
166- } else {
167- plugin .getLogger ().fine ("Layout path '" + path + "' from old config no longer exists in new config" );
186+ // Check if path exists in new config before applying
187+ boolean existsInNew = newConfig .contains (path );
188+
189+ // Always apply user values to preserve their customizations
190+ newConfig .set (path , value );
191+
192+ if (!existsInNew ) {
193+ plugin .getLogger ().fine ("Preserving custom layout path '" + path + "' from old config that doesn't exist in new default" );
168194 }
169195 }
170196 }
0 commit comments