Refactored and added /boosters cancel

This commit is contained in:
Auxilor
2022-02-13 15:50:01 +00:00
parent 51dcb5ec91
commit 56b70ed59d
5 changed files with 55 additions and 28 deletions
@@ -1,4 +1,4 @@
@file:JvmName("BoosterHandlers")
@file:JvmName("BoosterUtils")
package com.willfp.boosters
@@ -14,13 +14,21 @@ import org.bukkit.Sound
import org.bukkit.entity.Player
import java.util.*
private var active: ActivatedBooster? = null
private val plugin = BoostersPlugin.instance
private var usingSql: Boolean = false
var activeBooster: ActivatedBooster?
get() {
if (usingSql) {
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()) {
@@ -38,29 +46,19 @@ var activeBooster: ActivatedBooster?
return active
}
}
set(value) {
if (usingSql) {
if (value == null) {
fun setActiveBooster(booster: ActivatedBooster?) {
if (shouldUseSQL) {
if (booster == null) {
Bukkit.getServer().profile.write(boosterKey, "")
} else {
Bukkit.getServer().profile.write(boosterKey, "${value.booster.id}::${value.player}")
Bukkit.getServer().profile.write(boosterKey, "${booster.booster.id}::${booster.player}")
}
} else {
active = value
active = booster
}
}
private val boosterKey = PersistentDataKey(
plugin.namespacedKeyFactory.create("active_booster"),
PersistentDataKeyType.STRING,
""
)
var useSQL: Boolean
get() = usingSql
set(value) {
usingSql = value
}
}
val OfflinePlayer.boosters: List<Booster>
get() {
@@ -105,10 +103,10 @@ fun Player.activateBooster(booster: Booster): Boolean {
for (expiryMessage in booster.expiryMessages) {
Bukkit.broadcastMessage(expiryMessage)
}
activeBooster = null
BoosterUtils.setActiveBooster(null)
}
active = ActivatedBooster(booster, this.uniqueId)
BoosterUtils.setActiveBooster(ActivatedBooster(booster, this.uniqueId))
for (player in Bukkit.getOnlinePlayers()) {
player.playSound(
@@ -16,14 +16,15 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
val boostersYml = BoostersYml(this)
override fun handleEnableAdditional() {
useSQL = configYml.getBool("use-sql")
BoosterUtils.shouldUseSQL = configYml.getBool("use-sql")
BoosterUtils.setActiveBooster(null)
PlaceholderManager.registerPlaceholder(
PlaceholderEntry(
this,
"booster_info",
{
val booster = activeBooster
val booster = BoosterUtils.getActiveBooster()
if (booster == null) {
return@PlaceholderEntry this.langYml.getString("no-currently-active")
@@ -39,7 +40,7 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
)
)
this.registerHolderProvider { ListUtils.toSingletonList(activeBooster?.booster) }
this.registerHolderProvider { ListUtils.toSingletonList(BoosterUtils.getActiveBooster()?.booster) }
}
override fun loadListeners(): List<Listener> {
@@ -17,6 +17,7 @@ class CommandBoosters(plugin: EcoPlugin) :
init {
this.addSubcommand(CommandGive(plugin))
.addSubcommand(CommandReload(plugin))
.addSubcommand(CommandCancel(plugin))
}
override fun onExecute(sender: CommandSender, args: List<String>) {
@@ -0,0 +1,23 @@
package com.willfp.boosters.commands
import com.willfp.boosters.BoosterUtils
import com.willfp.boosters.gui.BoosterGUI
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(
plugin,
"cancel",
"boosters.command.cancel",
false
) {
override fun onExecute(sender: CommandSender, args: List<String>) {
BoosterUtils.setActiveBooster(null)
sender.sendMessage(plugin.langYml.getMessage("cancelled"))
}
}
@@ -37,6 +37,7 @@ permissions:
boosters.command.reload: true
boosters.command.give: true
boosters.command.boosters: true
boosters.command.cancel: true
boosters.command.reload:
description: Allows reloading the config
@@ -46,4 +47,7 @@ permissions:
default: true
boosters.command.give:
description: Allows the use of /boosters give.
default: op
boosters.command.cancel:
description: Allows the use of /boosters cancel.
default: op