diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt index d41c6a0..3247335 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt @@ -5,12 +5,12 @@ package com.willfp.boosters import com.willfp.boosters.boosters.ActivatedBooster import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Boosters +import com.willfp.eco.core.data.ServerProfile import com.willfp.eco.core.data.profile import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.Sound import org.bukkit.entity.Player -import java.util.* private val plugin = BoostersPlugin.instance @@ -53,12 +53,10 @@ fun Player.activateBooster(booster: Booster): Boolean { Bukkit.broadcastMessage(activationMessage) } - plugin.scheduler.runLater(booster.duration.toLong()) { - for (expiryMessage in booster.expiryMessages) { - Bukkit.broadcastMessage(expiryMessage) - } - plugin.activeBooster = null - } + ServerProfile.load().write( + plugin.expiryTimeKey, + (booster.duration.toDouble() * 50) + System.currentTimeMillis() + ) plugin.activeBooster = ActivatedBooster(booster, this.uniqueId) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoostersPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoostersPlugin.kt index bd780ad..f2f09aa 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoostersPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoostersPlugin.kt @@ -5,6 +5,7 @@ import com.willfp.boosters.boosters.Boosters import com.willfp.boosters.commands.CommandBoosters import com.willfp.boosters.config.BoostersYml import com.willfp.eco.core.command.impl.PluginCommand +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 @@ -20,53 +21,45 @@ import java.util.* class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") { val boostersYml = BoostersYml(this) - private var shouldUseSQL = false - - private var active: ActivatedBooster? = null private val boosterKey = PersistentDataKey( this.namespacedKeyFactory.create("active_booster"), PersistentDataKeyType.STRING, "" - ) + ).server() + + val expiryTimeKey = PersistentDataKey( + this.namespacedKeyFactory.create("expiry_time"), + PersistentDataKeyType.DOUBLE, + 0.0 + ).server() var activeBooster: ActivatedBooster? get() { - if (shouldUseSQL) { - val key = Bukkit.getServer().profile.read(boosterKey) + val key = Bukkit.getServer().profile.read(boosterKey) - return if (key.isEmpty()) { - null - } else { - val booster = key.split("::") - val id = booster[0] - val uuid = UUID.fromString(booster[1]) - ActivatedBooster( - Boosters.getByID(id) ?: return null, - uuid - ) - } + return if (key.isEmpty()) { + null } else { - return active + val booster = key.split("::") + val id = booster[0] + val uuid = UUID.fromString(booster[1]) + ActivatedBooster( + Boosters.getByID(id) ?: return null, + uuid + ) } } set(value) { - if (shouldUseSQL) { - if (value == null) { - Bukkit.getServer().profile.write(boosterKey, "") - } else { - Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}") - } + if (value == null) { + Bukkit.getServer().profile.write(boosterKey, "") } else { - active = value + Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}") } } override fun handleEnableAdditional() { - shouldUseSQL = configYml.getBool("use-sql") - activeBooster = null - PlaceholderManager.registerPlaceholder( PlaceholderEntry( this, @@ -124,6 +117,19 @@ class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") { this.registerHolderProvider { ListUtils.toSingletonList(activeBooster?.booster) } } + override fun handleReloadAdditional() { + this.scheduler.runTimer(1, 1) { + val booster = activeBooster ?: return@runTimer + val endTime = ServerProfile.load().read(expiryTimeKey) + if (endTime <= System.currentTimeMillis()) { + for (expiryMessage in booster.booster.expiryMessages) { + Bukkit.broadcastMessage(expiryMessage) + } + activeBooster = null + } + } + } + override fun loadListeners(): List { return listOf( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt index e219132..a02c37f 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt @@ -18,32 +18,17 @@ import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.* -/* -Stored externally to fix the weirdest bug of all time, that I don't understand. -I think it comes from reload behaviour, where the identities of the keys aren't the same, -even though the keys are - genuinely not a clue, and this took me twice as long to fix as it -took me to write the entire rest of the plugin. - */ -private val dataKeyTracker = mutableMapOf>() - class Booster( private val plugin: BoostersPlugin, val config: Config, ) : Holder { val id = config.getString("id") - val dataKey: PersistentDataKey - get() { - if (!dataKeyTracker.containsKey(id)) { - dataKeyTracker[id] = PersistentDataKey( - plugin.namespacedKeyFactory.create(id), - PersistentDataKeyType.INT, - 0 - ) - } - - return dataKeyTracker[id]!! - } + val dataKey: PersistentDataKey = PersistentDataKey( + plugin.namespacedKeyFactory.create(id), + PersistentDataKeyType.INT, + 0 + ).player() val name = config.getFormattedString("name") diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 3af98ce..88c72a3 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -3,11 +3,6 @@ # by Auxilor # -# Will synchronize active booster over all servers in a network. -# SQL Settings are in /plugins/eco/config.yml -# If false, boosters will be server-specific. -use-sql: false - gui: title: Boosters rows: 3