From fd013da51c59dfd458f23d63c08566c54a41f7c7 Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Sun, 26 Apr 2020 00:52:43 +0100 Subject: Add more error messages when loading config --- .../leonardobishop/quests/QuestsConfigLoader.java | 56 +++++++++++++++++----- .../quests/quests/tasktypes/TaskTypeManager.java | 19 ++++++-- .../quests/tasktypes/types/PlaytimeTaskType.java | 11 +++++ 3 files changed, 70 insertions(+), 16 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java index 093f30c6..460b75d2 100644 --- a/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java +++ b/src/main/java/com/leonardobishop/quests/QuestsConfigLoader.java @@ -5,6 +5,7 @@ import com.leonardobishop.quests.obj.misc.QItemStack; import com.leonardobishop.quests.quests.Category; import com.leonardobishop.quests.quests.Quest; import com.leonardobishop.quests.quests.Task; +import com.leonardobishop.quests.quests.tasktypes.ConfigValue; import com.leonardobishop.quests.quests.tasktypes.TaskType; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; @@ -44,7 +45,7 @@ public class QuestsConfigLoader { YamlConfiguration config = new YamlConfiguration(); config.load(new File(plugin.getDataFolder() + File.separator + "config.yml")); } catch (Exception ex) { - brokenFiles.put("
config.yml", ConfigLoadError.MALFORMED_YAML); + brokenFiles.put("
config.yml", new ConfigLoadError(ConfigLoadErrorType.MALFORMED_YAML)); plugin.setBrokenConfig(true); return; } @@ -73,38 +74,54 @@ public class QuestsConfigLoader { try { config.load(questFile); } catch (Exception ex) { - brokenFiles.put(relativeLocation.getPath(), ConfigLoadError.MALFORMED_YAML); + brokenFiles.put(relativeLocation.getPath(), new ConfigLoadError(ConfigLoadErrorType.MALFORMED_YAML)); return FileVisitResult.CONTINUE; } String id = questFile.getName().replace(".yml", ""); if (!StringUtils.isAlphanumeric(id)) { - brokenFiles.put(relativeLocation.getPath(), ConfigLoadError.INVALID_QUEST_ID); + brokenFiles.put(relativeLocation.getPath(), new ConfigLoadError(ConfigLoadErrorType.INVALID_QUEST_ID)); return FileVisitResult.CONTINUE; } // CHECK EVERYTHING WRONG WITH THE QUEST FILE BEFORE ACTUALLY LOADING THE QUEST - boolean isTheQuestFileOkay = true; + List questErrors = new ArrayList<>(); + List taskErrors = new ArrayList<>(); if (!config.isConfigurationSection("tasks")) { - isTheQuestFileOkay = false; + questErrors.add("'tasks' section not defined"); } else { //continue for (String taskId : config.getConfigurationSection("tasks").getKeys(false)) { String taskRoot = "tasks." + taskId; String taskType = config.getString(taskRoot + ".type"); if (!config.isConfigurationSection(taskRoot)) { - isTheQuestFileOkay = false; - break; //do not loop if section do not exist, just break directly + questErrors.add("task '" + taskId + "' cannot be read (has no children)"); + continue; + } + + // check the tasks + TaskType t = plugin.getTaskTypeManager().getTaskType(taskType); + if (t != null) { + List missingFields = new ArrayList<>(); + for (ConfigValue cv : t.getCreatorConfigValues()) { + if (cv.isRequired() && config.get(taskRoot + "." + cv.getKey()) == null) + missingFields.add(cv.getKey()); + } + if (!missingFields.isEmpty()) + taskErrors.add("task '" + taskId + "': '" + t.getType() + "' missing required field(s) '" + String.join(", ", missingFields) + "'"); } } } - if (!isTheQuestFileOkay) { //if the file quest is not okay, do not load the quest - brokenFiles.put(relativeLocation.getPath(), ConfigLoadError.MALFORMED_QUEST); + if (!questErrors.isEmpty()) { //if the file quest is not okay, do not load the quest + brokenFiles.put(relativeLocation.getPath(), new ConfigLoadError(ConfigLoadErrorType.MALFORMED_QUEST, String.join("; ", questErrors))); return FileVisitResult.CONTINUE; //next quest please! + } else if (!taskErrors.isEmpty()) { // likewise with tasks + brokenFiles.put(relativeLocation.getPath(), new ConfigLoadError(ConfigLoadErrorType.MALFORMED_TASK, String.join("; ", taskErrors))); + return FileVisitResult.CONTINUE; } // END OF THE CHECKING @@ -204,15 +221,16 @@ public class QuestsConfigLoader { return new QItemStack(name, loreNormal, loreStarted, is); } - public enum ConfigLoadError { + public enum ConfigLoadErrorType { MALFORMED_YAML("Malformed YAML"), INVALID_QUEST_ID("Invalid quest ID (must be alphanumeric)"), - MALFORMED_QUEST("Quest file is not configured properly"); + MALFORMED_QUEST("Quest file is not configured properly: %s"), + MALFORMED_TASK("Tasks are not configured properly: %s"); private String message; - ConfigLoadError(String message) { + ConfigLoadErrorType(String message) { this.message = message; } @@ -220,4 +238,18 @@ public class QuestsConfigLoader { return message; } } + public class ConfigLoadError { + + private ConfigLoadErrorType type; + private String[] extraInfo; + + public ConfigLoadError(ConfigLoadErrorType type, String... extraInfo) { + this.type = type; + this.extraInfo = extraInfo; + } + + public String getMessage() { + return String.format(type.getMessage(), (Object[]) extraInfo); + } + } } diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/TaskTypeManager.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/TaskTypeManager.java index 27a4346f..7b01d463 100644 --- a/src/main/java/com/leonardobishop/quests/quests/tasktypes/TaskTypeManager.java +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/TaskTypeManager.java @@ -43,12 +43,23 @@ public class TaskTypeManager { } public void registerQuestTasksWithTaskTypes(Quest quest) { + if (allowRegistrations) { + throw new IllegalStateException("Still accepting new task types (type registrations must be closed before registering quests)"); + } for (Task task : quest.getTasks()) { - for (TaskType taskType : taskTypes) { - if (taskType.getType().equalsIgnoreCase(task.getType())) { - taskType.registerQuest(quest); - } + TaskType t; + if ((t = getTaskType(task.getType())) != null) { + t.registerQuest(quest); + } + } + } + + public TaskType getTaskType(String string) { + for (TaskType taskType : taskTypes) { + if (taskType.getType().equalsIgnoreCase(string)) { + return taskType; } } + return null; } } diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/PlaytimeTaskType.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/PlaytimeTaskType.java index fc41eff4..d7ee0505 100644 --- a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/PlaytimeTaskType.java +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/PlaytimeTaskType.java @@ -8,18 +8,24 @@ import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; import com.leonardobishop.quests.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.quests.Quest; import com.leonardobishop.quests.quests.Task; +import com.leonardobishop.quests.quests.tasktypes.ConfigValue; import com.leonardobishop.quests.quests.tasktypes.TaskType; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import java.util.ArrayList; +import java.util.List; + public final class PlaytimeTaskType extends TaskType { private BukkitTask poll; + private List creatorConfigValues = new ArrayList<>(); public PlaytimeTaskType() { super("playtime", "Reinatix", "Track the amount of playing time a user has been on"); + this.creatorConfigValues.add(new ConfigValue("minutes", true, "Time in minutes.")); } @Override @@ -61,4 +67,9 @@ public final class PlaytimeTaskType extends TaskType { this.poll.cancel(); } } + + @Override + public List getCreatorConfigValues() { + return creatorConfigValues; + } } -- cgit v1.2.3-70-g09d2