diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterHandlers.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt similarity index 90% rename from eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterHandlers.kt rename to eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt index 0ca734e..346e239 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterHandlers.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/BoosterUtils.kt @@ -44,6 +44,10 @@ fun OfflinePlayer.setAmountOfBooster(booster: Booster, amount: Int) { this.profile.write(booster.dataKey, amount) } +fun OfflinePlayer.incrementBoosters(booster: Booster, amount: Int) { + this.setAmountOfBooster(booster, this.getAmountOfBooster(booster) + amount) +} + fun Player.activateBooster(booster: Booster): Boolean { val amount = this.getAmountOfBooster(booster) @@ -51,7 +55,7 @@ fun Player.activateBooster(booster: Booster): Boolean { return false } - setAmountOfBooster(booster, amount - 1) + this.setAmountOfBooster(booster, amount - 1) for (activationMessage in booster.getActivationMessages(this)) { Bukkit.broadcastMessage(activationMessage) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt index 5c9212c..ae930f9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Booster.kt @@ -16,17 +16,32 @@ import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.UUID +/* +Stored externally to fix the weirdest bug of all time, that I don't understand. +I think it comes from reload behaviour, where the identities of the keys aren't the same, +even though the keys are - genuinely not a clue, and this took me twice as long to fix as it +took me to write the entire rest of the plugin. + */ +private val dataKeyTracker = mutableMapOf>() + class Booster( - plugin: BoostersPlugin, + private val plugin: BoostersPlugin, val config: Config, ) : Holder { val id = config.getString("id") - val dataKey = PersistentDataKey( - plugin.namespacedKeyFactory.create(id), - PersistentDataKeyType.INT, - 0 - ) + val dataKey: PersistentDataKey + get() { + if (!dataKeyTracker.containsKey(id)) { + dataKeyTracker[id] = PersistentDataKey( + plugin.namespacedKeyFactory.create(id), + PersistentDataKeyType.INT, + 0 + ) + } + + return dataKeyTracker[id]!! + } val name = config.getFormattedString("name") @@ -48,6 +63,9 @@ class Booster( val expiryMessages: List = config.getStrings("messages.expiry") fun getGuiItem(player: Player): ItemStack { + val amount = player.getAmountOfBooster(this) + println("$id: $amount") + return ItemStackBuilder(Items.lookup(config.getString("gui.item"))) .setDisplayName(config.getFormattedString("gui.name")) .addLoreLines( diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Boosters.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Boosters.kt index 403ac31..ded2665 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Boosters.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/boosters/Boosters.kt @@ -46,7 +46,7 @@ object Boosters { EffectChains.compile(it, "Effect Chains") } for (booster in values()) { - removeSet(booster) + removeBooster(booster) } for (config in plugin.boostersYml.getSubsections("boosters")) { Booster(plugin, config) @@ -70,7 +70,7 @@ object Boosters { * @param booster The [Booster] to remove. */ @JvmStatic - fun removeSet(booster: Booster) { + fun removeBooster(booster: Booster) { BY_ID.remove(booster.id) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandGive.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandGive.kt index 4469b96..243598f 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandGive.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/commands/CommandGive.kt @@ -1,8 +1,7 @@ package com.willfp.boosters.commands import com.willfp.boosters.boosters.Boosters -import com.willfp.boosters.getAmountOfBooster -import com.willfp.boosters.setAmountOfBooster +import com.willfp.boosters.incrementBoosters import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.impl.Subcommand import com.willfp.eco.util.StringUtils @@ -42,25 +41,21 @@ class CommandGive(plugin: EcoPlugin) : amount = args[2].toIntOrNull() ?: amount } - this.plugin.scheduler.runAsync { - @Suppress("DEPRECATION") - val player = Bukkit.getOfflinePlayer(args[0]) - if (!player.hasPlayedBefore()) { - sender.sendMessage(plugin.langYml.getMessage("invalid-player")) - return@runAsync - } - - this.plugin.scheduler.run { - player.setAmountOfBooster(booster, player.getAmountOfBooster(booster) + amount) - } - - sender.sendMessage( - plugin.langYml.getMessage("gave-booster", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) - .replace("%player%", player.name ?: return@runAsync) - .replace("%booster%", booster.name) - .replace("%amount%", amount.toString()) - ) + @Suppress("DEPRECATION") + val player = Bukkit.getOfflinePlayer(args[0]) + if (!player.hasPlayedBefore()) { + sender.sendMessage(plugin.langYml.getMessage("invalid-player")) + return } + + player.incrementBoosters(booster, amount) + + sender.sendMessage( + plugin.langYml.getMessage("gave-booster", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%player%", player.name ?: return) + .replace("%booster%", booster.name) + .replace("%amount%", amount.toString()) + ) } override fun tabComplete(sender: CommandSender, args: List): List { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt index 4d7854f..b4f3903 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/boosters/gui/BoosterGUI.kt @@ -67,13 +67,6 @@ object BoosterGUI { slot( { player, _ -> booster.getGuiItem(player) } ) { - setUpdater { player, _, prev -> - val newItem = booster.getGuiItem(player) - prev.itemMeta = newItem.itemMeta - prev.type = newItem.type - prev.itemMeta?.lore?.forEach { println(it) } - prev - } onLeftClick(makeHandler(booster, plugin)) } )