Skip to content

Commit 3735e78

Browse files
committed
Sync missing language keys into existing lang files without overwriting
1 parent 94b2bde commit 3735e78

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/main/java/me/crafter/mc/lockettepro/Config.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import java.io.File;
1212
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.InputStreamReader;
15+
import java.nio.charset.StandardCharsets;
1316
import java.util.*;
1417

1518
public class Config {
@@ -278,6 +281,35 @@ public static void initAdditionalFiles() {
278281
if (!langfile.exists()) {
279282
plugin.saveResource(filename, false);
280283
}
284+
updateMissingLanguageKeys(filename);
285+
}
286+
}
287+
288+
private static void updateMissingLanguageKeys(String filename) {
289+
InputStream stream = plugin.getResource(filename);
290+
if (stream == null) {
291+
return;
292+
}
293+
294+
File file = new File(plugin.getDataFolder(), filename);
295+
try (InputStream input = stream; InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
296+
YamlConfiguration bundled = YamlConfiguration.loadConfiguration(reader);
297+
YamlConfiguration existing = YamlConfiguration.loadConfiguration(file);
298+
299+
boolean changed = false;
300+
for (String key : bundled.getKeys(true)) {
301+
if (bundled.isConfigurationSection(key)) continue;
302+
if (!existing.contains(key)) {
303+
existing.set(key, bundled.get(key));
304+
changed = true;
305+
}
306+
}
307+
308+
if (changed) {
309+
existing.save(file);
310+
}
311+
} catch (Exception e) {
312+
plugin.getLogger().warning("Failed to update missing language keys for " + filename + ": " + e.getMessage());
281313
}
282314
}
283315

0 commit comments

Comments
 (0)