aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/main/java/com
diff options
context:
space:
mode:
authorLMBishop <13875753+LMBishop@users.noreply.github.com>2022-05-01 18:48:21 +0100
committerLMBishop <13875753+LMBishop@users.noreply.github.com>2022-05-01 18:48:21 +0100
commit8068d1efc179e60e5b01a1733c392f5b3de21549 (patch)
tree21803b247438d81e08305e34b1ec177d43cdc1bf /common/src/main/java/com
parent649bb972c9bcd587a5d1b2d495922db5901d05db (diff)
Add time-limit option and quest expiry functionality (closes #379)
Diffstat (limited to 'common/src/main/java/com')
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/player/QPlayer.java12
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/player/questprogressfile/QuestProgressFile.java19
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/quest/Quest.java37
-rw-r--r--common/src/main/java/com/leonardobishop/quests/common/questcontroller/QuestController.java2
4 files changed, 69 insertions, 1 deletions
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
@@ -113,6 +113,18 @@ public class QPlayer {
}
/**
+ * 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.
*
* Warning: will fail if the player is not online.
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
@@ -146,6 +146,25 @@ public class QuestProgressFile {
}
/**
+ * 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.
*
* @param quest the quest to test for
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<Quest> {
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,13 +178,32 @@ public class Quest implements Comparable<Quest> {
* 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.
*
* @return the category id, or null
@@ -260,6 +281,8 @@ public class Quest implements Comparable<Quest> {
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<Quest> {
return this;
}
+ public Builder withTimeLimit(int timeLimit) {
+ this.timeLimit = timeLimit;
+ return this;
+ }
+
public Builder withPlaceholders(Map<String, String> placeholders) {
this.placeholders = placeholders;
return this;
@@ -322,6 +350,11 @@ public class Quest implements Comparable<Quest> {
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> {
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);
}