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.ActivatedBooster
import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Booster
import com.willfp.boosters.boosters.Boosters import com.willfp.boosters.boosters.Boosters
import com.willfp.eco.core.data.ServerProfile
import com.willfp.eco.core.data.profile import com.willfp.eco.core.data.profile
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer import org.bukkit.OfflinePlayer
import org.bukkit.Sound import org.bukkit.Sound
import org.bukkit.entity.Player import org.bukkit.entity.Player
import java.util.*
private val plugin = BoostersPlugin.instance private val plugin = BoostersPlugin.instance
@@ -53,12 +53,10 @@ fun Player.activateBooster(booster: Booster): Boolean {
Bukkit.broadcastMessage(activationMessage) Bukkit.broadcastMessage(activationMessage)
} }
plugin.scheduler.runLater(booster.duration.toLong()) { ServerProfile.load().write(
for (expiryMessage in booster.expiryMessages) { plugin.expiryTimeKey,
Bukkit.broadcastMessage(expiryMessage) (booster.duration.toDouble() * 50) + System.currentTimeMillis()
} )
plugin.activeBooster = null
}
plugin.activeBooster = ActivatedBooster(booster, this.uniqueId) 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.commands.CommandBoosters
import com.willfp.boosters.config.BoostersYml import com.willfp.boosters.config.BoostersYml
import com.willfp.eco.core.command.impl.PluginCommand 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.PersistentDataKey
import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.core.data.keys.PersistentDataKeyType
import com.willfp.eco.core.data.profile import com.willfp.eco.core.data.profile
@@ -20,19 +21,21 @@ import java.util.*
class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") { class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") {
val boostersYml = BoostersYml(this) val boostersYml = BoostersYml(this)
private var shouldUseSQL = false
private var active: ActivatedBooster? = null
private val boosterKey = PersistentDataKey( private val boosterKey = PersistentDataKey(
this.namespacedKeyFactory.create("active_booster"), this.namespacedKeyFactory.create("active_booster"),
PersistentDataKeyType.STRING, PersistentDataKeyType.STRING,
"" ""
) ).server()
val expiryTimeKey = PersistentDataKey(
this.namespacedKeyFactory.create("expiry_time"),
PersistentDataKeyType.DOUBLE,
0.0
).server()
var activeBooster: ActivatedBooster? var activeBooster: ActivatedBooster?
get() { get() {
if (shouldUseSQL) {
val key = Bukkit.getServer().profile.read(boosterKey) val key = Bukkit.getServer().profile.read(boosterKey)
return if (key.isEmpty()) { return if (key.isEmpty()) {
@@ -46,27 +49,17 @@ class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") {
uuid uuid
) )
} }
} else {
return active
}
} }
set(value) { set(value) {
if (shouldUseSQL) {
if (value == null) { if (value == null) {
Bukkit.getServer().profile.write(boosterKey, "") Bukkit.getServer().profile.write(boosterKey, "")
} else { } else {
Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}") Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}")
} }
} else {
active = value
}
} }
override fun handleEnableAdditional() { override fun handleEnableAdditional() {
shouldUseSQL = configYml.getBool("use-sql")
activeBooster = null
PlaceholderManager.registerPlaceholder( PlaceholderManager.registerPlaceholder(
PlaceholderEntry( PlaceholderEntry(
this, this,
@@ -124,6 +117,19 @@ class BoostersPlugin : LibReforgePlugin(2036, 14269, "&e") {
this.registerHolderProvider { ListUtils.toSingletonList(activeBooster?.booster) } 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> { override fun loadListeners(): List<Listener> {
return listOf( return listOf(
@@ -18,32 +18,17 @@ import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import java.util.* 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( class Booster(
private val plugin: BoostersPlugin, private val plugin: BoostersPlugin,
val config: Config, val config: Config,
) : Holder { ) : Holder {
val id = config.getString("id") val id = config.getString("id")
val dataKey: PersistentDataKey<Int> val dataKey: PersistentDataKey<Int> = PersistentDataKey(
get() {
if (!dataKeyTracker.containsKey(id)) {
dataKeyTracker[id] = PersistentDataKey(
plugin.namespacedKeyFactory.create(id), plugin.namespacedKeyFactory.create(id),
PersistentDataKeyType.INT, PersistentDataKeyType.INT,
0 0
) ).player()
}
return dataKeyTracker[id]!!
}
val name = config.getFormattedString("name") val name = config.getFormattedString("name")
@@ -3,11 +3,6 @@
# by Auxilor # 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: gui:
title: Boosters title: Boosters
rows: 3 rows: 3