Dateien nach "src/main/java/de/nexuslobby/utils" hochladen
This commit is contained in:
78
src/main/java/de/nexuslobby/utils/ConfigUpdater.java
Normal file
78
src/main/java/de/nexuslobby/utils/ConfigUpdater.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package de.nexuslobby.utils;
|
||||
|
||||
import de.nexuslobby.NexusLobby;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigUpdater {
|
||||
|
||||
public static void updateConfig(String fileName) {
|
||||
NexusLobby plugin = NexusLobby.getInstance();
|
||||
File configFile = new File(plugin.getDataFolder(), fileName);
|
||||
|
||||
if (!configFile.exists()) {
|
||||
plugin.saveResource(fileName, false);
|
||||
return;
|
||||
}
|
||||
|
||||
FileConfiguration serverConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
List<String> newFileContent = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String trimmed = line.trim();
|
||||
|
||||
// 1. Kommentare, Leerzeilen und Listen-Elemente direkt übernehmen
|
||||
if (trimmed.startsWith("#") || trimmed.isEmpty() || trimmed.startsWith("-")) {
|
||||
newFileContent.add(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2. Prüfen ob es ein Key ist
|
||||
if (trimmed.contains(":") && !trimmed.startsWith("-")) {
|
||||
String key = trimmed.split(":")[0];
|
||||
|
||||
// WICHTIG: Nur wenn der Key KEINE Sektion ist, darf er überschrieben werden
|
||||
if (serverConfig.contains(key) && !serverConfig.isConfigurationSection(key)) {
|
||||
String indentation = line.substring(0, line.indexOf(trimmed));
|
||||
Object value = serverConfig.get(key);
|
||||
newFileContent.add(indentation + key + ": " + formatValue(value));
|
||||
} else {
|
||||
// Es ist eine Sektion oder ein neuer Key -> Original aus JAR übernehmen
|
||||
newFileContent.add(line);
|
||||
}
|
||||
} else {
|
||||
newFileContent.add(line);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 3. Datei sauber speichern
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) {
|
||||
for (String outputLine : newFileContent) {
|
||||
writer.write(outputLine);
|
||||
writer.newLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().severe("Konnte " + fileName + " nicht speichern!");
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatValue(Object value) {
|
||||
if (value == null) return "";
|
||||
if (value instanceof String) {
|
||||
// Verhindert das "Zerschießen" der Config durch Sonderzeichen
|
||||
String s = (String) value;
|
||||
return "\"" + s.replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user