From 529c43b0e6bffa3fa96b36dcc284cf411ff88202 Mon Sep 17 00:00:00 2001 From: Dor Date: Sat, 12 Mar 2016 05:10:41 +0200 Subject: [PATCH] Fix config auto update. --- .../config/AutoUpdateConfigLoader.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java b/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java index bf95cb291..7ff29d1eb 100644 --- a/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java +++ b/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.HashSet; import java.util.Set; @@ -64,15 +65,12 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader { while (output.replaceAll("[//s]", "").startsWith("#")) { output = output.substring(output.indexOf('\n', output.indexOf('#')) + 1); } - - String[] keys = output.split("\n"); - // Read the internal config to get comments, then put them in the new one try { // Read internal BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName))); - HashMap comments = new HashMap(); + LinkedHashMap comments = new LinkedHashMap(); String temp = ""; String line; @@ -83,21 +81,37 @@ public abstract class AutoUpdateConfigLoader extends ConfigLoader { else if (line.contains(":")) { line = line.substring(0, line.indexOf(":") + 1); if (!temp.isEmpty()) { + if(comments.containsKey(line)) { + int index = 0; + while(comments.containsKey(line + index)) { + index++; + } + + line = line + index; + } + comments.put(line, temp); temp = ""; } } } - output = ""; // Dump to the new one - for (String key : keys) { - String comment = comments.get(key.substring(0, key.indexOf(":") + 1)); - if (comment != null) { - output += comment; + HashMap 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); } - output += key; - output += "\n"; } } catch (Exception e) {