From d74e5b013088fa780125cf03dfc00097430bd980 Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Sat, 28 Nov 2020 21:05:30 -0500 Subject: [PATCH] Avoid NPE from non-existant quests in player data, fixes #1475 --- .../file/SeparatedYamlStorage.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/storage/implementation/file/SeparatedYamlStorage.java b/main/src/main/java/me/blackvein/quests/storage/implementation/file/SeparatedYamlStorage.java index 7df31cb7b..6feb121ad 100644 --- a/main/src/main/java/me/blackvein/quests/storage/implementation/file/SeparatedYamlStorage.java +++ b/main/src/main/java/me/blackvein/quests/storage/implementation/file/SeparatedYamlStorage.java @@ -97,51 +97,53 @@ public class SeparatedYamlStorage implements StorageImplementation { if (data.contains("completedRedoableQuests")) { final List questNames = data.getStringList("completedRedoableQuests"); final List questTimes = data.getLongList("completedQuestTimes"); + final ConcurrentHashMap completedTimes = quester.getCompletedTimes(); for (int i = 0; i < questNames.size(); i++) { - final ConcurrentHashMap completedTimes = quester.getCompletedTimes(); - completedTimes.put(plugin.getQuest(questNames.get(i)), questTimes.get(i)); - quester.setCompletedTimes(completedTimes); + if (plugin.getQuest(questNames.get(i)) != null) { + completedTimes.put(plugin.getQuest(questNames.get(i)), questTimes.get(i)); + } } + quester.setCompletedTimes(completedTimes); } if (data.contains("amountsCompletedQuests")) { final List questNames = data.getStringList("amountsCompletedQuests"); final List questAmts = data.getIntegerList("amountsCompleted"); + final ConcurrentHashMap amountsCompleted = quester.getAmountsCompleted(); for (int i = 0; i < questNames.size(); i++) { - final ConcurrentHashMap amountsCompleted = quester.getAmountsCompleted(); - amountsCompleted.put(plugin.getQuest(questNames.get(i)), questAmts.get(i)); - quester.setAmountsCompleted(amountsCompleted); + if (plugin.getQuest(questNames.get(i)) != null) { + amountsCompleted.put(plugin.getQuest(questNames.get(i)), questAmts.get(i)); + } } + quester.setAmountsCompleted(amountsCompleted); } quester.setLastKnownName(data.getString("lastKnownName")); quester.setQuestPoints(data.getInt("quest-points")); + final LinkedList completedQuests = quester.getCompletedQuests(); if (data.isList("completed-Quests")) { for (final String s : data.getStringList("completed-Quests")) { for (final Quest q : plugin.getQuests()) { if (q.getName().equalsIgnoreCase(s)) { if (!quester.getCompletedQuests().contains(q)) { - final LinkedList completedQuests = quester.getCompletedQuests(); completedQuests.add(q); - quester.setCompletedQuests(completedQuests); } break; } } } - } else { - quester.setCompletedQuests(new LinkedList()); } + quester.setCompletedQuests(completedQuests); if (data.isString("currentQuests") == false) { final List questNames = data.getStringList("currentQuests"); final List questStages = data.getIntegerList("currentStages"); // These appear to differ sometimes? That seems bad. final int maxSize = Math.min(questNames.size(), questStages.size()); + final ConcurrentHashMap currentQuests = quester.getCurrentQuests(); for (int i = 0; i < maxSize; i++) { if (plugin.getQuest(questNames.get(i)) != null) { - final ConcurrentHashMap currentQuests = quester.getCurrentQuests(); currentQuests.put(plugin.getQuest(questNames.get(i)), questStages.get(i)); - quester.setCurrentQuests(currentQuests); } } + quester.setCurrentQuests(currentQuests); final ConfigurationSection dataSec = data.getConfigurationSection("questData"); if (dataSec == null || dataSec.getKeys(false).isEmpty()) { return null; @@ -506,7 +508,6 @@ public class SeparatedYamlStorage implements StorageImplementation { for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { - System.out.println("File " + listOfFiles[i].getName()); final String name = listOfFiles[i].getName().substring(0, listOfFiles[i].getName().lastIndexOf(".")); UUID id = null; try {