Boosters now use server persistence

This commit is contained in:
Auxilor
2022-02-17 16:28:59 +00:00
parent 60b9f6e516
commit bdc83e05f2
4 changed files with 44 additions and 60 deletions
@@ -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)
@@ -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<Listener> {
return listOf(
@@ -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<String, PersistentDataKey<Int>>()
class Booster(
private val plugin: BoostersPlugin,
val config: Config,
) : Holder {
val id = config.getString("id")
val dataKey: PersistentDataKey<Int>
get() {
if (!dataKeyTracker.containsKey(id)) {
dataKeyTracker[id] = PersistentDataKey(
plugin.namespacedKeyFactory.create(id),
PersistentDataKeyType.INT,
0
)
}
return dataKeyTracker[id]!!
}
val dataKey: PersistentDataKey<Int> = PersistentDataKey(
plugin.namespacedKeyFactory.create(id),
PersistentDataKeyType.INT,
0
).player()
val name = config.getFormattedString("name")
@@ -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