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
The item stack to get the string for
* @returnA string describing the item stack
*/ public static @NotNull String getUpperCasedItemStackString(@NotNull ItemStack itemStack) { return uppercaseFirst(getItemStackString(itemStack)); } /** * Gets the proper string representation of an item stack * * @param itemStackThe item stack to print
* @returnThe string representation of the item stack
*/ 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 stringThe string to run on
* @returnThe same string, with the first character converted to uppercase
*/ 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 nameThe name to normalize
* @returnThe normalized name
*/ public static @NotNull String normalizeName(@NotNull String name) { return name.toLowerCase().replace("_", " "); } /** * Gets the currency to print for the given amount of money * * @param moneyThe amount to pay/use
* @returnThe currency name to use
*/ 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 questThe quest to check for availability
* @returnTrue if the quest is unavailable
*/ 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; } }