Improves performance of StargateYamlConfiguration a bit

This commit is contained in:
2023-04-20 18:44:46 +02:00
parent 9d981f2cc6
commit 486149fa01

View File

@@ -28,11 +28,13 @@ public class StargateYamlConfiguration extends YamlConfiguration {
@Override @Override
public @NotNull String saveToString() { public @NotNull String saveToString() {
// Convert YAML comments to normal comments
return this.convertYAMLMappingsToComments(super.saveToString()); return this.convertYAMLMappingsToComments(super.saveToString());
} }
@Override @Override
public void loadFromString(@NotNull String contents) throws InvalidConfigurationException { public void loadFromString(@NotNull String contents) throws InvalidConfigurationException {
// Convert normal comments to YAML comments to prevent them from disappearing
super.loadFromString(this.convertCommentsToYAMLMappings(contents)); super.loadFromString(this.convertCommentsToYAMLMappings(contents));
} }
@@ -50,19 +52,23 @@ public class StargateYamlConfiguration extends YamlConfiguration {
int commentId = 0; int commentId = 0;
for (String line : configString.split("\n")) { for (String line : configString.split("\n")) {
if (line.trim().startsWith("#")) { String trimmed = line.trim();
if (trimmed.startsWith("#")) {
//Temporarily store the comment line //Temporarily store the comment line
currentComment.add(line.trim().replaceFirst(line.trim().startsWith("# ") ? "# " : "{2}#", if (trimmed.startsWith("# ")) {
START_OF_COMMENT_LINE)); 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 //Write the full formatted comment to the StringBuilder
if (!currentComment.isEmpty()) { if (!currentComment.isEmpty()) {
int indentation = getIndentation(line); int indentation = getIndentation(line);
generateCommentYAML(yamlBuilder, currentComment, commentId++, indentation); generateCommentYAML(yamlBuilder, currentComment, commentId++, indentation);
currentComment = new ArrayList<>(); currentComment.clear();
} }
//Add the non-comment line assuming it isn't empty //Add the non-comment line assuming it isn't empty
if (!line.trim().isEmpty()) { if (!trimmed.isEmpty()) {
yamlBuilder.append(line).append("\n"); yamlBuilder.append(line).append("\n");
} }
} }