-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConfigMigrator.java
More file actions
224 lines (196 loc) · 8.92 KB
/
ConfigMigrator.java
File metadata and controls
224 lines (196 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2022-2025 Noah Ross
*
* This file is part of PerPlayerKit.
*
* PerPlayerKit is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* PerPlayerKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PerPlayerKit. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.noah.perplayerkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Migrates legacy config.yml (config-version 1) to the new layout (config-version 2).
*
* v1 stored translatable strings (prefix, motd.message, scheduled-broadcast.messages,
* messages.*.message, disabled-command-message) directly in config.yml. v2 moves them
* to lang files. If the user customized any of those values, this migrator writes them
* into plugins/PerPlayerKit/lang/en.yml so their changes survive the upgrade.
*
* Runs before Lang is initialized.
*/
public class ConfigMigrator {
public static final int CURRENT_VERSION = 2;
private final Plugin plugin;
public ConfigMigrator(Plugin plugin) {
this.plugin = plugin;
}
public void migrate() {
File configFile = new File(plugin.getDataFolder(), "config.yml");
if (!configFile.exists()) {
return;
}
FileConfiguration userConfig = YamlConfiguration.loadConfiguration(configFile);
int version = userConfig.getInt("config-version", 1);
if (version >= CURRENT_VERSION) {
return;
}
plugin.getLogger().info("Migrating config from v" + version + " to v" + CURRENT_VERSION);
Map<String, Object> savedCustomizations = extractCustomizedStrings(userConfig);
if (!savedCustomizations.isEmpty()) {
writeCustomizationsToLangFile(savedCustomizations);
plugin.getLogger().info("Preserved " + savedCustomizations.size()
+ " customized string(s) from config.yml into lang/en.yml");
}
userConfig.set("prefix", null);
userConfig.set("disabled-command-message", null);
if (userConfig.isConfigurationSection("motd")) {
userConfig.set("motd.message", null);
}
if (userConfig.isConfigurationSection("scheduled-broadcast")) {
userConfig.set("scheduled-broadcast.messages", null);
}
if (userConfig.isConfigurationSection("messages")) {
for (String key : userConfig.getConfigurationSection("messages").getKeys(false)) {
if (userConfig.isConfigurationSection("messages." + key)) {
userConfig.set("messages." + key + ".message", null);
}
}
}
userConfig.set("config-version", CURRENT_VERSION);
if (!userConfig.contains("language")) {
userConfig.set("language", "en");
}
try {
userConfig.save(configFile);
plugin.getLogger().info("config.yml migrated to v" + CURRENT_VERSION);
} catch (IOException e) {
plugin.getLogger().severe("Failed to save migrated config.yml: " + e.getMessage());
}
}
private Map<String, Object> extractCustomizedStrings(FileConfiguration userConfig) {
FileConfiguration defaults = loadBundledDefaults();
Map<String, Object> diffs = new LinkedHashMap<>();
copyIfCustomized(userConfig, defaults, "prefix", diffs);
if (userConfig.contains("motd.message")) {
List<String> current = userConfig.getStringList("motd.message");
List<String> defaultsList = bundledMotdDefault();
if (!current.equals(defaultsList) && !current.isEmpty()) {
diffs.put("motd.message", new ArrayList<>(current));
}
}
if (userConfig.contains("scheduled-broadcast.messages")) {
List<String> current = userConfig.getStringList("scheduled-broadcast.messages");
List<String> defaultsList = bundledBroadcastDefault();
if (!current.equals(defaultsList) && !current.isEmpty()) {
diffs.put("scheduled-broadcast.messages", new ArrayList<>(current));
}
}
if (userConfig.contains("disabled-command-message")) {
String current = userConfig.getString("disabled-command-message");
String defaultValue = defaults.getString("error.disabled-in-world");
if (current != null && !current.equals(defaultValue)) {
diffs.put("error.disabled-in-world", current);
}
}
Map<String, String> broadcastKeyMap = Map.of(
"messages.player-repaired.message", "broadcast-messages.player-repaired",
"messages.player-healed.message", "broadcast-messages.player-healed",
"messages.player-opened-kit-room.message", "broadcast-messages.player-opened-kit-room",
"messages.player-loaded-private-kit.message", "broadcast-messages.player-loaded-private-kit",
"messages.player-loaded-public-kit.message", "broadcast-messages.player-loaded-public-kit",
"messages.player-loaded-enderchest.message", "broadcast-messages.player-loaded-enderchest",
"messages.player-copied-kit.message", "broadcast-messages.player-copied-kit",
"messages.player-copied-ec.message", "broadcast-messages.player-copied-ec",
"messages.player-regeared.message", "broadcast-messages.player-regeared"
);
for (Map.Entry<String, String> entry : broadcastKeyMap.entrySet()) {
String oldPath = entry.getKey();
String newKey = entry.getValue();
if (!userConfig.contains(oldPath)) {
continue;
}
String current = userConfig.getString(oldPath);
String defaultValue = defaults.getString(newKey);
if (current != null && !current.equals(defaultValue)) {
diffs.put(newKey, current);
}
}
return diffs;
}
private void copyIfCustomized(FileConfiguration user, FileConfiguration defaults, String path,
Map<String, Object> out) {
Object current = user.get(path);
Object defaultValue = defaults.get(path);
if (current == null) {
return;
}
if (defaultValue != null && defaultValue.equals(current)) {
return;
}
out.put(path, current);
}
private void writeCustomizationsToLangFile(Map<String, Object> customizations) {
File langDir = new File(plugin.getDataFolder(), "lang");
if (!langDir.exists() && !langDir.mkdirs()) {
plugin.getLogger().warning("Failed to create lang directory");
return;
}
File langFile = new File(langDir, "en.yml");
if (!langFile.exists()) {
try (java.io.InputStream in = plugin.getResource("lang/en.yml")) {
if (in != null) {
Files.copy(in, langFile.toPath());
}
} catch (IOException e) {
plugin.getLogger().warning("Failed to copy bundled lang/en.yml: " + e.getMessage());
return;
}
}
YamlConfiguration langConfig = YamlConfiguration.loadConfiguration(langFile);
for (Map.Entry<String, Object> e : customizations.entrySet()) {
langConfig.set(e.getKey(), e.getValue());
}
try {
langConfig.save(langFile);
} catch (IOException ex) {
plugin.getLogger().severe("Failed to save customizations to lang/en.yml: " + ex.getMessage());
}
}
private FileConfiguration loadBundledDefaults() {
try (java.io.InputStream in = plugin.getResource("lang/en.yml")) {
if (in == null) {
return new YamlConfiguration();
}
return YamlConfiguration.loadConfiguration(new InputStreamReader(in, StandardCharsets.UTF_8));
} catch (IOException e) {
return new YamlConfiguration();
}
}
private List<String> bundledMotdDefault() {
return loadBundledDefaults().getStringList("motd.message");
}
private List<String> bundledBroadcastDefault() {
return loadBundledDefaults().getStringList("scheduled-broadcast.messages");
}
}