Skip to content

Commit 12d61e5

Browse files
authored
Merge pull request #10 from NighterDevelopment/copilot/fix-ea55fb49-7ecc-4a1b-be75-020e758f762e
Fix configuration updater backup issues and sell button functionality
2 parents 2ea924c + af8b369 commit 12d61e5

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

core/src/main/java/github/nighter/smartspawner/spawner/gui/storage/SpawnerStorageUI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void initializeStaticButtons() {
121121
}
122122

123123
// Create sell button
124-
GuiButton sellButton = layout.getButton("sell");
124+
GuiButton sellButton = layout.getButton("sell_all");
125125
if (sellButton != null) {
126126
staticButtons.put("sell", createButton(
127127
sellButton.getMaterial(),
@@ -289,8 +289,8 @@ private void addNavigationButtons(Map<Integer, ItemStack> updates, SpawnerData s
289289
}
290290

291291
// Add sell button if shop integration is available and button is enabled
292-
if (layout.hasButton("sell")) {
293-
GuiButton sellButton = layout.getButton("sell");
292+
if (layout.hasButton("sell_all")) {
293+
GuiButton sellButton = layout.getButton("sell_all");
294294
String indicatorKey = getPageIndicatorKey(page, totalPages, spawner);
295295
int finalTotalPages = totalPages;
296296
ItemStack sellIndicator = pageIndicatorCache.computeIfAbsent(

core/src/main/java/github/nighter/smartspawner/updates/ConfigUpdater.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ private void applyUserValues(FileConfiguration newConfig, Map<String, Object> us
170170
// Don't override config_version
171171
if (path.equals(CONFIG_VERSION_KEY)) continue;
172172

173-
if (newConfig.contains(path)) {
174-
newConfig.set(path, value);
175-
} else {
176-
plugin.getLogger().warning("Config path '" + path + "' from old config no longer exists in new config");
173+
// Check if path exists in new config before applying
174+
boolean existsInNew = newConfig.contains(path);
175+
176+
// Always apply user values to preserve their customizations
177+
newConfig.set(path, value);
178+
179+
if (!existsInNew) {
180+
plugin.getLogger().fine("Preserving custom config path '" + path + "' from old config that doesn't exist in new default");
177181
}
178182
}
179183
}

core/src/main/java/github/nighter/smartspawner/updates/GuiLayoutUpdater.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)