Skip to content

Commit

Permalink
Add time limit placeholder (closes #524)
Browse files Browse the repository at this point in the history
  • Loading branch information
LMBishop committed Aug 7, 2023
1 parent 104e1f6 commit ae262e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Expand Up @@ -188,6 +188,14 @@ public String onPlaceholderRequest(Player p, String params) {
result = "0";
}
break;
case "timeleft":
if (qPlayer.hasStartedQuest(quest)) {
long timeLeft = qPlayer.getQuestProgressFile().getTimeRemainingFor(quest);
result = timeLeft != -1 ? Format.formatTime(TimeUnit.SECONDS.convert(timeLeft, TimeUnit.MILLISECONDS)) : Messages.PLACEHOLDERAPI_NO_TIME_LIMIT.getMessage();
} else {
result = "0";
}
break;
case "canaccept":
result = (qPlayer.canStartQuest(quest) == QuestStartResult.QUEST_SUCCESS ? Messages.PLACEHOLDERAPI_TRUE.getMessageLegacyColor() : Messages.PLACEHOLDERAPI_FALSE.getMessageLegacyColor());
break;
Expand Down
@@ -1,10 +1,12 @@
package com.leonardobishop.quests.bukkit.menu.itemstack;

import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import com.leonardobishop.quests.bukkit.util.Format;
import com.leonardobishop.quests.bukkit.util.Messages;
import com.leonardobishop.quests.bukkit.util.chat.Chat;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress;
import com.leonardobishop.quests.common.player.questprogressfile.QuestProgressFile;
import com.leonardobishop.quests.common.quest.Quest;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -16,6 +18,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -115,6 +118,7 @@ public ItemStack toItemStack(Quest quest, QPlayer qPlayer, QuestProgress questPr
if (questProgress != null) {
for (String s : tempLore) {
s = processPlaceholders(s, questProgress);
s = processTimeLeft(s, quest, qPlayer.getQuestProgressFile());
if (plugin.getQuestsConfig().getBoolean("options.gui-use-placeholderapi")) {
s = plugin.getPlaceholderAPIProcessor().apply(player, s);
}
Expand Down Expand Up @@ -158,4 +162,14 @@ public static String processPlaceholders(String s, QuestProgress questProgress)
}
return s;
}
}

public static String processTimeLeft(String s, Quest quest, QuestProgressFile questProgressFile) {
String timeLeft;
if (quest.isTimeLimitEnabled()) {
timeLeft = Format.formatTime(TimeUnit.SECONDS.convert(questProgressFile.getTimeRemainingFor(quest), TimeUnit.MILLISECONDS));
} else {
timeLeft = Chat.legacyColor(Messages.UI_PLACEHOLDERS_NO_TIME_LIMIT.getMessageLegacyColor());
}
return s.replace("{timeleft}", timeLeft);
}
}
Expand Up @@ -71,9 +71,12 @@ public enum Messages {
UI_PLACEHOLDERS_TRUE("messages.ui-placeholder-completed-true", "true"),
UI_PLACEHOLDERS_FALSE("messages.ui-placeholder-completed-false", "false"),
UI_PLACEHOLDERS_TRUNCATED("messages.ui-placeholder-truncated", " +{amount} more"),
UI_PLACEHOLDERS_NO_TIME_LIMIT("messages.ui-placeholder-no-time-limit", "No time limit"),
PLACEHOLDERAPI_TRUE("messages.placeholderapi-true", "true"),
PLACEHOLDERAPI_FALSE("messages.placeholderapi-false", "false"),
PLACEHOLDERAPI_NO_TRACKED_QUEST("messages.placeholderapi-no-tracked-quest", "No tracked quest"),
PLACEHOLDERAPI_QUEST_NOT_STARTED("messages.placeholderapi-quest-not-started", "Quest not started"),
PLACEHOLDERAPI_NO_TIME_LIMIT("messages.placeholderapi-no-time-limit", "No time limit"),
PLACEHOLDERAPI_DATA_NOT_LOADED("messages.placeholderapi-data-not-loaded", "Data not loaded");

static {
Expand Down
3 changes: 3 additions & 0 deletions bukkit/src/main/resources/resources/bukkit/config.yml
Expand Up @@ -415,9 +415,12 @@ messages:
ui-placeholder-completed-true: "true"
ui-placeholder-completed-false: "false"
ui-placeholder-truncated: " +{amount} more"
ui-placeholder-no-time-limit: "No time limit"
placeholderapi-true: "true"
placeholderapi-false: "false"
placeholderapi-no-tracked-quest: "No tracked quest"
placeholderapi-quest-not-started: "Quest not started"
placeholderapi-no-time-limit: "No time limit"
placeholderapi-data-not-loaded: "Data not loaded"


Expand Down

0 comments on commit ae262e0

Please sign in to comment.