9
0
mirror of https://github.com/Auxilor/EcoQuests.git synced 2025-12-26 18:39:10 +00:00

Added auto-resettable quests (for daily / weekly / monthly) quests

This commit is contained in:
Auxilor
2023-08-08 22:55:16 +01:00
parent 97e86946da
commit 81694b3b3c
4 changed files with 92 additions and 4 deletions

View File

@@ -75,6 +75,12 @@ class EcoQuestsPlugin : LibreforgePlugin() {
}
}
}
this.scheduler.runTimer(20, 20) {
for (quest in Quests.values()) {
quest.resetIfNeeded()
}
}
}
override fun loadListeners(): List<Listener> {

View File

@@ -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)) {

View File

@@ -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<String>()
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(" ")
}

View File

@@ -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