From 36b9258b752b8423b036996cdf8d53492eae0bf9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 17 Jan 2019 14:53:33 -0500 Subject: [PATCH] Delay loading of module config, see #631. Fixes #642 --- src/main/java/me/blackvein/quests/Quests.java | 194 +++++++++++------- 1 file changed, 119 insertions(+), 75 deletions(-) diff --git a/src/main/java/me/blackvein/quests/Quests.java b/src/main/java/me/blackvein/quests/Quests.java index 4b613ff94..8a1c65eac 100644 --- a/src/main/java/me/blackvein/quests/Quests.java +++ b/src/main/java/me/blackvein/quests/Quests.java @@ -432,9 +432,9 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener public void run() { loadQuests(); loadEvents(); - getLogger().log(Level.INFO, "" + quests.size() + " Quest(s) loaded."); - getLogger().log(Level.INFO, "" + events.size() + " Event(s) loaded."); - getLogger().log(Level.INFO, "" + Lang.size() + " Phrase(s) loaded."); + getLogger().log(Level.INFO, "Loaded " + quests.size() + " Quest(s)" + + ", " + events.size() + " Event(s)" + + ", " + Lang.size() + " Phrase(s)"); questers.addAll(getOnlineQuesters()); loadModules(); } @@ -470,6 +470,41 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } else { f.mkdir(); } + boolean failedToLoad; + FileConfiguration config = null; + File file = new File(this.getDataFolder(), "quests.yml"); + try { + config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), "UTF-8")); + } catch (IOException e) { + e.printStackTrace(); + } + if (config != null) { + ConfigurationSection questsSection; + if (config.contains("quests")) { + questsSection = config.getConfigurationSection("quests"); + for (String questKey : questsSection.getKeys(false)) { + try { // main "skip quest" try/catch block + Quest quest = new Quest(); + failedToLoad = false; + if (config.contains("quests." + questKey + ".name")) { + quest.setName(parseString(config.getString("quests." + questKey + ".name"), quest)); + loadCustomSections(quest, config, questKey); + } else { + skipQuestProcess("Quest block \'" + questKey + "\' is missing " + ChatColor.RED + "name:"); + } + if (failedToLoad == true) { + getLogger().log(Level.SEVERE, "Failed to load Quest \"" + questKey + "\". Skipping."); + } + } catch (SkipQuest ex) { + continue; + } catch (StageFailedException ex) { + continue; + } + } + } + } else { + getLogger().severe("Unable to load module data from quests.yml"); + } } public void loadModule(File jar) { @@ -861,7 +896,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener loadQuestPlanner(config, questsSection, quest, questKey); } quest.plugin = this; - processStages(quest, config, questKey); // needsSaving may be modified as a side-effect + processStages(quest, config, questKey); loadQuestRewards(config, quest, questKey); quests.add(quest); if (needsSaving) { @@ -1008,29 +1043,6 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } } } - if (config.contains("quests." + questKey + ".rewards.custom-rewards")) { - ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".rewards.custom-rewards"); - Map> temp = new HashMap>(); - for (String path : sec.getKeys(false)) { - String name = sec.getString(path + ".name"); - Optionalfound = Optional.empty(); - for (CustomReward cr : customRewards) { - if (cr.getName().equalsIgnoreCase(name)) { - found=Optional.of(cr); - break; - } - } - if (!found.isPresent()) { - getLogger().warning("Custom reward \"" + name + "\" for Quest \"" + quest.getName() + "\" could not be found!"); - continue; - } else { - ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data=populateCustoms(sec2,found.get().datamap); - temp.put(name, data); - } - } - rews.setCustomRewards(temp); - } } private void loadQuestRequirements(FileConfiguration config, ConfigurationSection questsSection, Quest quest, String questKey) throws SkipQuest { @@ -1195,29 +1207,6 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener skipQuestProcess("heroes-secondary-class: Requirement for Quest " + quest.getName() + " is not a valid Heroes class!"); } } - if (config.contains("quests." + questKey + ".requirements.custom-requirements")) { - ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".requirements.custom-requirements"); - Map> temp = new HashMap>(); - for (String path : sec.getKeys(false)) { - String name = sec.getString(path + ".name"); - Optionalfound=Optional.empty(); - for (CustomRequirement cr : customRequirements) { - if (cr.getName().equalsIgnoreCase(name)) { - found=Optional.of(cr); - break; - } - } - if (!found.isPresent()) { - getLogger().warning("Custom requirement \"" + name + "\" for Quest \"" + quest.getName() + "\" could not be found!"); - skipQuestProcess((String) null); // null bc we warn, not severe for this one - } else { - ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data=populateCustoms(sec2,found.get().datamap); - temp.put(name, data); - } - } - reqs.setCustomRequirements(temp); - } } private void loadQuestPlanner(FileConfiguration config, ConfigurationSection questsSection, Quest quest, String questKey) throws SkipQuest { @@ -1861,31 +1850,6 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener stageFailed("Stage " + s2 + " of Quest " + quest.getName() + " is missing password-phrases!"); } } - if (config.contains("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives")) { - ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives"); - for (String path : sec.getKeys(false)) { - String name = sec.getString(path + ".name"); - int count = sec.getInt(path + ".count"); - Optional found = Optional.empty(); - for (CustomObjective cr : customObjectives) { - if (cr.getName().equalsIgnoreCase(name)) { - found = Optional.of(cr); - break; - } - } - if (!found.isPresent()) { - getLogger().warning("Custom objective \"" + name + "\" for Stage " + s2 + " of Quest \"" + quest.getName() + "\" could not be found!"); - continue; - } else { - ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data = populateCustoms(sec2, found.get().getData()); // Added in Github PR #554 - - oStage.customObjectives.add(found.get()); - oStage.customObjectiveCounts.add(count); - oStage.customObjectiveData.add(data); - } - } - } if (config.contains("quests." + questKey + ".stages.ordered." + s2 + ".objective-override")) { oStage.objectiveOverride = config.getString("quests." + questKey + ".stages.ordered." + s2 + ".objective-override"); } @@ -2004,6 +1968,86 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } } + private void loadCustomSections(Quest quest, FileConfiguration config, String questKey) throws StageFailedException, SkipQuest { + ConfigurationSection questStages = config.getConfigurationSection("quests." + questKey + ".stages.ordered"); + for (String s2 : questStages.getKeys(false)) { + Stage oStage = new Stage(); + if (config.contains("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives")) { + ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives"); + for (String path : sec.getKeys(false)) { + String name = sec.getString(path + ".name"); + int count = sec.getInt(path + ".count"); + Optional found = Optional.empty(); + for (CustomObjective cr : customObjectives) { + if (cr.getName().equalsIgnoreCase(name)) { + found = Optional.of(cr); + break; + } + } + if (!found.isPresent()) { + getLogger().warning("Custom objective \"" + name + "\" for Stage " + s2 + " of Quest \"" + quest.getName() + "\" could not be found!"); + continue; + } else { + ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); + Map data = populateCustoms(sec2, found.get().getData()); // Added in Github PR #554 + + oStage.customObjectives.add(found.get()); + oStage.customObjectiveCounts.add(count); + oStage.customObjectiveData.add(data); + } + } + } + } + Rewards rews = quest.getRewards(); + if (config.contains("quests." + questKey + ".rewards.custom-rewards")) { + ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".rewards.custom-rewards"); + Map> temp = new HashMap>(); + for (String path : sec.getKeys(false)) { + String name = sec.getString(path + ".name"); + Optionalfound = Optional.empty(); + for (CustomReward cr : customRewards) { + if (cr.getName().equalsIgnoreCase(name)) { + found=Optional.of(cr); + break; + } + } + if (!found.isPresent()) { + getLogger().warning("Custom reward \"" + name + "\" for Quest \"" + quest.getName() + "\" could not be found!"); + continue; + } else { + ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); + Map data=populateCustoms(sec2,found.get().datamap); + temp.put(name, data); + } + } + rews.setCustomRewards(temp); + } + Requirements reqs = quest.getRequirements(); + if (config.contains("quests." + questKey + ".requirements.custom-requirements")) { + ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".requirements.custom-requirements"); + Map> temp = new HashMap>(); + for (String path : sec.getKeys(false)) { + String name = sec.getString(path + ".name"); + Optionalfound=Optional.empty(); + for (CustomRequirement cr : customRequirements) { + if (cr.getName().equalsIgnoreCase(name)) { + found=Optional.of(cr); + break; + } + } + if (!found.isPresent()) { + getLogger().warning("Custom requirement \"" + name + "\" for Quest \"" + quest.getName() + "\" could not be found!"); + skipQuestProcess((String) null); // null bc we warn, not severe for this one + } else { + ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); + Map data=populateCustoms(sec2,found.get().datamap); + temp.put(name, data); + } + } + reqs.setCustomRequirements(temp); + } + } + /** * Add possibilty to use fallbacks for customs. * Avoid null objects in datamap by initialize the entry value with empty string if no fallback present.