remove YAML parsing hacks in favor of native bukkit provided parsing (#4714)

* fix yaml config to use native comment handling.
fixes #4702
replaces #4713
This commit is contained in:
the456gamer 2022-01-09 19:16:17 +00:00 committed by GitHub
parent d5ce8fc6ff
commit dd4a5a6b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 80 deletions

View File

@ -5,10 +5,9 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.util.HashMap;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
public abstract class AutoUpdateConfigLoader extends ConfigLoader {
@ -33,7 +32,9 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
protected void saveConfig() {
try {
mcMMO.p.getLogger().info("Saving changes to config file - "+fileName);
config.save(configFile);
YamlConfiguration yamlConfiguration = (YamlConfiguration) config;
yamlConfiguration.options().indent(4);
yamlConfiguration.save(configFile);
} catch (IOException e) {
e.printStackTrace();
}
@ -53,22 +54,25 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
boolean needSave = false;
// keys present in current config file that are not in the template
Set<String> oldKeys = new HashSet<>(configKeys);
oldKeys.removeAll(internalConfigKeys);
if (!oldKeys.isEmpty()) {
mcMMO.p.debug("old key(s) in \"" +fileName+"\"");
for (String key : oldKeys) {
mcMMO.p.debug(" old-key:" + key);
}
}
// keys present in template that are not in current file
Set<String> newKeys = new HashSet<>(internalConfigKeys);
newKeys.removeAll(configKeys);
// Don't need a re-save if we have old keys sticking around?
// Would be less saving, but less... correct?
if (!newKeys.isEmpty() || !oldKeys.isEmpty()) {
if (!newKeys.isEmpty()) {
needSave = true;
}
//
// for (String key : oldKeys) {
// mcMMO.p.debug("Detected potentially unused key: " + key);
// //config.set(key, null);
// }
for (String key : newKeys) {
mcMMO.p.debug("Adding new key: " + key + " = " + internalConfig.get(key));
@ -76,70 +80,8 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
}
if (needSave) {
// Get Bukkit's version of an acceptable config with new keys, and no old keys
String output = config.saveToString();
// Convert to the superior 4 space indentation
output = output.replace(" ", " ");
// Rip out Bukkit's attempt to save comments at the top of the file
while (output.replaceAll("[//s]", "").startsWith("#")) {
output = output.substring(output.indexOf('\n', output.indexOf('#')) + 1);
}
// Read the internal config to get comments, then put them in the new one
try {
// Read internal
BufferedReader reader = new BufferedReader(new InputStreamReader(mcMMO.p.getResource(fileName)));
LinkedHashMap<String, String> comments = new LinkedHashMap<>();
StringBuilder temp = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("#")) {
temp.append(line).append("\n");
}
else if (line.contains(":")) {
line = line.substring(0, line.indexOf(":") + 1);
if (temp.length() > 0) {
if(comments.containsKey(line)) {
int index = 0;
while(comments.containsKey(line + index)) {
index++;
}
line = line + index;
}
comments.put(line, temp.toString());
temp = new StringBuilder();
}
}
}
// Dump to the new one
HashMap<String, Integer> indexed = new HashMap<>();
for (String key : comments.keySet()) {
String actualkey = key.substring(0, key.indexOf(":") + 1);
int index = 0;
if(indexed.containsKey(actualkey)) {
index = indexed.get(actualkey);
}
boolean isAtTop = !output.contains("\n" + actualkey);
index = output.indexOf((isAtTop ? "" : "\n") + actualkey, index);
if (index >= 0) {
output = output.substring(0, index) + "\n" + comments.get(key) + output.substring(isAtTop ? index : index + 1);
indexed.put(actualkey, index + comments.get(key).length() + actualkey.length() + 1);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
// Save it
if(dataFolder == null) {
mcMMO.p.getLogger().severe("Data folder should never be null!");
return;
@ -153,11 +95,10 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
}
File newSaveFile = new File(dataFolder, saveName);
FileWriter fileWriter = new FileWriter(newSaveFile.getAbsolutePath());
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(output);
writer.flush();
writer.close();
YamlConfiguration yamlConfiguration = (YamlConfiguration) config;
yamlConfiguration.options().indent(4);
yamlConfiguration.save(newSaveFile);
}
catch (Exception e) {
e.printStackTrace();