@@ -24,174 +24,4 @@ public GuiLayoutUpdater(SmartSpawner plugin) {
2424 this .plugin = plugin ;
2525 this .currentVersion = plugin .getDescription ().getVersion ();
2626 }
27-
28- public void checkAndUpdateLayouts () {
29- File layoutsDir = new File (plugin .getDataFolder (), GUI_LAYOUTS_DIR );
30- if (!layoutsDir .exists ()) {
31- return ;
32- }
33-
34- for (String layoutName : LAYOUT_NAMES ) {
35- File layoutDir = new File (layoutsDir , layoutName );
36- if (!layoutDir .exists ()) {
37- continue ;
38- }
39-
40- for (String fileName : LAYOUT_FILES ) {
41- File layoutFile = new File (layoutDir , fileName );
42- if (layoutFile .exists ()) {
43- updateLayoutFile (layoutName , layoutFile , fileName );
44- }
45- }
46- }
47- }
48-
49- private void updateLayoutFile (String layoutName , File layoutFile , String fileName ) {
50- try {
51- FileConfiguration currentConfig = YamlConfiguration .loadConfiguration (layoutFile );
52- String configVersionStr = currentConfig .getString (GUI_LAYOUT_VERSION_KEY , "0.0.0" );
53-
54- if (configVersionStr .equals ("0.0.0" )) {
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 ();
79- return ;
80- }
81-
82- Version configVersion = new Version (configVersionStr );
83- Version pluginVersion = new Version (currentVersion );
84-
85- if (configVersion .compareTo (pluginVersion ) >= 0 ) {
86- return ;
87- }
88-
89- Map <String , Object > userValues = flattenConfig (currentConfig );
90-
91- File tempFile = new File (layoutFile .getParent (), fileName .replace (".yml" , "_new.yml" ));
92- createDefaultLayoutFileWithHeader (layoutName , tempFile , fileName );
93-
94- FileConfiguration newConfig = YamlConfiguration .loadConfiguration (tempFile );
95- newConfig .set (GUI_LAYOUT_VERSION_KEY , currentVersion );
96-
97- boolean configDiffers = hasConfigDifferences (userValues , newConfig );
98-
99- if (configDiffers ) {
100- File backupFile = new File (layoutFile .getParent (), fileName .replace (".yml" , "_backup_" + configVersionStr + ".yml" ));
101- Files .copy (layoutFile .toPath (), backupFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
102- plugin .getLogger ().info ("Layout backup created at " + backupFile .getName ());
103- } else {
104- plugin .debug ("No significant layout changes detected for " + layoutName + "/" + fileName + ", skipping backup creation" );
105- }
106-
107- applyUserValues (newConfig , userValues );
108- newConfig .save (layoutFile );
109- tempFile .delete ();
110-
111- } catch (Exception e ) {
112- plugin .getLogger ().log (Level .WARNING , "Failed to update layout " + layoutName + "/" + fileName + ": " + e .getMessage (), e );
113- }
114- }
115-
116- private void createDefaultLayoutFileWithHeader (String layoutName , File destinationFile , String fileName ) {
117- try {
118- String resourcePath = GUI_LAYOUTS_DIR + "/" + layoutName + "/" + fileName ;
119- plugin .saveResource (resourcePath , true );
120-
121- FileConfiguration config = YamlConfiguration .loadConfiguration (destinationFile );
122-
123- // Create a new configuration with version at the top
124- FileConfiguration newConfig = new YamlConfiguration ();
125- newConfig .set (GUI_LAYOUT_VERSION_KEY , currentVersion );
126-
127- // Copy all existing keys to preserve order, with version first
128- for (String key : config .getKeys (false )) {
129- if (!key .equals (GUI_LAYOUT_VERSION_KEY )) {
130- newConfig .set (key , config .get (key ));
131- }
132- }
133-
134- newConfig .save (destinationFile );
135-
136- } catch (Exception e ) {
137- plugin .getLogger ().log (Level .WARNING , "Failed to create default layout file with header for " + layoutName + "/" + fileName , e );
138- }
139- }
140-
141- private boolean hasConfigDifferences (Map <String , Object > userValues , FileConfiguration newConfig ) {
142- Map <String , Object > newValues = flattenConfig (newConfig );
143-
144- for (Map .Entry <String , Object > entry : userValues .entrySet ()) {
145- String path = entry .getKey ();
146- Object userValue = entry .getValue ();
147-
148- if (path .equals (GUI_LAYOUT_VERSION_KEY )) continue ;
149-
150- if (!newValues .containsKey (path )) {
151- return true ;
152- }
153-
154- Object newValue = newValues .get (path );
155- if (!Objects .equals (userValue , newValue )) {
156- return true ;
157- }
158- }
159-
160- for (String path : newValues .keySet ()) {
161- if (!path .equals (GUI_LAYOUT_VERSION_KEY ) && !userValues .containsKey (path )) {
162- return true ;
163- }
164- }
165-
166- return false ;
167- }
168-
169- private Map <String , Object > flattenConfig (ConfigurationSection config ) {
170- Map <String , Object > result = new HashMap <>();
171- for (String key : config .getKeys (true )) {
172- if (!config .isConfigurationSection (key )) {
173- result .put (key , config .get (key ));
174- }
175- }
176- return result ;
177- }
178-
179- private void applyUserValues (FileConfiguration newConfig , Map <String , Object > userValues ) {
180- for (Map .Entry <String , Object > entry : userValues .entrySet ()) {
181- String path = entry .getKey ();
182- Object value = entry .getValue ();
183-
184- if (path .equals (GUI_LAYOUT_VERSION_KEY )) continue ;
185-
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" );
194- }
195- }
196- }
19727}
0 commit comments