Fixed mismatched data keys

This commit is contained in:
Auxilor
2022-02-11 10:22:06 +00:00
parent 1f22a2590b
commit 948ff63dbd
5 changed files with 46 additions and 36 deletions
@@ -44,6 +44,10 @@ fun OfflinePlayer.setAmountOfBooster(booster: Booster, amount: Int) {
this.profile.write(booster.dataKey, amount) 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 { fun Player.activateBooster(booster: Booster): Boolean {
val amount = this.getAmountOfBooster(booster) val amount = this.getAmountOfBooster(booster)
@@ -51,7 +55,7 @@ fun Player.activateBooster(booster: Booster): Boolean {
return false return false
} }
setAmountOfBooster(booster, amount - 1) this.setAmountOfBooster(booster, amount - 1)
for (activationMessage in booster.getActivationMessages(this)) { for (activationMessage in booster.getActivationMessages(this)) {
Bukkit.broadcastMessage(activationMessage) Bukkit.broadcastMessage(activationMessage)
@@ -16,17 +16,32 @@ import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import java.util.UUID 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<String, PersistentDataKey<Int>>()
class Booster( class Booster(
plugin: BoostersPlugin, private val plugin: BoostersPlugin,
val config: Config, val config: Config,
) : Holder { ) : Holder {
val id = config.getString("id") val id = config.getString("id")
val dataKey = PersistentDataKey( val dataKey: PersistentDataKey<Int>
get() {
if (!dataKeyTracker.containsKey(id)) {
dataKeyTracker[id] = PersistentDataKey(
plugin.namespacedKeyFactory.create(id), plugin.namespacedKeyFactory.create(id),
PersistentDataKeyType.INT, PersistentDataKeyType.INT,
0 0
) )
}
return dataKeyTracker[id]!!
}
val name = config.getFormattedString("name") val name = config.getFormattedString("name")
@@ -48,6 +63,9 @@ class Booster(
val expiryMessages: List<String> = config.getStrings("messages.expiry") val expiryMessages: List<String> = config.getStrings("messages.expiry")
fun getGuiItem(player: Player): ItemStack { fun getGuiItem(player: Player): ItemStack {
val amount = player.getAmountOfBooster(this)
println("$id: $amount")
return ItemStackBuilder(Items.lookup(config.getString("gui.item"))) return ItemStackBuilder(Items.lookup(config.getString("gui.item")))
.setDisplayName(config.getFormattedString("gui.name")) .setDisplayName(config.getFormattedString("gui.name"))
.addLoreLines( .addLoreLines(
@@ -46,7 +46,7 @@ object Boosters {
EffectChains.compile(it, "Effect Chains") EffectChains.compile(it, "Effect Chains")
} }
for (booster in values()) { for (booster in values()) {
removeSet(booster) removeBooster(booster)
} }
for (config in plugin.boostersYml.getSubsections("boosters")) { for (config in plugin.boostersYml.getSubsections("boosters")) {
Booster(plugin, config) Booster(plugin, config)
@@ -70,7 +70,7 @@ object Boosters {
* @param booster The [Booster] to remove. * @param booster The [Booster] to remove.
*/ */
@JvmStatic @JvmStatic
fun removeSet(booster: Booster) { fun removeBooster(booster: Booster) {
BY_ID.remove(booster.id) BY_ID.remove(booster.id)
} }
} }
@@ -1,8 +1,7 @@
package com.willfp.boosters.commands package com.willfp.boosters.commands
import com.willfp.boosters.boosters.Boosters import com.willfp.boosters.boosters.Boosters
import com.willfp.boosters.getAmountOfBooster import com.willfp.boosters.incrementBoosters
import com.willfp.boosters.setAmountOfBooster
import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.Subcommand import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils import com.willfp.eco.util.StringUtils
@@ -42,26 +41,22 @@ class CommandGive(plugin: EcoPlugin) :
amount = args[2].toIntOrNull() ?: amount amount = args[2].toIntOrNull() ?: amount
} }
this.plugin.scheduler.runAsync {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
val player = Bukkit.getOfflinePlayer(args[0]) val player = Bukkit.getOfflinePlayer(args[0])
if (!player.hasPlayedBefore()) { if (!player.hasPlayedBefore()) {
sender.sendMessage(plugin.langYml.getMessage("invalid-player")) sender.sendMessage(plugin.langYml.getMessage("invalid-player"))
return@runAsync return
} }
this.plugin.scheduler.run { player.incrementBoosters(booster, amount)
player.setAmountOfBooster(booster, player.getAmountOfBooster(booster) + amount)
}
sender.sendMessage( sender.sendMessage(
plugin.langYml.getMessage("gave-booster", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) plugin.langYml.getMessage("gave-booster", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS)
.replace("%player%", player.name ?: return@runAsync) .replace("%player%", player.name ?: return)
.replace("%booster%", booster.name) .replace("%booster%", booster.name)
.replace("%amount%", amount.toString()) .replace("%amount%", amount.toString())
) )
} }
}
override fun tabComplete(sender: CommandSender, args: List<String>): List<String> { override fun tabComplete(sender: CommandSender, args: List<String>): List<String> {
val completions = mutableListOf<String>() val completions = mutableListOf<String>()
@@ -67,13 +67,6 @@ object BoosterGUI {
slot( slot(
{ player, _ -> booster.getGuiItem(player) } { 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)) onLeftClick(makeHandler(booster, plugin))
} }
) )