Reduces the complexity of convertYAMLMappingsToComments
This commit is contained in:
@@ -459,11 +459,13 @@ public final class StargateConfig {
|
|||||||
* @param currentConfiguration <p>The current config to back up</p>
|
* @param currentConfiguration <p>The current config to back up</p>
|
||||||
*/
|
*/
|
||||||
private void migrateConfig(FileConfiguration currentConfiguration) {
|
private void migrateConfig(FileConfiguration currentConfiguration) {
|
||||||
|
String debugPath = "StargateConfig::migrateConfig";
|
||||||
|
|
||||||
//Save the old config just in case something goes wrong
|
//Save the old config just in case something goes wrong
|
||||||
try {
|
try {
|
||||||
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
|
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
|
||||||
} catch (IOException e) {
|
} 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -481,7 +483,7 @@ public final class StargateConfig {
|
|||||||
FileHelper.getInputStreamForInternalFile("/config-migrations.txt")), "=",
|
FileHelper.getInputStreamForInternalFile("/config-migrations.txt")), "=",
|
||||||
ColorConversion.NORMAL);
|
ColorConversion.NORMAL);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Stargate.debug("StargateConfig::migrateConfig", "Unable to load config migration file");
|
Stargate.debug(debugPath, "Unable to load config migration file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -502,7 +504,7 @@ public final class StargateConfig {
|
|||||||
if (oldConfiguration.get(key) instanceof MemorySection) {
|
if (oldConfiguration.get(key) instanceof MemorySection) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Stargate.debug("StargateConfig::migrateConfig", "Setting " + key + " to " +
|
Stargate.debug(debugPath, "Setting " + key + " to " +
|
||||||
oldConfiguration.get(key));
|
oldConfiguration.get(key));
|
||||||
newConfiguration.set(key, oldConfiguration.get(key));
|
newConfiguration.set(key, oldConfiguration.get(key));
|
||||||
}
|
}
|
||||||
@@ -510,7 +512,7 @@ public final class StargateConfig {
|
|||||||
try {
|
try {
|
||||||
newConfiguration.save(new File(dataFolderPath, "config.yml"));
|
newConfiguration.save(new File(dataFolderPath, "config.yml"));
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
Stargate.debug("StargateConfig::migrateConfig", "Unable to save migrated config");
|
Stargate.debug(debugPath, "Unable to save migrated config");
|
||||||
}
|
}
|
||||||
|
|
||||||
Stargate.getInstance().reloadConfig();
|
Stargate.getInstance().reloadConfig();
|
||||||
|
@@ -60,28 +60,54 @@ public class StargateYamlConfiguration extends YamlConfiguration {
|
|||||||
previousIndentation = getIndentation(line);
|
previousIndentation = getIndentation(line);
|
||||||
}
|
}
|
||||||
//Temporarily store the comment line
|
//Temporarily store the comment line
|
||||||
if (trimmed.startsWith("# ")) {
|
addComment(currentComment, trimmed);
|
||||||
currentComment.add(trimmed.replaceFirst("# ", START_OF_COMMENT_LINE));
|
|
||||||
} else {
|
|
||||||
currentComment.add(trimmed.replaceFirst("#", START_OF_COMMENT_LINE));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//Write the full formatted comment to the StringBuilder
|
addYamlString(yamlBuilder, currentComment, line, previousIndentation, commentId);
|
||||||
if (!currentComment.isEmpty()) {
|
commentId++;
|
||||||
int indentation = trimmed.isEmpty() ? previousIndentation : getIndentation(line);
|
previousIndentation = 0;
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return yamlBuilder.toString();
|
return yamlBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a YAML string to the given string builder
|
||||||
|
*
|
||||||
|
* @param yamlBuilder <p>The string builder used for building YAML</p>
|
||||||
|
* @param currentComment <p>The comment to add as a YAML string</p>
|
||||||
|
* @param line <p>The current line</p>
|
||||||
|
* @param previousIndentation <p>The indentation of the current block comment</p>
|
||||||
|
* @param commentId <p>The id of the comment</p>
|
||||||
|
*/
|
||||||
|
private void addYamlString(StringBuilder yamlBuilder, List<String> 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 <p>The list to add to</p>
|
||||||
|
* @param comment <p>The comment to add</p>
|
||||||
|
*/
|
||||||
|
private void addComment(List<String> 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
|
* Generates a YAML-compatible string for one comment block
|
||||||
*
|
*
|
||||||
@@ -114,36 +140,49 @@ public class StargateYamlConfiguration extends YamlConfiguration {
|
|||||||
*/
|
*/
|
||||||
private String convertYAMLMappingsToComments(String yamlString) {
|
private String convertYAMLMappingsToComments(String yamlString) {
|
||||||
StringBuilder finalText = new StringBuilder();
|
StringBuilder finalText = new StringBuilder();
|
||||||
boolean isReadingCommentBlock = false;
|
|
||||||
int commentIndentation = 0;
|
String[] lines = yamlString.split("\n");
|
||||||
for (String line : yamlString.split("\n")) {
|
for (int currentIndex = 0; currentIndex < lines.length; currentIndex++) {
|
||||||
|
String line = lines[currentIndex];
|
||||||
String possibleComment = line.trim();
|
String possibleComment = line.trim();
|
||||||
|
|
||||||
if (isReadingCommentBlock && line.contains(END_OF_COMMENT)) {
|
if (possibleComment.startsWith(START_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);
|
|
||||||
//Add an empty line before every comment block
|
//Add an empty line before every comment block
|
||||||
finalText.append("\n");
|
finalText.append("\n");
|
||||||
} else if (line.isEmpty() && !isReadingCommentBlock) {
|
currentIndex = readComment(finalText, lines, currentIndex + 1, getIndentation(line));
|
||||||
//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");
|
|
||||||
} else {
|
} else {
|
||||||
//Output the configuration key
|
//Output the configuration key
|
||||||
finalText.append(line).append("\n");
|
finalText.append(line).append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalText.toString().trim();
|
return finalText.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fully reads a comment
|
||||||
|
*
|
||||||
|
* @param builder <p>The string builder to write to</p>
|
||||||
|
* @param lines <p>The lines to read from</p>
|
||||||
|
* @param startIndex <p>The index to start reading from</p>
|
||||||
|
* @param commentIndentation <p>The indentation of the read comment</p>
|
||||||
|
* @return <p>The index containing the next non-comment line</p>
|
||||||
|
*/
|
||||||
|
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
|
* Gets a string containing the given indentation
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user