diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/EcoQuestsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/EcoQuestsPlugin.kt index 6423d9f..bc8c0bc 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/EcoQuestsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/EcoQuestsPlugin.kt @@ -75,6 +75,12 @@ class EcoQuestsPlugin : LibreforgePlugin() { } } } + + this.scheduler.runTimer(20, 20) { + for (quest in Quests.values()) { + quest.resetIfNeeded() + } + } } override fun loadListeners(): List { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt index 053b245..ed8e2f2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt @@ -2,6 +2,7 @@ package com.willfp.ecoquests.quests import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.core.data.ServerProfile import com.willfp.eco.core.data.keys.PersistentDataKey import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.core.data.profile @@ -17,6 +18,7 @@ import com.willfp.eco.util.toNiceString import com.willfp.ecoquests.api.event.PlayerQuestCompleteEvent import com.willfp.ecoquests.api.event.PlayerQuestStartEvent import com.willfp.ecoquests.tasks.Tasks +import com.willfp.ecoquests.util.formatDuration import com.willfp.libreforge.EmptyProvidedHolder import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.conditions.Conditions @@ -82,11 +84,33 @@ class Quest( ViolationContext(plugin, "quest $id start-conditions") ) + private val lastResetTimeKey = PersistentDataKey( + plugin.createNamespacedKey("quest_${id}_last_reset_time"), + PersistentDataKeyType.INT, + 0 + ) + + private val resetTime = config.getInt("reset-time") + + val minutesUntilReset: Int + get() = if (resetTime < 0) { + Int.MAX_VALUE + } else { + val currentMinutes = (System.currentTimeMillis() / 1000 / 60).toInt() + val previousMinutes = ServerProfile.load().read(lastResetTimeKey) + + resetTime - (currentMinutes - previousMinutes) + } + init { PlayerlessPlaceholder(plugin, "quest_${id}_name") { this.name } + PlayerlessPlaceholder(plugin, "quest_${id}_tasks") { + this.tasks.size.toNiceString() + } + PlayerPlaceholder(plugin, "quest_${id}_started") { hasStarted(it).toNiceString() } @@ -95,13 +119,13 @@ class Quest( hasCompleted(it).toNiceString() } - PlayerlessPlaceholder(plugin, "quest_${id}_tasks") { - this.tasks.size.toNiceString() - } - PlayerPlaceholder(plugin, "quest_${id}_tasks_completed") { this.tasks.count { t -> t.hasCompleted(it) }.toNiceString() } + + PlayerlessPlaceholder(plugin, "quest_${id}_time_until_reset") { + formatDuration(this.minutesUntilReset) + } } fun hasCompleted(player: Player): Boolean { @@ -137,6 +161,22 @@ class Quest( Bukkit.getPluginManager().callEvent(PlayerQuestStartEvent(player, this)) } + fun resetIfNeeded() { + if (resetTime < 0) { + return + } + + if (minutesUntilReset > 0) { + return + } + + ServerProfile.load().write(lastResetTimeKey, (System.currentTimeMillis() / 1000 / 60).toInt()) + + for (player in Bukkit.getOnlinePlayers()) { + reset(player) + } + } + fun checkCompletion(player: Player): Boolean { // Check if the player has completed the Quest before if (player.profile.read(hasCompletedKey)) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/util/Time.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/util/Time.kt new file mode 100644 index 0000000..571bcb2 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/util/Time.kt @@ -0,0 +1,36 @@ +package com.willfp.ecoquests.util + +const val MINUTES_IN_HOUR = 60 +const val HOURS_IN_DAY = 24 +const val DAYS_IN_MONTH = 30 + +fun formatDuration(minutes: Int): String { + if (minutes < 0) return "Invalid Duration" + + + val totalHours = minutes / MINUTES_IN_HOUR + val totalDays = totalHours / HOURS_IN_DAY + val totalMonths = totalDays / DAYS_IN_MONTH + + val remainingMinutes = minutes % MINUTES_IN_HOUR + val remainingHours = totalHours % HOURS_IN_DAY + val remainingDays = totalDays % DAYS_IN_MONTH + + val parts = mutableListOf() + + if (totalMonths > 0) { + parts.add("${totalMonths}mo") + } + + if (remainingDays > 0) { + parts.add("${remainingDays}d") + } + + if (remainingHours > 0) { + parts.add("${remainingHours}h") + } + + parts.add("${remainingMinutes}m") + + return parts.joinToString(" ") +} diff --git a/eco-core/core-plugin/src/main/resources/quests/_example.yml b/eco-core/core-plugin/src/main/resources/quests/_example.yml index 9903063..68b1ed2 100644 --- a/eco-core/core-plugin/src/main/resources/quests/_example.yml +++ b/eco-core/core-plugin/src/main/resources/quests/_example.yml @@ -3,6 +3,12 @@ name: "Example Quest" gui: item: paper name:"&eExample Quest" +# How many minutes between this quest being reset (set to -1 to disable) +# 1 Day: 1440 +# 1 Week: 10080 +# 1 Month: 43200 +reset-time: -1 + # A list of task IDs required to complete this quest. tasks: - break_100_stone