From a51b0c6ac010b7395b75e31ff9833b43c468acde Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Mon, 18 Apr 2022 15:18:17 +0100 Subject: Fix reset and fullreset commands (closes #351) --- .../player/questprogressfile/QuestProgress.java | 10 ++++++ .../questprogressfile/QuestProgressFile.java | 37 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) (limited to 'common') diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgress.java b/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgress.java index d0e0627d..06d8a91d 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgress.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgress.java @@ -130,6 +130,16 @@ public class QuestProgress { } } + public boolean hasNonDefaultValues() { + if (this.started || this.completed || this.completedBefore || this.completionDate != 0) return true; + else { + for (TaskProgress progress : this.taskProgress.values()) { + if (progress.getProgress() != null || progress.isCompleted()) return true; + } + return false; + } + } + public void queueForCompletionTest() { plugin.getQuestCompleter().queueSingular(this); } diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgressFile.java b/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgressFile.java index 4a740ee3..40d80509 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgressFile.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgressFile.java @@ -168,7 +168,7 @@ public class QuestProgressFile { } /** - * Get the {@link UUID} of the player this QuestProgressFile represents + * Get the {@link UUID} of the player this QuestProgressFile represents. * * @return UUID */ @@ -177,7 +177,7 @@ public class QuestProgressFile { } /** - * Get the {@link QuestProgress} for a specified {@link Quest}, and generates a new one if it does not exist + * Get the {@link QuestProgress} for a specified {@link Quest}, and generates a new one if it does not exist. * * @param quest the quest to get progress for * @return {@link QuestProgress} or null if the quest does not exist @@ -192,14 +192,23 @@ public class QuestProgressFile { /** * Generate a new blank {@link QuestProgress} for a specified {@code quest}. - * Has no effect if there is already an existing {@link QuestProgress} for {@code quest}. * * @param quest the quest to generate progress for */ public void generateBlankQuestProgress(Quest quest) { - QuestProgress questProgress = new QuestProgress(plugin, quest.getId(), false, false, 0, playerUUID, false, false); + generateBlankQuestProgress(quest, false); + } + + /** + * Generate a new blank {@link QuestProgress} for a specified {@code quest}. + * + * @param quest the quest to generate progress for + * @param modified the modified state of the quest + */ + public void generateBlankQuestProgress(Quest quest, boolean modified) { + QuestProgress questProgress = new QuestProgress(plugin, quest.getId(), false, false, 0, playerUUID, false, modified); for (Task task : quest.getTasks()) { - TaskProgress taskProgress = new TaskProgress(questProgress, task.getId(), null, playerUUID, false, false); + TaskProgress taskProgress = new TaskProgress(questProgress, task.getId(), null, playerUUID, false, modified); questProgress.addTaskProgress(taskProgress); } @@ -210,6 +219,24 @@ public class QuestProgressFile { questProgress.clear(); } + /** + * Reset quests to their default state. More specifically, this will reset all + * quest progress with non-default parameters back to default and only + * set the modified flag in that case. + */ + public void reset() { + for (QuestProgress questProgress : questProgress.values()) { + if (!questProgress.hasNonDefaultValues()) { + continue; + } + Quest quest = plugin.getQuestManager().getQuestById(questProgress.getQuestId()); + if (quest == null) { + continue; + } + generateBlankQuestProgress(quest, true); + } + } + /** * Removes any references to quests or tasks which are no longer defined in the config. */ -- cgit v1.2.3-70-g09d2