tchristofferson / Config-Updater

Used to update files for Bukkit/Spigot API
MIT License
66 stars 9 forks source link

Ignored List Issue #12

Closed SpokenWig620933 closed 2 years ago

SpokenWig620933 commented 2 years ago

I updated to this version of the code and everything worked smoothly until I ignored a section with a list in it. When it rewrote the file, it completely removed the list and it's key.

When I added this:

if(trimmedLine.startsWith("-")) {
    for(String ignoredSection : ignoredSections) {
        boolean isIgnoredParent = ignoredSection.equals(keyBuilder.toString());

    if(isIgnoredParent || keyBuilder.isSubKeyOf(ignoredSection)) valueBuilder.append("\n" + line);

    continue lineLoop;
    }
}

to this:

private static Map<String, String> parseIgnoredSections(File toUpdate, FileConfiguration currentConfig, Map<String, String> comments, List<String> ignoredSections) throws IOException {
        Map<String, String> ignoredSectionsValues = new LinkedHashMap<>(ignoredSections.size());
        StringBuilder valueBuilder = new StringBuilder();
        String currentIgnoredSection = null;

        try(BufferedReader reader = new BufferedReader(new FileReader(toUpdate))) {
            KeyBuilder keyBuilder = new KeyBuilder(currentConfig, SEPARATOR);

            String line;
            lineLoop : while((line = reader.readLine()) != null) {
                String trimmedLine = line.trim();

                if(trimmedLine.isEmpty() || trimmedLine.startsWith("#")) continue;

                if(trimmedLine.startsWith("-")) {
                    for(String ignoredSection : ignoredSections) {
                        boolean isIgnoredParent = ignoredSection.equals(keyBuilder.toString());

                        if(isIgnoredParent || keyBuilder.isSubKeyOf(ignoredSection)) valueBuilder.append("\n" + line);

                        continue lineLoop;
                    }
                }

                keyBuilder.parseLine(trimmedLine);
                String fullKey = keyBuilder.toString();

                if(currentIgnoredSection != null && !KeyBuilder.isSubKeyOf(currentIgnoredSection, fullKey, SEPARATOR)) {
                    ignoredSectionsValues.put(currentIgnoredSection, valueBuilder.toString());
                    valueBuilder.setLength(0);
                    currentIgnoredSection = null;
                }

                for(String ignoredSection : ignoredSections) {
                    boolean isIgnoredParent = ignoredSection.equals(fullKey);

                    if(isIgnoredParent || keyBuilder.isSubKeyOf(ignoredSection)) {
                        if(valueBuilder.length() > 0) valueBuilder.append("\n");

                        String comment = comments.get(fullKey);

                        if(comment != null) {
                            String indents = KeyBuilder.getIndents(fullKey, SEPARATOR);
                            valueBuilder.append(indents).append(comment.replace("\n", "\n" + indents));
                            valueBuilder.setLength(valueBuilder.length() - indents.length());
                        }

                        valueBuilder.append(line);

                        if(isIgnoredParent) currentIgnoredSection = fullKey;

                        break;
                    }
                }
            }
        }

        if(valueBuilder.length() > 0) ignoredSectionsValues.put(currentIgnoredSection, valueBuilder.toString());

        return ignoredSectionsValues;
    }
It kept the lists, if you want to fix this.
tchristofferson commented 2 years ago

I see what you mean. If a list is in an ignored section the key is added but the list value is not added. I will try and work on a fix soon.

SpokenWig620933 commented 2 years ago

I did just give you a fix, but okay.

tchristofferson commented 2 years ago

Oh, I didn't notice you solved it for me. You should create a pull request to get credit for the code. It also makes it easier for me to check out your code.

tchristofferson commented 2 years ago

Fixed with pull request #13