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 yamlBuilder

The string builder used for building YAML

+ * @param currentComment

The comment to add as a YAML string

+ * @param line

The current line

+ * @param previousIndentation

The indentation of the current block comment

+ * @param commentId

The id of the comment

+ */ + private void addYamlString(StringBuilder yamlBuilder, List currentComment, String line, + int previousIndentation, int commentId) { + String trimmed = line.trim(); + //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(); + } + //Add the non-comment line assuming it isn't empty + if (!trimmed.isEmpty()) { + yamlBuilder.append(line).append("\n"); + } + } + + /** + * Adds the given comment to the given list + * + * @param commentParts

The list to add to

+ * @param comment

The comment to add

+ */ + private void addComment(List commentParts, String comment) { + if (comment.startsWith("# ")) { + commentParts.add(comment.replaceFirst("# ", START_OF_COMMENT_LINE)); + } else { + commentParts.add(comment.replaceFirst("#", START_OF_COMMENT_LINE)); + } + } + /** * Generates a YAML-compatible string for one comment block * @@ -114,36 +140,49 @@ public class StargateYamlConfiguration extends YamlConfiguration { */ private String convertYAMLMappingsToComments(String yamlString) { StringBuilder finalText = new StringBuilder(); - boolean isReadingCommentBlock = false; - int commentIndentation = 0; - for (String line : yamlString.split("\n")) { + + String[] lines = yamlString.split("\n"); + for (int currentIndex = 0; currentIndex < lines.length; currentIndex++) { + String line = lines[currentIndex]; String possibleComment = line.trim(); - if (isReadingCommentBlock && line.contains(END_OF_COMMENT)) { - //Skip the line signifying the end of a comment - isReadingCommentBlock = false; - } else if (possibleComment.startsWith(START_OF_COMMENT)) { - //Skip the comment start line, and start comment parsing - isReadingCommentBlock = true; - //Get the indentation to use for the comment block - commentIndentation = getIndentation(line); + if (possibleComment.startsWith(START_OF_COMMENT)) { //Add an empty line before every comment block finalText.append("\n"); - } else if (line.isEmpty() && !isReadingCommentBlock) { - //Output the empty line as-is, as it's not part of a comment - finalText.append("\n"); - } else if (isReadingCommentBlock) { - possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, ""); - //Output the comment with correct indentation - finalText.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n"); + currentIndex = readComment(finalText, lines, currentIndex + 1, getIndentation(line)); } else { //Output the configuration key finalText.append(line).append("\n"); } } + return finalText.toString().trim(); } + /** + * Fully reads a comment + * + * @param builder

The string builder to write to

+ * @param lines

The lines to read from

+ * @param startIndex

The index to start reading from

+ * @param commentIndentation

The indentation of the read comment

+ * @return

The 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 *