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 package com.willfp.boosters
@@ -14,13 +14,21 @@ import org.bukkit.Sound
import org.bukkit.entity.Player import org.bukkit.entity.Player
import java.util.* import java.util.*
private var active: ActivatedBooster? = null
private val plugin = BoostersPlugin.instance private val plugin = BoostersPlugin.instance
private var usingSql: Boolean = false
var activeBooster: ActivatedBooster? object BoosterUtils {
get() { private var active: ActivatedBooster? = null
if (usingSql) {
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) val key = Bukkit.getServer().profile.read(boosterKey)
return if (key.isEmpty()) { return if (key.isEmpty()) {
@@ -38,29 +46,19 @@ var activeBooster: ActivatedBooster?
return active return active
} }
} }
set(value) {
if (usingSql) { fun setActiveBooster(booster: ActivatedBooster?) {
if (value == null) { if (shouldUseSQL) {
if (booster == 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, "${booster.booster.id}::${booster.player}")
} }
} else { } 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> val OfflinePlayer.boosters: List<Booster>
get() { get() {
@@ -105,10 +103,10 @@ fun Player.activateBooster(booster: Booster): Boolean {
for (expiryMessage in booster.expiryMessages) { for (expiryMessage in booster.expiryMessages) {
Bukkit.broadcastMessage(expiryMessage) Bukkit.broadcastMessage(expiryMessage)
} }
activeBooster = null BoosterUtils.setActiveBooster(null)
} }
active = ActivatedBooster(booster, this.uniqueId) BoosterUtils.setActiveBooster(ActivatedBooster(booster, this.uniqueId))
for (player in Bukkit.getOnlinePlayers()) { for (player in Bukkit.getOnlinePlayers()) {
player.playSound( player.playSound(
@@ -16,14 +16,15 @@ class BoostersPlugin : LibReforgePlugin(0, 14269, "&e") {
val boostersYml = BoostersYml(this) val boostersYml = BoostersYml(this)
override fun handleEnableAdditional() { override fun handleEnableAdditional() {
useSQL = configYml.getBool("use-sql") BoosterUtils.shouldUseSQL = configYml.getBool("use-sql")
BoosterUtils.setActiveBooster(null)
PlaceholderManager.registerPlaceholder( PlaceholderManager.registerPlaceholder(
PlaceholderEntry( PlaceholderEntry(
this, this,
"booster_info", "booster_info",
{ {
val booster = activeBooster val booster = BoosterUtils.getActiveBooster()
if (booster == null) { if (booster == null) {
return@PlaceholderEntry this.langYml.getString("no-currently-active") 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> { override fun loadListeners(): List<Listener> {
@@ -17,6 +17,7 @@ class CommandBoosters(plugin: EcoPlugin) :
init { init {
this.addSubcommand(CommandGive(plugin)) this.addSubcommand(CommandGive(plugin))
.addSubcommand(CommandReload(plugin)) .addSubcommand(CommandReload(plugin))
.addSubcommand(CommandCancel(plugin))
} }
override fun onExecute(sender: CommandSender, args: List<String>) { 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.reload: true
boosters.command.give: true boosters.command.give: true
boosters.command.boosters: true boosters.command.boosters: true
boosters.command.cancel: true
boosters.command.reload: boosters.command.reload:
description: Allows reloading the config description: Allows reloading the config
@@ -46,4 +47,7 @@ permissions:
default: true default: true
boosters.command.give: boosters.command.give:
description: Allows the use of /boosters give. description: Allows the use of /boosters give.
default: op
boosters.command.cancel:
description: Allows the use of /boosters cancel.
default: op default: op