/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.volumetricpixels.questy.loading.loaders;

import org.yaml.snakeyaml.Yaml;

import com.volumetricpixels.questy.QuestManager;
import com.volumetricpixels.questy.loading.QuestLoader;
import com.volumetricpixels.questy.loading.QuestLoading;
import com.volumetricpixels.questy.loading.QuestLoading.ObjectiveBuilder;
import com.volumetricpixels.questy.loading.QuestLoading.OutcomeBuilder;
import com.volumetricpixels.questy.loading.QuestLoading.QuestBuilder;
import com.volumetricpixels.questy.quest.Quest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Loads YML quests.
 */
public class YMLQuestLoader implements QuestLoader {
    private final QuestManager questManager;

    public YMLQuestLoader(QuestManager questManager) {
        this.questManager = questManager;
    }

    @Override
    public Set<Quest> loadQuests(File directory) {
        if (directory == null || !directory.isDirectory()) {
            return null;
        }

        Yaml yaml = new Yaml();
        Set<Quest> result = new HashSet<>();
        // iterate through all files in the directory which end with .yml
        for (File file : directory.listFiles(fl -> endsWith(fl, ".yml"))) {
            try {
                result.add(loadQuest(yaml, new FileInputStream(file)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public Quest loadQuest(Yaml yaml, InputStream stream) {
        Map<?, ?> map = (Map<?, ?>) yaml.load(stream);

        QuestBuilder builder = QuestLoading
                .quest(questManager, map.get("name").toString());
        builder.description(map.get("description").toString());

        Map<?, ?> objectives = (Map) map.get("objectives");
        for (Entry<?, ?> objEntry : objectives.entrySet()) {
            ObjectiveBuilder obj = builder
                    .objective(objEntry.getKey().toString());
            Map<?, ?> objMap = (Map) objEntry.getValue();
            obj.description(objMap.get("description").toString());

            Map<?, ?> outcomes = (Map) objMap.get("outcomes");
            for (Entry<?, ?> ocEntry : outcomes.entrySet()) {
                OutcomeBuilder oc = obj
                        .outcome(ocEntry.getKey().toString());
                Map<?, ?> ocMap = (Map) ocEntry.getValue();
                oc.description(ocMap.get("description").toString());
                oc.type(ocMap.get("type").toString());

                Object next = ocMap.get("next");
                if (next != null) {
                    oc.next(builder.objective(next.toString()));
                }
            }
        }

        return builder.build();
    }

    private Map convert(Map<?, ?> map) {
        Map<Object, Object> result = new HashMap<>();
        for (Entry<?, ?> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            Object value = entry.getValue();

            if (value instanceof Map) {
                result.put(key, convert((Map<?, ?>) value));
            } else {
                result.put(key, value);
            }
        }
        return result;
    }

    @Override
    public String getQuestFormat() {
        return "YAML";
    }
}

@@ -3103,468 +3103,8 @@
  %7D%0A%0A
-    private Map convert(Map%3C?, ?%3E map) %7B%0A        Map%3CObject, Object%3E result = new HashMap%3C%3E();%0A        for (Entry%3C?, ?%3E entry : map.entrySet()) %7B%0A            String key = entry.getKey().toString();%0A            Object value = entry.getValue();%0A%0A            if (value instanceof Map) %7B%0A                result.put(key, convert((Map%3C?, ?%3E) value));%0A            %7D else %7B%0A                result.put(key, value);%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A
     

Remove redundant method