diff options
Diffstat (limited to 'src/main/java/com/leonardobishop')
| -rw-r--r-- | src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java | 252 |
1 files changed, 133 insertions, 119 deletions
diff --git a/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java index a20663aa..05e94e8f 100644 --- a/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java +++ b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java @@ -16,9 +16,18 @@ import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.IOException; import java.net.URI; -import java.nio.file.*; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -79,154 +88,159 @@ public class QuestsConfigLoader { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) { - File questFile = new File(path.toUri()); - URI relativeLocation = questsRoot.relativize(path.toUri()); + try { + File questFile = new File(path.toUri()); + URI relativeLocation = questsRoot.relativize(path.toUri()); - if (!questFile.getName().toLowerCase().endsWith(".yml")) return FileVisitResult.CONTINUE; + if (!questFile.getName().toLowerCase().endsWith(".yml")) return FileVisitResult.CONTINUE; - YamlConfiguration config = new YamlConfiguration(); - // test QUEST file integrity - try { - config.load(questFile); - } catch (Exception ex) { - filesWithProblems.put(relativeLocation.getPath(), Collections.singletonList(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.MALFORMED_YAML.getDescription()))); - return FileVisitResult.CONTINUE; - } + YamlConfiguration config = new YamlConfiguration(); + // test QUEST file integrity + try { + config.load(questFile); + } catch (Exception ex) { + filesWithProblems.put(relativeLocation.getPath(), Collections.singletonList(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.MALFORMED_YAML.getDescription()))); + return FileVisitResult.CONTINUE; + } - String id = questFile.getName().replace(".yml", ""); + String id = questFile.getName().replace(".yml", ""); - List<ConfigProblem> configProblems = new ArrayList<>(); + List<ConfigProblem> configProblems = new ArrayList<>(); - if (!StringUtils.isAlphanumeric(id)) { - configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.INVALID_QUEST_ID.getDescription(id))); - } + if (!StringUtils.isAlphanumeric(id)) { + configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.INVALID_QUEST_ID.getDescription(id))); + } - // CHECK EVERYTHING WRONG WITH THE QUEST FILE BEFORE ACTUALLY LOADING THE QUEST + // CHECK EVERYTHING WRONG WITH THE QUEST FILE BEFORE ACTUALLY LOADING THE QUEST - if (!config.isConfigurationSection("tasks")) { - configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.NO_TASKS.getDescription(), "tasks")); - } else { //continue - int validTasks = 0; - for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { - boolean isValid = true; - String taskRoot = "tasks." + taskId; - String taskType = config.getString(taskRoot + ".type"); + if (!config.isConfigurationSection("tasks")) { + configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.NO_TASKS.getDescription(), "tasks")); + } else { //continue + int validTasks = 0; + for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { + boolean isValid = true; + String taskRoot = "tasks." + taskId; + String taskType = config.getString(taskRoot + ".type"); - if (!config.isConfigurationSection(taskRoot)) { - configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.TASK_MALFORMED_NOT_SECTION.getDescription(taskId), taskRoot)); - continue; - } + if (!config.isConfigurationSection(taskRoot)) { + configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.TASK_MALFORMED_NOT_SECTION.getDescription(taskId), taskRoot)); + continue; + } - if (taskType == null) { - configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.NO_TASK_TYPE.getDescription(), taskRoot)); - continue; - } + if (taskType == null) { + configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.NO_TASK_TYPE.getDescription(), taskRoot)); + continue; + } - // check the tasks - TaskType t = plugin.getTaskTypeManager().getTaskType(taskType); - if (t != null) { - HashMap<String, Object> configValues = new HashMap<>(); - for (String key : config.getConfigurationSection(taskRoot).getKeys(false)) { - configValues.put(key, config.get(taskRoot + "." + key)); + // check the tasks + TaskType t = plugin.getTaskTypeManager().getTaskType(taskType); + if (t != null) { + HashMap<String, Object> configValues = new HashMap<>(); + for (String key : config.getConfigurationSection(taskRoot).getKeys(false)) { + configValues.put(key, config.get(taskRoot + "." + key)); + } + + configProblems.addAll(t.detectProblemsInConfig(taskRoot, configValues)); + } else { + configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.UNKNOWN_TASK_TYPE.getDescription(taskType), taskRoot)); + isValid = false; } - configProblems.addAll(t.detectProblemsInConfig(taskRoot, configValues)); - } else { - configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.UNKNOWN_TASK_TYPE.getDescription(taskType), taskRoot)); - isValid = false; + if (isValid) { + validTasks++; + } } - - if (isValid) { - validTasks++; + if (validTasks == 0) { + configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.NO_TASKS.getDescription(), "tasks")); } } - if (validTasks == 0) { - configProblems.add(new ConfigProblem(ConfigProblemType.ERROR, ConfigProblemDescriptions.NO_TASKS.getDescription(), "tasks")); - } - } - boolean error = false; - for (ConfigProblem problem : configProblems) { - if (problem.getType() == ConfigProblemType.ERROR) { - error = true; - break; + boolean error = false; + for (ConfigProblem problem : configProblems) { + if (problem.getType() == ConfigProblemType.ERROR) { + error = true; + break; + } } - } - // END OF THE CHECKING + // END OF THE CHECKING + if (!error && !Options.ERROR_CHECKING_OVERRIDE.getBooleanValue(false)) { + QItemStack displayItem = getQItemStack("display", config); + List<String> rewards = config.getStringList("rewards"); + List<String> requirements = config.getStringList("options.requires"); + List<String> rewardString = config.getStringList("rewardstring"); + List<String> startString = config.getStringList("startstring"); + boolean repeatable = config.getBoolean("options.repeatable", false); + boolean cooldown = config.getBoolean("options.cooldown.enabled", false); + boolean permissionRequired = config.getBoolean("options.permission-required", false); + int cooldownTime = config.getInt("options.cooldown.time", 10); + int sortOrder = config.getInt("options.sort-order", 1); + String category = config.getString("options.category"); + Map<String, String> placeholders = new HashMap<>(); - QItemStack displayItem = getQItemStack("display", config); - List<String> rewards = config.getStringList("rewards"); - List<String> requirements = config.getStringList("options.requires"); - List<String> rewardString = config.getStringList("rewardstring"); - List<String> startString = config.getStringList("startstring"); - boolean repeatable = config.getBoolean("options.repeatable", false); - boolean cooldown = config.getBoolean("options.cooldown.enabled", false); - boolean permissionRequired = config.getBoolean("options.permission-required", false); - int cooldownTime = config.getInt("options.cooldown.time", 10); - int sortOrder = config.getInt("options.sort-order", 1); - String category = config.getString("options.category"); - Map<String, String> placeholders = new HashMap<>(); + if (category == null) category = ""; - if (category == null) category = ""; + Quest quest; + if (category.equals("")) { + quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, startString, placeholders, sortOrder); + } else { + quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, startString, placeholders, category, sortOrder); + Category c = plugin.getQuestManager().getCategoryById(category); + if (c != null) { + c.registerQuestId(id); + } else { + configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.UNKNOWN_CATEGORY.getDescription(category), "options.category")); + } + } - Quest quest; - if (category.equals("")) { - quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, startString, placeholders, sortOrder); - } else { - quest = new Quest(id, displayItem, rewards, requirements, repeatable, cooldown, cooldownTime, permissionRequired, rewardString, startString, placeholders, category, sortOrder); - Category c = plugin.getQuestManager().getCategoryById(category); - if (c != null) { - c.registerQuestId(id); - } else { - configProblems.add(new ConfigProblem(ConfigProblemType.WARNING, ConfigProblemDescriptions.UNKNOWN_CATEGORY.getDescription(category), "options.category")); - } - } + for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { + String taskRoot = "tasks." + taskId; + String taskType = config.getString(taskRoot + ".type"); - for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { - String taskRoot = "tasks." + taskId; - String taskType = config.getString(taskRoot + ".type"); + Task task = new Task(taskId, taskType); - Task task = new Task(taskId, taskType); + for (String key : config.getConfigurationSection(taskRoot).getKeys(false)) { + task.addConfigValue(key, config.get(taskRoot + "." + key)); + } - for (String key : config.getConfigurationSection(taskRoot).getKeys(false)) { - task.addConfigValue(key, config.get(taskRoot + "." + key)); - } + if (globalTaskConfig.containsKey(taskType)) { + for (Map.Entry<String, Object> entry : globalTaskConfig.get(taskType).entrySet()) { + if (Options.GLOBAL_TASK_CONFIGURATION_OVERRIDE.getBooleanValue() && task.getConfigValue(entry.getKey()) != null) + continue; + task.addConfigValue(entry.getKey(), entry.getValue()); + } + } - if (globalTaskConfig.containsKey(taskType)) { - for (Map.Entry<String, Object> entry : globalTaskConfig.get(taskType).entrySet()) { - if (Options.GLOBAL_TASK_CONFIGURATION_OVERRIDE.getBooleanValue() && task.getConfigValue(entry.getKey()) != null) continue; - task.addConfigValue(entry.getKey(), entry.getValue()); + quest.registerTask(task); } - } - quest.registerTask(task); - } + for (String line : displayItem.getLoreNormal()) { + findInvalidTaskReferences(quest, line, configProblems, "display.lore-normal"); + } + for (String line : displayItem.getLoreStarted()) { + findInvalidTaskReferences(quest, line, configProblems, "display.lore-started"); + } - for (String line : displayItem.getLoreNormal()) { - findInvalidTaskReferences(quest, line, configProblems, "display.lore-normal"); - } - for (String line : displayItem.getLoreStarted()) { - findInvalidTaskReferences(quest, line, configProblems, "display.lore-started"); - } - - if (config.isConfigurationSection("placeholders")) { - for (String p : config.getConfigurationSection("placeholders").getKeys(false)) { - placeholders.put(p, config.getString("placeholders." + p)); - findInvalidTaskReferences(quest, config.getString("placeholders." + p), configProblems, "placeholders." + p); + if (config.isConfigurationSection("placeholders")) { + for (String p : config.getConfigurationSection("placeholders").getKeys(false)) { + placeholders.put(p, config.getString("placeholders." + p)); + findInvalidTaskReferences(quest, config.getString("placeholders." + p), configProblems, "placeholders." + p); + } + } + if (plugin.getConfig().getBoolean("options.show-quest-registrations")) { + plugin.getQuestsLogger().info("Registering quest " + quest.getId() + " with " + quest.getTasks().size() + " tasks."); + } + plugin.getQuestManager().registerQuest(quest); + plugin.getTaskTypeManager().registerQuestTasksWithTaskTypes(quest); + pathToQuest.put(relativeLocation.getPath(), quest); } - } - pathToQuest.put(relativeLocation.getPath(), quest); - if (!configProblems.isEmpty()) { - filesWithProblems.put(relativeLocation.getPath(), configProblems); - } - if (!error && !Options.ERROR_CHECKING_OVERRIDE.getBooleanValue(false)) { - if (plugin.getConfig().getBoolean("options.show-quest-registrations")) { - plugin.getQuestsLogger().info("Registering quest " + quest.getId() + " with " + quest.getTasks().size() + " tasks."); + if (!configProblems.isEmpty()) { + filesWithProblems.put(relativeLocation.getPath(), configProblems); } - plugin.getQuestManager().registerQuest(quest); - plugin.getTaskTypeManager().registerQuestTasksWithTaskTypes(quest); + } catch (Exception e) { + plugin.getQuestsLogger().severe("An exception occurred when attempting to load quest '" + path + "' (will be ignored)"); + e.printStackTrace(); } return FileVisitResult.CONTINUE; } |
