diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index fefaf62..dce9109 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -459,11 +459,13 @@ public final class StargateConfig { * @param currentConfiguration
The current config to back up
*/ private void migrateConfig(FileConfiguration currentConfiguration) { + String debugPath = "StargateConfig::migrateConfig"; + //Save the old config just in case something goes wrong try { currentConfiguration.save(new File(dataFolderPath, "config.yml.old")); } catch (IOException e) { - Stargate.debug("StargateConfig::migrateConfig", "Unable to save old backup and do migration"); + Stargate.debug(debugPath, "Unable to save old backup and do migration"); return; } @@ -481,7 +483,7 @@ public final class StargateConfig { FileHelper.getInputStreamForInternalFile("/config-migrations.txt")), "=", ColorConversion.NORMAL); } catch (IOException e) { - Stargate.debug("StargateConfig::migrateConfig", "Unable to load config migration file"); + Stargate.debug(debugPath, "Unable to load config migration file"); return; } @@ -502,7 +504,7 @@ public final class StargateConfig { if (oldConfiguration.get(key) instanceof MemorySection) { continue; } - Stargate.debug("StargateConfig::migrateConfig", "Setting " + key + " to " + + Stargate.debug(debugPath, "Setting " + key + " to " + oldConfiguration.get(key)); newConfiguration.set(key, oldConfiguration.get(key)); } @@ -510,7 +512,7 @@ public final class StargateConfig { try { newConfiguration.save(new File(dataFolderPath, "config.yml")); } catch (IOException exception) { - Stargate.debug("StargateConfig::migrateConfig", "Unable to save migrated config"); + Stargate.debug(debugPath, "Unable to save migrated config"); } Stargate.getInstance().reloadConfig(); diff --git a/src/main/java/net/knarcraft/stargate/config/StargateYamlConfiguration.java b/src/main/java/net/knarcraft/stargate/config/StargateYamlConfiguration.java index 400f5e2..9e7bfd0 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateYamlConfiguration.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateYamlConfiguration.java @@ -60,28 +60,54 @@ public class StargateYamlConfiguration extends YamlConfiguration { previousIndentation = getIndentation(line); } //Temporarily store the comment line - if (trimmed.startsWith("# ")) { - currentComment.add(trimmed.replaceFirst("# ", START_OF_COMMENT_LINE)); - } else { - currentComment.add(trimmed.replaceFirst("#", START_OF_COMMENT_LINE)); - } + addComment(currentComment, trimmed); } else { - //Write the full formatted comment to the StringBuilder - if (!currentComment.isEmpty()) { - int indentation = trimmed.isEmpty() ? previousIndentation : getIndentation(line); - generateCommentYAML(yamlBuilder, currentComment, commentId++, indentation); - currentComment.clear(); - previousIndentation = 0; - } - //Add the non-comment line assuming it isn't empty - if (!trimmed.isEmpty()) { - yamlBuilder.append(line).append("\n"); - } + addYamlString(yamlBuilder, currentComment, line, previousIndentation, commentId); + commentId++; + previousIndentation = 0; } } return yamlBuilder.toString(); } + /** + * Adds a YAML string to the given string builder + * + * @param yamlBuilderThe string builder used for building YAML
+ * @param currentCommentThe comment to add as a YAML string
+ * @param lineThe current line
+ * @param previousIndentationThe indentation of the current block comment
+ * @param commentIdThe id of the comment
+ */ + private void addYamlString(StringBuilder yamlBuilder, ListThe list to add to
+ * @param commentThe comment to add
+ */ + private void addComment(ListThe string builder to write to
+ * @param linesThe lines to read from
+ * @param startIndexThe index to start reading from
+ * @param commentIndentationThe indentation of the read comment
+ * @returnThe index containing the next non-comment line
+ */ + private int readComment(StringBuilder builder, String[] lines, int startIndex, int commentIndentation) { + for (int currentIndex = startIndex; currentIndex < lines.length; currentIndex++) { + String line = lines[currentIndex]; + String possibleComment = line.trim(); + if (!line.contains(END_OF_COMMENT)) { + possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, ""); + builder.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n"); + } else { + return currentIndex; + } + } + + return startIndex; + } + /** * Gets a string containing the given indentation *