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 2e69307..6833a39 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 @@ -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 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( 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 c10ae56..a72548e 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 @@ -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 { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandBoosters.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandBoosters.kt index da9ea3f..183855e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandBoosters.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandBoosters.kt @@ -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) { 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 new file mode 100644 index 0000000..5e5e5a6 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandCancel.kt @@ -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) { + BoosterUtils.setActiveBooster(null) + sender.sendMessage(plugin.langYml.getMessage("cancelled")) + } +} diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index eb9de59..84d677e 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -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 \ No newline at end of file