From 67a72d31c68cee4410983daf960e4f8ab82a9b17 Mon Sep 17 00:00:00 2001
From: PikaMug <2267126+PikaMug@users.noreply.github.com>
Date: Tue, 26 Jul 2022 07:32:26 -0400
Subject: [PATCH] Separate method to check punctuality, see #1991
---
.../java/me/blackvein/quests/Quester.java | 179 ++++++++++--------
1 file changed, 99 insertions(+), 80 deletions(-)
diff --git a/core/src/main/java/me/blackvein/quests/Quester.java b/core/src/main/java/me/blackvein/quests/Quester.java
index c090a4883..31eaaf321 100644
--- a/core/src/main/java/me/blackvein/quests/Quester.java
+++ b/core/src/main/java/me/blackvein/quests/Quester.java
@@ -643,6 +643,103 @@ public class Quester implements IQuester {
}
return true;
}
+
+ /**
+ * Check if Quester is too early or late for a planned quest
+ *
+ * For player cooldown, use {@link #canAcceptOffer(IQuest, boolean)} instead
+ *
+ * @param quest The quest to check
+ * @param giveReason Whether to inform Quester of unpunctuality
+ * @return true if on time
+ */
+ public boolean isOnTime(final IQuest quest, final boolean giveReason) {
+ final Planner pln = quest.getPlanner();
+ final long currentTime = System.currentTimeMillis();
+ final long start = pln.getStartInMillis(); // Start time in milliseconds since UTC epoch
+ final long end = pln.getEndInMillis(); // End time in milliseconds since UTC epoch
+ final long duration = end - start; // How long the quest can be active for
+ final long repeat = pln.getRepeat(); // Length to wait in-between start times
+ if (start != -1) {
+ if (currentTime < start) {
+ if (giveReason) {
+ String early = Lang.get("plnTooEarly");
+ early = early.replace("", ChatColor.AQUA + quest.getName() + ChatColor.YELLOW);
+ early = early.replace("", ChatColor.RED
+ + MiscUtil.getTime(start - currentTime) + ChatColor.YELLOW);
+ sendMessage(ChatColor.YELLOW + early);
+ }
+ return false;
+ }
+ }
+ if (end != -1 && repeat == -1) {
+ if (currentTime > end) {
+ if (giveReason) {
+ String late = Lang.get("plnTooLate");
+ late = late.replace("", ChatColor.AQUA + quest.getName() + ChatColor.RED);
+ late = late.replace("", ChatColor.DARK_PURPLE
+ + MiscUtil.getTime(currentTime - end) + ChatColor.RED);
+ sendMessage(ChatColor.RED + late);
+ }
+ return false;
+ }
+ }
+ if (repeat != -1 && start != -1 && end != -1) {
+ // Ensure that we're past the initial duration
+ if (currentTime > end) {
+ final int maxSize = 2;
+ final LinkedHashMap mostRecent = new LinkedHashMap() {
+ private static final long serialVersionUID = 3046838061019897713L;
+
+ @Override
+ protected boolean removeEldestEntry(final Map.Entry eldest) {
+ return size() > maxSize;
+ }
+ };
+
+ // Get last completed time
+ long completedTime = 0L;
+ if (getCompletedTimes().containsKey(quest)) {
+ completedTime = getCompletedTimes().get(quest);
+ }
+ long completedEnd = 0L;
+
+ // Store last completed, upcoming, and most recent periods of activity
+ long nextStart = start;
+ long nextEnd = end;
+ while (currentTime >= nextStart) {
+ if (nextStart < completedTime && completedTime < nextEnd) {
+ completedEnd = nextEnd;
+ }
+ nextStart += repeat;
+ nextEnd = nextStart + duration;
+ mostRecent.put(nextStart, nextEnd);
+ }
+
+ // Check whether the quest is currently active
+ boolean active = false;
+ for (final Entry startEnd : mostRecent.entrySet()) {
+ if (startEnd.getKey() <= currentTime && currentTime < startEnd.getValue()) {
+ active = true;
+ break;
+ }
+ }
+
+ // If quest is not active, or new period of activity should override player cooldown
+ if (!active || (quest.getPlanner().getOverride() && completedEnd > 0L && currentTime < completedEnd)) {
+ if (giveReason) {
+ final String early = Lang.get("plnTooEarly")
+ .replace("", ChatColor.AQUA + quest.getName() + ChatColor.YELLOW)
+ .replace("", ChatColor.DARK_PURPLE
+ + MiscUtil.getTime((completedEnd) - currentTime) + ChatColor.YELLOW);
+ sendMessage(ChatColor.YELLOW + early);
+ }
+ return false;
+ }
+ }
+ }
+ return true;
+ }
/**
* Start a quest for this Quester
@@ -661,86 +758,8 @@ public class Quester implements IQuester {
}
final OfflinePlayer offlinePlayer = getOfflinePlayer();
if (offlinePlayer.isOnline()) {
- final Planner pln = quest.getPlanner();
- final long currentTime = System.currentTimeMillis();
- final long start = pln.getStartInMillis(); // Start time in milliseconds since UTC epoch
- final long end = pln.getEndInMillis(); // End time in milliseconds since UTC epoch
- final long duration = end - start; // How long the quest can be active for
- final long repeat = pln.getRepeat(); // Length to wait in-between start times
- if (start != -1) {
- if (currentTime < start) {
- String early = Lang.get("plnTooEarly");
- early = early.replace("", ChatColor.AQUA + quest.getName() + ChatColor.YELLOW);
- early = early.replace("", ChatColor.RED
- + MiscUtil.getTime(start - currentTime) + ChatColor.YELLOW);
- sendMessage(ChatColor.YELLOW + early);
- return;
- }
- }
- if (end != -1 && repeat == -1) {
- if (currentTime > end) {
- String late = Lang.get("plnTooLate");
- late = late.replace("", ChatColor.AQUA + quest.getName() + ChatColor.RED);
- late = late.replace("", ChatColor.DARK_PURPLE
- + MiscUtil.getTime(currentTime - end) + ChatColor.RED);
- sendMessage(ChatColor.RED + late);
- return;
- }
- }
- if (repeat != -1 && start != -1 && end != -1) {
- // Ensure that we're past the initial duration
- if (currentTime > end) {
- final int maxSize = 2;
- final LinkedHashMap mostRecent = new LinkedHashMap() {
- private static final long serialVersionUID = 3046838061019897713L;
-
- @Override
- protected boolean removeEldestEntry(final Map.Entry eldest) {
- return size() > maxSize;
- }
- };
-
- // Get last completed time
- long completedTime = 0L;
- if (getCompletedTimes().containsKey(quest)) {
- completedTime = getCompletedTimes().get(quest);
- }
- long completedEnd = 0L;
-
- // Store last completed, upcoming, and most recent periods of activity
- long nextStart = start;
- long nextEnd = end;
- while (currentTime >= nextStart) {
- if (nextStart < completedTime && completedTime < nextEnd) {
- completedEnd = nextEnd;
- }
- nextStart += repeat;
- nextEnd = nextStart + duration;
- mostRecent.put(nextStart, nextEnd);
- }
-
- // Check whether the quest is currently active
- boolean active = false;
- for (final Entry startEnd : mostRecent.entrySet()) {
- if (startEnd.getKey() <= currentTime && currentTime < startEnd.getValue()) {
- active = true;
- break;
- }
- }
-
- // If quest is not active, or new period of activity should override player cooldown, inform user
- if (!active || (quest.getPlanner().getOverride() && completedEnd > 0L
- && currentTime < (completedEnd /*+ repeat*/))) {
- if (getPlayer().isOnline()) {
- final String early = Lang.get("plnTooEarly")
- .replace("", ChatColor.AQUA + quest.getName() + ChatColor.YELLOW)
- .replace("", ChatColor.DARK_PURPLE
- + MiscUtil.getTime((completedEnd /*+ repeat*/) - currentTime) + ChatColor.YELLOW);
- sendMessage(ChatColor.YELLOW + early);
- }
- return;
- }
- }
+ if (!isOnTime(quest, true)) {
+ return;
}
}
if (quest.testRequirements(offlinePlayer) || ignoreRequirements) {