Avoid NPE from non-existant quests in player data, fixes #1475

This commit is contained in:
PikaMug 2020-11-28 21:05:30 -05:00
parent 21d7768374
commit d74e5b0130

View File

@ -97,51 +97,53 @@ public class SeparatedYamlStorage implements StorageImplementation {
if (data.contains("completedRedoableQuests")) { if (data.contains("completedRedoableQuests")) {
final List<String> questNames = data.getStringList("completedRedoableQuests"); final List<String> questNames = data.getStringList("completedRedoableQuests");
final List<Long> questTimes = data.getLongList("completedQuestTimes"); final List<Long> questTimes = data.getLongList("completedQuestTimes");
final ConcurrentHashMap<Quest, Long> completedTimes = quester.getCompletedTimes();
for (int i = 0; i < questNames.size(); i++) { for (int i = 0; i < questNames.size(); i++) {
final ConcurrentHashMap<Quest, Long> completedTimes = quester.getCompletedTimes(); if (plugin.getQuest(questNames.get(i)) != null) {
completedTimes.put(plugin.getQuest(questNames.get(i)), questTimes.get(i)); completedTimes.put(plugin.getQuest(questNames.get(i)), questTimes.get(i));
quester.setCompletedTimes(completedTimes); }
} }
quester.setCompletedTimes(completedTimes);
} }
if (data.contains("amountsCompletedQuests")) { if (data.contains("amountsCompletedQuests")) {
final List<String> questNames = data.getStringList("amountsCompletedQuests"); final List<String> questNames = data.getStringList("amountsCompletedQuests");
final List<Integer> questAmts = data.getIntegerList("amountsCompleted"); final List<Integer> questAmts = data.getIntegerList("amountsCompleted");
final ConcurrentHashMap<Quest, Integer> amountsCompleted = quester.getAmountsCompleted();
for (int i = 0; i < questNames.size(); i++) { for (int i = 0; i < questNames.size(); i++) {
final ConcurrentHashMap<Quest, Integer> amountsCompleted = quester.getAmountsCompleted(); if (plugin.getQuest(questNames.get(i)) != null) {
amountsCompleted.put(plugin.getQuest(questNames.get(i)), questAmts.get(i)); amountsCompleted.put(plugin.getQuest(questNames.get(i)), questAmts.get(i));
quester.setAmountsCompleted(amountsCompleted); }
} }
quester.setAmountsCompleted(amountsCompleted);
} }
quester.setLastKnownName(data.getString("lastKnownName")); quester.setLastKnownName(data.getString("lastKnownName"));
quester.setQuestPoints(data.getInt("quest-points")); quester.setQuestPoints(data.getInt("quest-points"));
final LinkedList<Quest> completedQuests = quester.getCompletedQuests();
if (data.isList("completed-Quests")) { if (data.isList("completed-Quests")) {
for (final String s : data.getStringList("completed-Quests")) { for (final String s : data.getStringList("completed-Quests")) {
for (final Quest q : plugin.getQuests()) { for (final Quest q : plugin.getQuests()) {
if (q.getName().equalsIgnoreCase(s)) { if (q.getName().equalsIgnoreCase(s)) {
if (!quester.getCompletedQuests().contains(q)) { if (!quester.getCompletedQuests().contains(q)) {
final LinkedList<Quest> completedQuests = quester.getCompletedQuests();
completedQuests.add(q); completedQuests.add(q);
quester.setCompletedQuests(completedQuests);
} }
break; break;
} }
} }
} }
} else {
quester.setCompletedQuests(new LinkedList<Quest>());
} }
quester.setCompletedQuests(completedQuests);
if (data.isString("currentQuests") == false) { if (data.isString("currentQuests") == false) {
final List<String> questNames = data.getStringList("currentQuests"); final List<String> questNames = data.getStringList("currentQuests");
final List<Integer> questStages = data.getIntegerList("currentStages"); final List<Integer> questStages = data.getIntegerList("currentStages");
// These appear to differ sometimes? That seems bad. // These appear to differ sometimes? That seems bad.
final int maxSize = Math.min(questNames.size(), questStages.size()); final int maxSize = Math.min(questNames.size(), questStages.size());
final ConcurrentHashMap<Quest, Integer> currentQuests = quester.getCurrentQuests();
for (int i = 0; i < maxSize; i++) { for (int i = 0; i < maxSize; i++) {
if (plugin.getQuest(questNames.get(i)) != null) { if (plugin.getQuest(questNames.get(i)) != null) {
final ConcurrentHashMap<Quest, Integer> currentQuests = quester.getCurrentQuests();
currentQuests.put(plugin.getQuest(questNames.get(i)), questStages.get(i)); currentQuests.put(plugin.getQuest(questNames.get(i)), questStages.get(i));
quester.setCurrentQuests(currentQuests);
} }
} }
quester.setCurrentQuests(currentQuests);
final ConfigurationSection dataSec = data.getConfigurationSection("questData"); final ConfigurationSection dataSec = data.getConfigurationSection("questData");
if (dataSec == null || dataSec.getKeys(false).isEmpty()) { if (dataSec == null || dataSec.getKeys(false).isEmpty()) {
return null; return null;
@ -506,7 +508,6 @@ public class SeparatedYamlStorage implements StorageImplementation {
for (int i = 0; i < listOfFiles.length; i++) { for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) { if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
final String name = listOfFiles[i].getName().substring(0, listOfFiles[i].getName().lastIndexOf(".")); final String name = listOfFiles[i].getName().substring(0, listOfFiles[i].getName().lastIndexOf("."));
UUID id = null; UUID id = null;
try { try {