Refactoring

This commit is contained in:
Auxilor
2022-02-13 15:58:40 +00:00
parent b6bf956791
commit 6e79fcdfcb
4 changed files with 60 additions and 62 deletions
@@ -5,8 +5,6 @@ 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.keys.PersistentDataKey
import com.willfp.eco.core.data.keys.PersistentDataKeyType
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
@@ -16,50 +14,6 @@ import java.util.*
private val plugin = BoostersPlugin.instance 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<Booster> val OfflinePlayer.boosters: List<Booster>
get() { get() {
val found = mutableListOf<Booster>() val found = mutableListOf<Booster>()
@@ -103,10 +57,10 @@ fun Player.activateBooster(booster: Booster): Boolean {
for (expiryMessage in booster.expiryMessages) { for (expiryMessage in booster.expiryMessages) {
Bukkit.broadcastMessage(expiryMessage) 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()) { for (player in Bukkit.getOnlinePlayers()) {
player.playSound( player.playSound(
@@ -1,8 +1,13 @@
package com.willfp.boosters 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.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.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.PlaceholderEntry
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
import com.willfp.eco.util.ListUtils import com.willfp.eco.util.ListUtils
@@ -11,20 +16,63 @@ import com.willfp.eco.util.savedDisplayName
import com.willfp.libreforge.LibReforgePlugin import com.willfp.libreforge.LibReforgePlugin
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.event.Listener import org.bukkit.event.Listener
import java.util.*
class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") { class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
val boostersYml = BoostersYml(this) 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() { override fun handleEnableAdditional() {
BoosterUtils.shouldUseSQL = configYml.getBool("use-sql") shouldUseSQL = configYml.getBool("use-sql")
BoosterUtils.setActiveBooster(null) activeBooster = null
PlaceholderManager.registerPlaceholder( PlaceholderManager.registerPlaceholder(
PlaceholderEntry( PlaceholderEntry(
this, this,
"booster_info", "booster_info",
{ {
val booster = BoosterUtils.getActiveBooster() val booster = activeBooster
if (booster == null) { if (booster == null) {
return@PlaceholderEntry this.langYml.getString("no-currently-active") return@PlaceholderEntry this.langYml.getString("no-currently-active")
@@ -45,7 +93,7 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
this, this,
"active", "active",
{ {
BoosterUtils.getActiveBooster()?.booster?.id ?: "" activeBooster?.booster?.id ?: ""
}, },
false false
) )
@@ -56,7 +104,7 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
this, this,
"active_name", "active_name",
{ {
BoosterUtils.getActiveBooster()?.booster?.name ?: "" activeBooster?.booster?.name ?: ""
}, },
false false
) )
@@ -67,13 +115,13 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
this, this,
"active_player", "active_player",
{ {
BoosterUtils.getActiveBooster()?.player?.savedDisplayName ?: "" activeBooster?.player?.savedDisplayName ?: ""
}, },
false false
) )
) )
this.registerHolderProvider { ListUtils.toSingletonList(BoosterUtils.getActiveBooster()?.booster) } this.registerHolderProvider { ListUtils.toSingletonList(activeBooster?.booster) }
} }
override fun loadListeners(): List<Listener> { override fun loadListeners(): List<Listener> {
@@ -1,12 +1,9 @@
package com.willfp.boosters.commands package com.willfp.boosters.commands
import com.willfp.boosters.BoosterUtils import com.willfp.boosters.BoostersPlugin
import com.willfp.boosters.gui.BoosterGUI
import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.command.impl.Subcommand import com.willfp.eco.core.command.impl.Subcommand
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class CommandCancel(plugin: EcoPlugin) : class CommandCancel(plugin: EcoPlugin) :
Subcommand( Subcommand(
@@ -17,7 +14,7 @@ class CommandCancel(plugin: EcoPlugin) :
) { ) {
override fun onExecute(sender: CommandSender, args: List<String>) { override fun onExecute(sender: CommandSender, args: List<String>) {
BoosterUtils.setActiveBooster(null) (plugin as BoostersPlugin).activeBooster = null
sender.sendMessage(plugin.langYml.getMessage("cancelled")) sender.sendMessage(plugin.langYml.getMessage("cancelled"))
} }
} }
@@ -1,6 +1,5 @@
package com.willfp.boosters.gui package com.willfp.boosters.gui
import com.willfp.boosters.BoosterUtils
import com.willfp.boosters.BoostersPlugin import com.willfp.boosters.BoostersPlugin
import com.willfp.boosters.activateBooster import com.willfp.boosters.activateBooster
import com.willfp.boosters.boosters.Booster import com.willfp.boosters.boosters.Booster
@@ -23,7 +22,7 @@ object BoosterGUI {
return SlotHandler { event, _, _ -> return SlotHandler { event, _, _ ->
val player = event.whoClicked.tryAsPlayer() ?: return@SlotHandler val player = event.whoClicked.tryAsPlayer() ?: return@SlotHandler
if (BoosterUtils.getActiveBooster() != null) { if (plugin.activeBooster != null) {
player.sendMessage(plugin.langYml.getMessage("already-active")) player.sendMessage(plugin.langYml.getMessage("already-active"))
player.playSound( player.playSound(
player.location, player.location,