22
33import github .nighter .smartspawner .SmartSpawner ;
44import github .nighter .smartspawner .updates .ConfigVersionService ;
5- import org .bukkit .configuration .file .FileConfiguration ;
6- import org .bukkit .configuration .file .YamlConfiguration ;
75
86import 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 */
2012public 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