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 6833a39..d41c6a0 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,8 +5,6 @@ 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.keys.PersistentDataKey -import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.core.data.profile import org.bukkit.Bukkit import org.bukkit.OfflinePlayer @@ -16,50 +14,6 @@ import java.util.* private val plugin = BoostersPlugin.instance -object BoosterUtils { - private var active: ActivatedBooster? = null - - private val boosterKey = PersistentDataKey( - plugin.namespacedKeyFactory.create("active_booster"), - PersistentDataKeyType.STRING, - "" - ) - - var shouldUseSQL = false - - fun getActiveBooster(): ActivatedBooster? { - if (shouldUseSQL) { - 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 - ) - } - } else { - return active - } - } - - fun setActiveBooster(booster: ActivatedBooster?) { - if (shouldUseSQL) { - if (booster == null) { - Bukkit.getServer().profile.write(boosterKey, "") - } else { - Bukkit.getServer().profile.write(boosterKey, "${booster.booster.id}::${booster.player}") - } - } else { - active = booster - } - } -} - val OfflinePlayer.boosters: List get() { val found = mutableListOf() @@ -103,10 +57,10 @@ fun Player.activateBooster(booster: Booster): Boolean { for (expiryMessage in booster.expiryMessages) { Bukkit.broadcastMessage(expiryMessage) } - BoosterUtils.setActiveBooster(null) + plugin.activeBooster = null } - BoosterUtils.setActiveBooster(ActivatedBooster(booster, this.uniqueId)) + plugin.activeBooster = ActivatedBooster(booster, this.uniqueId) for (player in Bukkit.getOnlinePlayers()) { player.playSound( 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 5facd8a..7ce48d2 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 @@ -1,8 +1,13 @@ package com.willfp.boosters +import com.willfp.boosters.boosters.ActivatedBooster +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.keys.PersistentDataKey +import com.willfp.eco.core.data.keys.PersistentDataKeyType +import com.willfp.eco.core.data.profile import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry import com.willfp.eco.core.integrations.placeholder.PlaceholderManager import com.willfp.eco.util.ListUtils @@ -11,20 +16,63 @@ import com.willfp.eco.util.savedDisplayName import com.willfp.libreforge.LibReforgePlugin import org.bukkit.Bukkit import org.bukkit.event.Listener +import java.util.* class BoostersPlugin : LibReforgePlugin(0, 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, + "" + ) + + var activeBooster: ActivatedBooster? + get() { + if (shouldUseSQL) { + 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 + ) + } + } else { + return active + } + } + set(value) { + if (shouldUseSQL) { + if (value == null) { + Bukkit.getServer().profile.write(boosterKey, "") + } else { + Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}") + } + } else { + active = value + } + } + override fun handleEnableAdditional() { - BoosterUtils.shouldUseSQL = configYml.getBool("use-sql") - BoosterUtils.setActiveBooster(null) + shouldUseSQL = configYml.getBool("use-sql") + activeBooster = null PlaceholderManager.registerPlaceholder( PlaceholderEntry( this, "booster_info", { - val booster = BoosterUtils.getActiveBooster() + val booster = activeBooster if (booster == null) { return@PlaceholderEntry this.langYml.getString("no-currently-active") @@ -45,7 +93,7 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") { this, "active", { - BoosterUtils.getActiveBooster()?.booster?.id ?: "" + activeBooster?.booster?.id ?: "" }, false ) @@ -56,7 +104,7 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") { this, "active_name", { - BoosterUtils.getActiveBooster()?.booster?.name ?: "" + activeBooster?.booster?.name ?: "" }, false ) @@ -67,13 +115,13 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") { this, "active_player", { - BoosterUtils.getActiveBooster()?.player?.savedDisplayName ?: "" + activeBooster?.player?.savedDisplayName ?: "" }, false ) ) - this.registerHolderProvider { ListUtils.toSingletonList(BoosterUtils.getActiveBooster()?.booster) } + this.registerHolderProvider { ListUtils.toSingletonList(activeBooster?.booster) } } override fun loadListeners(): List { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandCancel.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandCancel.kt index 5e5e5a6..ad14462 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandCancel.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandCancel.kt @@ -1,12 +1,9 @@ package com.willfp.boosters.commands -import com.willfp.boosters.BoosterUtils -import com.willfp.boosters.gui.BoosterGUI +import com.willfp.boosters.BoostersPlugin import com.willfp.eco.core.EcoPlugin -import com.willfp.eco.core.command.impl.PluginCommand import com.willfp.eco.core.command.impl.Subcommand import org.bukkit.command.CommandSender -import org.bukkit.entity.Player class CommandCancel(plugin: EcoPlugin) : Subcommand( @@ -17,7 +14,7 @@ class CommandCancel(plugin: EcoPlugin) : ) { override fun onExecute(sender: CommandSender, args: List) { - BoosterUtils.setActiveBooster(null) + (plugin as BoostersPlugin).activeBooster = null sender.sendMessage(plugin.langYml.getMessage("cancelled")) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt index d8c8d22..c424fb3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt @@ -1,6 +1,5 @@ package com.willfp.boosters.gui -import com.willfp.boosters.BoosterUtils import com.willfp.boosters.BoostersPlugin import com.willfp.boosters.activateBooster import com.willfp.boosters.boosters.Booster @@ -23,7 +22,7 @@ object BoosterGUI { return SlotHandler { event, _, _ -> val player = event.whoClicked.tryAsPlayer() ?: return@SlotHandler - if (BoosterUtils.getActiveBooster() != null) { + if (plugin.activeBooster != null) { player.sendMessage(plugin.langYml.getMessage("already-active")) player.playSound( player.location,