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

View File

@@ -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)

View File

@@ -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<String, PersistentDataKey<Int>>()
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<Int>
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<String> = 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(

View File

@@ -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)
}
}

View File

@@ -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<String>): List<String> {

View File

@@ -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))
}
)