From 8068d1efc179e60e5b01a1733c392f5b3de21549 Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Sun, 1 May 2022 18:48:21 +0100 Subject: Add time-limit option and quest expiry functionality (closes #379) --- .../quests/common/player/QPlayer.java | 12 +++++++ .../questprogressfile/QuestProgressFile.java | 19 +++++++++++ .../leonardobishop/quests/common/quest/Quest.java | 37 +++++++++++++++++++++- .../common/questcontroller/QuestController.java | 2 ++ 4 files changed, 69 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java index 3e775dca..c7c71fbb 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java +++ b/common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java @@ -112,6 +112,18 @@ public class QPlayer { return questController.cancelQuestForPlayer(this, quest); } + /** + * Attempt to expire a quest for the player. This will also play all effects (such as titles, messages etc.) + * + * @param quest the quest to start + * @return true if the quest was expired, false otherwise + */ + public boolean expireQuest(@NotNull Quest quest) { + Objects.requireNonNull(quest, "quest cannot be null"); + + return questController.expireQuestForPlayer(this, quest); + } + /** * Check if the player can start a quest. * 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 2953a340..13756619 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 @@ -145,6 +145,25 @@ public class QuestProgressFile { return 0; } + /** + * Gets the time remaining before a quest will have expired. + * + * @param quest the quest to test for + * @return 0 if no time remaining, -1 if the time limit is disabled or the quest is not started, + * otherwise the time left in milliseconds + */ + public long getTimeRemainingFor(Quest quest) { + QuestProgress questProgress = getQuestProgress(quest); + if (quest.isTimeLimitEnabled() && questProgress.isStarted()) { + return Math.max( + questProgress.getStartedDate() + + TimeUnit.MILLISECONDS.convert(quest.getTimeLimit(), TimeUnit.MINUTES) + - System.currentTimeMillis() + , 0); + } + return -1; + } + /** * Tests whether or not the player meets the requirements to start a specific quest. * diff --git a/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java b/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java index 999507b2..f12c9a5f 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java +++ b/common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java @@ -18,6 +18,8 @@ public class Quest implements Comparable { private boolean repeatEnabled; private boolean cooldownEnabled; private int cooldown; + private boolean timeLimitEnabled; + private int timeLimit; private int sortOrder; private boolean permissionRequired; private boolean autoStartEnabled; @@ -176,12 +178,31 @@ public class Quest implements Comparable { * Get the cooldown for this quest between completing and restarting the quest. * Whether or not this cooldown is in use depends on {@link Quest#isCooldownEnabled()}. * - * @return the cooldown, in seconds + * @return the cooldown, in minutes */ public int getCooldown() { return cooldown; } + /** + * Get whether this quest has a time limit. + * + * @return boolean + */ + public boolean isTimeLimitEnabled() { + return timeLimitEnabled; + } + + /** + * Get the time limit for this quest. + * Whether or not this time limit is in use depends on {@link Quest#isTimeLimitEnabled()}. + * + * @return the time limit, in minutes + */ + public int getTimeLimit() { + return timeLimit; + } + /** * Get the category id this quest is in. * @@ -260,6 +281,8 @@ public class Quest implements Comparable { private boolean repeatEnabled = false; private boolean cooldownEnabled = false; private int cooldown = 0; + private boolean timeLimitEnabled = false; + private int timeLimit = 0; private int sortOrder = 1; private boolean permissionRequired = false; private boolean autoStartEnabled = false; @@ -307,6 +330,11 @@ public class Quest implements Comparable { return this; } + public Builder withTimeLimit(int timeLimit) { + this.timeLimit = timeLimit; + return this; + } + public Builder withPlaceholders(Map placeholders) { this.placeholders = placeholders; return this; @@ -322,6 +350,11 @@ public class Quest implements Comparable { return this; } + public Builder withTimeLimitEnabled(boolean timeLimitEnabled) { + this.timeLimitEnabled = timeLimitEnabled; + return this; + } + public Builder withPermissionRequired(boolean permissionRequired) { this.permissionRequired = permissionRequired; return this; @@ -358,6 +391,8 @@ public class Quest implements Comparable { quest.repeatEnabled = this.repeatEnabled; quest.cooldownEnabled = this.cooldownEnabled; quest.cooldown = this.cooldown; + quest.timeLimitEnabled = this.timeLimitEnabled; + quest.timeLimit = this.timeLimit; quest.sortOrder = this.sortOrder; quest.permissionRequired = this.permissionRequired; quest.autoStartEnabled = this.autoStartEnabled; diff --git a/common/src/main/java/com/leonardobishop/quests/common/questcontroller/QuestController.java b/common/src/main/java/com/leonardobishop/quests/common/questcontroller/QuestController.java index 14aa03fd..d1e4812c 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/questcontroller/QuestController.java +++ b/common/src/main/java/com/leonardobishop/quests/common/questcontroller/QuestController.java @@ -22,6 +22,8 @@ public interface QuestController { boolean cancelQuestForPlayer(QPlayer qPlayer, Quest quest); + boolean expireQuestForPlayer(QPlayer qPlayer, Quest quest); + void trackQuestForPlayer(QPlayer qPlayer, Quest quest); } -- cgit v1.2.3-70-g09d2