Refactoring
This commit is contained in:
@@ -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<Booster>
|
||||
get() {
|
||||
val found = mutableListOf<Booster>()
|
||||
@@ -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(
|
||||
|
||||
@@ -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<Listener> {
|
||||
|
||||
@@ -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<String>) {
|
||||
BoosterUtils.setActiveBooster(null)
|
||||
(plugin as BoostersPlugin).activeBooster = null
|
||||
sender.sendMessage(plugin.langYml.getMessage("cancelled"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user