mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-22 13:16:45 +01:00
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:
parent
d5ce8fc6ff
commit
dd4a5a6b9a
@ -5,10 +5,9 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
||||||
@ -33,7 +32,9 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
|||||||
protected void saveConfig() {
|
protected void saveConfig() {
|
||||||
try {
|
try {
|
||||||
mcMMO.p.getLogger().info("Saving changes to config file - "+fileName);
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -53,22 +54,25 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
|||||||
|
|
||||||
boolean needSave = false;
|
boolean needSave = false;
|
||||||
|
|
||||||
|
// keys present in current config file that are not in the template
|
||||||
Set<String> oldKeys = new HashSet<>(configKeys);
|
Set<String> oldKeys = new HashSet<>(configKeys);
|
||||||
oldKeys.removeAll(internalConfigKeys);
|
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);
|
Set<String> newKeys = new HashSet<>(internalConfigKeys);
|
||||||
newKeys.removeAll(configKeys);
|
newKeys.removeAll(configKeys);
|
||||||
|
|
||||||
// Don't need a re-save if we have old keys sticking around?
|
if (!newKeys.isEmpty()) {
|
||||||
// Would be less saving, but less... correct?
|
|
||||||
if (!newKeys.isEmpty() || !oldKeys.isEmpty()) {
|
|
||||||
needSave = true;
|
needSave = true;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// for (String key : oldKeys) {
|
|
||||||
// mcMMO.p.debug("Detected potentially unused key: " + key);
|
|
||||||
// //config.set(key, null);
|
|
||||||
// }
|
|
||||||
|
|
||||||
for (String key : newKeys) {
|
for (String key : newKeys) {
|
||||||
mcMMO.p.debug("Adding new key: " + key + " = " + internalConfig.get(key));
|
mcMMO.p.debug("Adding new key: " + key + " = " + internalConfig.get(key));
|
||||||
@ -76,70 +80,8 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (needSave) {
|
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
|
// Save it
|
||||||
|
|
||||||
if(dataFolder == null) {
|
if(dataFolder == null) {
|
||||||
mcMMO.p.getLogger().severe("Data folder should never be null!");
|
mcMMO.p.getLogger().severe("Data folder should never be null!");
|
||||||
return;
|
return;
|
||||||
@ -153,11 +95,10 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File newSaveFile = new File(dataFolder, saveName);
|
File newSaveFile = new File(dataFolder, saveName);
|
||||||
FileWriter fileWriter = new FileWriter(newSaveFile.getAbsolutePath());
|
YamlConfiguration yamlConfiguration = (YamlConfiguration) config;
|
||||||
BufferedWriter writer = new BufferedWriter(fileWriter);
|
yamlConfiguration.options().indent(4);
|
||||||
writer.write(output);
|
yamlConfiguration.save(newSaveFile);
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
Reference in New Issue
Block a user