Ignore cooldown after repeat cycle, part 2. Fixes #1244

This commit is contained in:
PikaMug 2020-07-28 16:48:51 -04:00
parent ab87f6bb00
commit ffd69394a2
3 changed files with 40 additions and 22 deletions

View File

@ -464,7 +464,7 @@ public class Quester {
String early = Lang.get("plnTooEarly");
early = early.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.YELLOW);
early = early.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(start - System.currentTimeMillis()) + ChatColor.YELLOW);
+ MiscUtil.getTime(start - currentTime) + ChatColor.YELLOW);
p.sendMessage(ChatColor.YELLOW + early);
return;
}
@ -474,7 +474,7 @@ public class Quester {
String late = Lang.get("plnTooLate");
late = late.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.RED);
late = late.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(System.currentTimeMillis() - end) + ChatColor.RED);
+ MiscUtil.getTime(currentTime - end) + ChatColor.RED);
p.sendMessage(ChatColor.RED + late);
return;
}
@ -483,7 +483,7 @@ public class Quester {
// Ensure that we're past the initial duration
if (currentTime > end) {
final int maxSize = 2;
final LinkedHashMap<Long, Long> cache = new LinkedHashMap<Long, Long>() {
final LinkedHashMap<Long, Long> mostRecent = new LinkedHashMap<Long, Long>() {
private static final long serialVersionUID = 3046838061019897713L;
@Override
@ -492,31 +492,41 @@ public class Quester {
}
};
// Store both the upcoming and most recent period of activity
// Get last completed time
long completedTime = 0L;
if (getCompletedTimes().containsKey(q.getName())) {
completedTime = getCompletedTimes().get(q.getName());
}
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;
cache.put(nextStart, nextEnd);
mostRecent.put(nextStart, nextEnd);
}
// Check whether the quest is currently active
boolean active = false;
for (Entry<Long, Long> startEnd : cache.entrySet()) {
for (Entry<Long, Long> startEnd : mostRecent.entrySet()) {
if (startEnd.getKey() <= currentTime && currentTime < startEnd.getValue()) {
active = true;
}
}
// If quest is not active, or new period of activity should override player cooldown, inform user
if (!active || (q.getPlanner().getOverride() && getCompletedTimes().containsKey(q.getName())
&& currentTime < (getCompletedTimes().get(q.getName()) + duration))) {
if (!active | (q.getPlanner().getOverride() && completedEnd > 0L
&& currentTime < (completedEnd + repeat))) {
if (p.isOnline()) {
final String early = Lang.get("plnTooEarly")
.replace("<quest>", ChatColor.AQUA + q.getName() + ChatColor.YELLOW)
.replace("<time>", ChatColor.DARK_PURPLE
+ MiscUtil.getTime(nextStart - currentTime) + ChatColor.YELLOW);
+ MiscUtil.getTime((completedEnd + repeat) - currentTime) + ChatColor.YELLOW);
p.sendMessage(ChatColor.YELLOW + early);
}
return;

View File

@ -204,7 +204,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
.thatExcludesNonPlayersWithMessage("Console may not perform this conversation!")
.addConversationAbandonedListener(this);
this.npcConversationFactory = new ConversationFactory(this).withModality(false)
.withFirstPrompt(new NpcOfferQuestPrompt(this)).withTimeout(settings.getAcceptTimeout())
.withFirstPrompt(new NpcOfferQuestPrompt()).withTimeout(settings.getAcceptTimeout())
.withLocalEcho(false).addConversationAbandonedListener(this);
// 10 - Register listeners

View File

@ -26,19 +26,13 @@ import me.blackvein.quests.Quest;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
import me.blackvein.quests.util.Lang;
import me.blackvein.quests.util.MiscUtil;
public class NpcOfferQuestPrompt extends StringPrompt {
private final Quests plugin;
public NpcOfferQuestPrompt(Quests plugin) {
this.plugin = plugin;
}
@SuppressWarnings("unchecked")
@Override
public String getPromptText(ConversationContext context) {
Quests plugin = (Quests)context.getPlugin();
Quester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
LinkedList<Quest> quests = (LinkedList<Quest>) context.getSessionData("npcQuests");
String npc = (String) context.getSessionData("npc");
@ -64,6 +58,7 @@ public class NpcOfferQuestPrompt extends StringPrompt {
@SuppressWarnings("unchecked")
@Override
public Prompt acceptInput(ConversationContext context, String input) {
Quests plugin = (Quests)context.getPlugin();
Quester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
LinkedList<Quest> quests = (LinkedList<Quest>) context.getSessionData("npcQuests");
int numInput = -1;
@ -101,15 +96,28 @@ public class NpcOfferQuestPrompt extends StringPrompt {
}
if (q == null) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("invalidOption"));
return new NpcOfferQuestPrompt(plugin);
return new NpcOfferQuestPrompt();
} else {
Player player = quester.getPlayer();
if (quester.canAcceptOffer(q, true)) {
quester.setQuestToTake(q.getName());
String s = extracted(plugin, quester);
for (String msg : s.split("<br>")) {
player.sendMessage(msg);
}
if (!plugin.getSettings().canAskConfirmation()) {
quester.takeQuest(q, false);
} else {
plugin.getConversationFactory().buildConversation((Conversable) player).begin();
}
}
/*Player player = quester.getPlayer();
if (!quester.getCompletedQuests().contains(q.getName())) {
if (quester.getCurrentQuests().size() < plugin.getSettings().getMaxQuests()
|| plugin.getSettings().getMaxQuests() < 1) {
if (q.testRequirements(quester)) {
quester.setQuestToTake(q.getName());
String s = extracted(quester);
String s = extracted(plugin, quester);
for (String msg : s.split("<br>")) {
player.sendMessage(msg);
}
@ -143,7 +151,7 @@ public class NpcOfferQuestPrompt extends StringPrompt {
player.sendMessage(ChatColor.YELLOW + completed);
} else {
quester.setQuestToTake(q.getName());
String s = extracted(quester);
String s = extracted(plugin, quester);
for (String msg : s.split("<br>")) {
player.sendMessage(msg);
}
@ -158,13 +166,13 @@ public class NpcOfferQuestPrompt extends StringPrompt {
msg = msg.replace("<number>", String.valueOf(plugin.getSettings().getMaxQuests()));
player.sendMessage(ChatColor.YELLOW + msg);
}
}
}*/
return Prompt.END_OF_CONVERSATION;
}
}
}
private String extracted(final Quester quester) {
private String extracted(final Quests plugin, final Quester quester) {
return MessageFormat.format("{0}- {1}{2}{3} -\n\n{4}{5}\n", ChatColor.GOLD, ChatColor.DARK_PURPLE,
quester.getQuestToTake(), ChatColor.GOLD, ChatColor.RESET, plugin.getQuest(quester.getQuestToTake())
.getDescription());