EpicKnarvik97 6e2832d616 Merge branch 'custom-formatting' into quests-5
# Conflicts:
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/NPCQuestInfo.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestAreaHandler.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestPlannerInfoGenerator.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestRequirementsInfoGenerator.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestRewardsInfoGenerator.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestStagesInfoGenerator.java
#	src/main/java/net/knarcraft/dynmapcitizens/handler/trait/quests/QuestsHandler.java
#	src/main/java/net/knarcraft/dynmapcitizens/util/QuestsHelper.java
2024-01-02 22:04:43 +01:00

115 lines
3.9 KiB
Java

package net.knarcraft.dynmapcitizens.util;
import me.pikamug.quests.quests.Quest;
import me.pikamug.quests.quests.components.Planner;
import net.knarcraft.dynmapcitizens.DynmapCitizens;
import net.knarcraft.dynmapcitizens.handler.VaultHandler;
import net.knarcraft.dynmapcitizens.handler.trait.quests.QuestNPCType;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* A helper class for various quests-related tasks
*/
public class QuestsHelper {
/**
* Gets an item stack string with the first character upper-cased
*
* @param itemStack <p>The item stack to get the string for</p>
* @return <p>A string describing the item stack</p>
*/
public static @NotNull String getUpperCasedItemStackString(@NotNull ItemStack itemStack) {
return uppercaseFirst(getItemStackString(itemStack));
}
/**
* Gets the proper string representation of an item stack
*
* @param itemStack <p>The item stack to print</p>
* @return <p>The string representation of the item stack</p>
*/
public static @NotNull String getItemStackString(@NotNull ItemStack itemStack) {
return normalizeName(itemStack.getType().name()) + " x " + itemStack.getAmount();
}
/**
* Makes the first character in a string uppercase
*
* @param string <p>The string to run on</p>
* @return <p>The same string, with the first character converted to uppercase</p>
*/
private static @NotNull String uppercaseFirst(@NotNull String string) {
return string.substring(0, 1).toUpperCase() + string.substring(1);
}
/**
* Normalizes an internal name to make it human-readable
*
* @param name <p>The name to normalize</p>
* @return <p>The normalized name</p>
*/
public static @NotNull String normalizeName(@NotNull String name) {
return name.toLowerCase().replace("_", " ");
}
/**
* Gets the currency to print for the given amount of money
*
* @param money <p>The amount to pay/use</p>
* @return <p>The currency name to use</p>
*/
public static @NotNull String getCurrency(double money) {
VaultHandler vaultHandler = DynmapCitizens.getInstance().getVaultHandler();
if (vaultHandler.isEnabled()) {
return vaultHandler.getCurrency(money != 1);
} else {
return "money";
}
}
/**
* Checks whether the given quest is unavailable, according to its planner information
*
* @param quest <p>The quest to check for availability</p>
* @return <p>True if the quest is unavailable</p>
*/
public static boolean isQuestUnavailable(@NotNull Quest quest) {
Planner planner = quest.getPlanner();
long currentTime = System.currentTimeMillis();
if (!planner.hasEnd() && !planner.hasStart()) {
return false;
}
if (!planner.hasStart()) {
//If past the end datetime, the quest is no longer available
return currentTime > planner.getEndInMillis();
}
if (!planner.hasEnd()) {
//If before the start datetime, the quest is not yet available
return currentTime < planner.getStartInMillis();
}
if (!planner.hasRepeat()) {
//If outside the start and end dates, the quest is unavailable
return currentTime < planner.getStartInMillis() || currentTime > planner.getEndInMillis();
}
long startTime = planner.getStartInMillis();
long endTime = planner.getEndInMillis();
do {
if (currentTime > startTime && currentTime < endTime) {
return false;
}
startTime += planner.getRepeat();
endTime += planner.getRepeat();
//If startTime > currentTime, current time cannot be within the interval
} while (startTime < currentTime);
return true;
}
}