Skip to content

Commit b5a5598

Browse files
committed
refactor: Simplify SpawnerAuditListener constructor and remove legacy migration code from DiscordConfigUpdater
1 parent bf069b9 commit b5a5598

3 files changed

Lines changed: 5 additions & 63 deletions

File tree

core/src/main/java/github/nighter/smartspawner/SmartSpawner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private void initializeServices() {
248248
// Initialize logging system
249249
this.loggingConfig = new LoggingConfig(this);
250250
this.spawnerActionLogger = new SpawnerActionLogger(this, loggingConfig);
251-
this.spawnerAuditListener = new SpawnerAuditListener(this, spawnerActionLogger);
251+
this.spawnerAuditListener = new SpawnerAuditListener(spawnerActionLogger);
252252
}
253253

254254
private void initializeEconomyComponents() {
@@ -534,7 +534,7 @@ public void reload() {
534534
// Unregister the old listener before registering a fresh one to prevent
535535
// duplicate event handling and the associated memory leak.
536536
if (spawnerAuditListener != null) HandlerList.unregisterAll(spawnerAuditListener);
537-
this.spawnerAuditListener = new SpawnerAuditListener(this, spawnerActionLogger);
537+
this.spawnerAuditListener = new SpawnerAuditListener(spawnerActionLogger);
538538
getServer().getPluginManager().registerEvents(spawnerAuditListener, this);
539539

540540
// Reinitialize FormUI components in case config changed

core/src/main/java/github/nighter/smartspawner/logging/SpawnerAuditListener.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package github.nighter.smartspawner.logging;
22

3-
import github.nighter.smartspawner.SmartSpawner;
43
import github.nighter.smartspawner.api.events.*;
54
import org.bukkit.event.EventHandler;
65
import org.bukkit.event.EventPriority;
@@ -12,11 +11,9 @@
1211
* Listens to all spawner events and delegates to the SpawnerActionLogger.
1312
*/
1413
public class SpawnerAuditListener implements Listener {
15-
private final SmartSpawner plugin;
1614
private final SpawnerActionLogger logger;
1715

18-
public SpawnerAuditListener(SmartSpawner plugin, SpawnerActionLogger logger) {
19-
this.plugin = plugin;
16+
public SpawnerAuditListener(SpawnerActionLogger logger) {
2017
this.logger = logger;
2118
}
2219

core/src/main/java/github/nighter/smartspawner/logging/discord/DiscordConfigUpdater.java

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@
22

33
import github.nighter.smartspawner.SmartSpawner;
44
import github.nighter.smartspawner.updates.ConfigVersionService;
5-
import org.bukkit.configuration.file.FileConfiguration;
6-
import org.bukkit.configuration.file.YamlConfiguration;
75

86
import java.io.File;
9-
import java.io.IOException;
107

118
/**
129
* Ensures {@code discord_logging.yml} exists and is up-to-date.
1310
* Delegates version checking / backup / merge to {@link ConfigVersionService}.
14-
*
15-
* <p>Legacy migration: if the server still has a {@code discord.yml} from a
16-
* previous version, its user settings (enabled, webhook_url, show_player_head,
17-
* log_all_events, logged_events) are copied into the new file before the old
18-
* file is renamed to {@code discord.yml.backup}.</p>
1911
*/
2012
public class DiscordConfigUpdater {
2113

2214
private static final String FILE_NAME = "discord_logging.yml";
23-
private static final String LEGACY_NAME = "discord.yml";
2415
private static final String VERSION_KEY = "config_version";
2516

2617
private final SmartSpawner plugin;
@@ -31,53 +22,7 @@ public DiscordConfigUpdater(SmartSpawner plugin) {
3122

3223
/** Call this before {@link DiscordWebhookConfig} tries to load the file. */
3324
public void checkAndUpdate() {
34-
File newFile = new File(plugin.getDataFolder(), FILE_NAME);
35-
File legacyFile = new File(plugin.getDataFolder(), LEGACY_NAME);
36-
37-
// ── Migrate from legacy discord.yml if it exists and new file doesn't ──
38-
if (!newFile.exists() && legacyFile.exists()) {
39-
migrateFromLegacy(legacyFile, newFile);
40-
return; // createFromDefaults + legacy merge already written
41-
}
42-
43-
ConfigVersionService.updateFile(plugin, newFile, FILE_NAME, VERSION_KEY);
44-
}
45-
46-
// ── Helpers ───────────────────────────────────────────────────────────────
47-
48-
/**
49-
* Creates {@code discord_logging.yml} from bundled defaults, then overlays
50-
* user values from the legacy {@code discord.yml}, and renames the old
51-
* file.
52-
*/
53-
private void migrateFromLegacy(File legacyFile, File newFile) {
54-
plugin.getLogger().info("[Discord] Migrating " + LEGACY_NAME + " → " + FILE_NAME + " …");
55-
try {
56-
// 1. Write fresh defaults
57-
ConfigVersionService.updateFile(plugin, newFile, FILE_NAME, VERSION_KEY);
58-
59-
// 2. Overlay user values from legacy file
60-
FileConfiguration legacy = YamlConfiguration.loadConfiguration(legacyFile);
61-
FileConfiguration fresh = YamlConfiguration.loadConfiguration(newFile);
62-
63-
for (String key : new String[]{"enabled", "webhook_url", "show_player_head",
64-
"log_all_events"}) {
65-
if (legacy.contains(key)) fresh.set(key, legacy.get(key));
66-
}
67-
if (legacy.contains("logged_events")) {
68-
fresh.set("logged_events", legacy.get("logged_events"));
69-
}
70-
fresh.save(newFile);
71-
72-
// 3. Rename legacy file so it won't trigger migration again
73-
File backup = new File(plugin.getDataFolder(), LEGACY_NAME + ".backup");
74-
if (legacyFile.renameTo(backup)) {
75-
plugin.getLogger().info("[Discord] Old " + LEGACY_NAME
76-
+ " renamed to " + LEGACY_NAME + ".backup");
77-
}
78-
} catch (IOException e) {
79-
plugin.getLogger().warning("[Discord] Migration failed: " + e.getMessage()
80-
+ " – a fresh " + FILE_NAME + " has been created instead.");
81-
}
25+
File file = new File(plugin.getDataFolder(), FILE_NAME);
26+
ConfigVersionService.updateFile(plugin, file, FILE_NAME, VERSION_KEY);
8227
}
8328
}

0 commit comments

Comments
 (0)