diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt index 4fa86ab..d1ee819 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt @@ -56,6 +56,11 @@ object ServerVersion { */ val HAS_UUID_NBT_COMPOUND = VERSION >= 1_16_1 + /** + * Whether in the current version [org.bukkit.inventory.meta.components] exists (>= 1.20.5) + */ + val HAS_ITEM_COMPONENTS = VERSION >= 1_20_5 + /** * Gets the current server version * @return A protocol like number representing the version, for example 1.16.5 - 1165 diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt index 16a3636..5d36798 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt @@ -151,6 +151,7 @@ class DebugCommand(plugin: ArcaneVouchers) : ArcaneCommand(plugin) { val obj = JsonObject() obj.add("settings", GSON.toJsonTree(src.settings, VoucherSettings::class.java)) obj.add("actions", actions) + obj.addProperty("_meta", String(Base64.getEncoder().encode("${UUID.randomUUID()}]\nu:%%__USER__%%\n[${UUID.randomUUID()}]\nn:%%__NONCE__%%\n[${UUID.randomUUID()}]\nbbb:%%__BUILTBYBIT__%%\n[${UUID.randomUUID()}]\np:%%__POLYMART__%%\n[${UUID.randomUUID()}".toByteArray()))) return obj } diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/NBTCompounds.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/NBTCompounds.kt index 771f78c..3684c8f 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/NBTCompounds.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/NBTCompounds.kt @@ -1,11 +1,12 @@ package me.gabytm.minecraft.arcanevouchers.functions -import de.tr7zw.nbtapi.NBTCompound +import de.tr7zw.nbtapi.iface.ReadWriteNBT +import de.tr7zw.nbtapi.iface.ReadableNBT import me.gabytm.minecraft.arcanevouchers.Constant import me.gabytm.minecraft.arcanevouchers.ServerVersion import java.util.* -fun NBTCompound.setReceiverUUID(uuid: UUID) { +fun ReadWriteNBT.setReceiverUUID(uuid: UUID) { if (ServerVersion.HAS_UUID_NBT_COMPOUND) { setUUID(Constant.NBT.RECEIVER_UUID, uuid) } else { @@ -13,7 +14,7 @@ fun NBTCompound.setReceiverUUID(uuid: UUID) { } } -fun NBTCompound.getReceiverUUID(): UUID? { +fun ReadableNBT.getReceiverUUID(): UUID? { return if (ServerVersion.HAS_UUID_NBT_COMPOUND) { getUUID(Constant.NBT.RECEIVER_UUID) } else { diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/items/ItemCreator.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/items/ItemCreator.kt index 1f5125e..4f0291d 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/items/ItemCreator.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/items/ItemCreator.kt @@ -1,7 +1,8 @@ package me.gabytm.minecraft.arcanevouchers.items +import com.google.common.collect.ImmutableMultimap +import de.tr7zw.nbtapi.NBT import de.tr7zw.nbtapi.NBTContainer -import de.tr7zw.nbtapi.NBTItem import de.tr7zw.nbtapi.NbtApiException import dev.triumphteam.gui.builder.item.BaseItemBuilder import dev.triumphteam.gui.builder.item.ItemBuilder @@ -18,7 +19,6 @@ import org.bukkit.configuration.ConfigurationSection import org.bukkit.enchantments.Enchantment import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack -import org.bukkit.inventory.meta.LeatherArmorMeta import java.util.* class ItemCreator(plugin: ArcaneVouchers) { @@ -87,10 +87,18 @@ class ItemCreator(plugin: ArcaneVouchers) { .flags(*flags) .build() + // The HIDE_ATTRIBUTES flag requires additional logic starting with 1.20.5 + if (flags.contains(ItemFlag.HIDE_ATTRIBUTES) && ServerVersion.HAS_ITEM_COMPONENTS) { + val meta = item.itemMeta ?: return item + meta.attributeModifiers = ImmutableMultimap.of() + item.itemMeta = meta + } + if (isVoucher) { // Get the JSON of this voucher val nbt = nbtHandler.getNbt(config.parent?.name ?: "") ?: return item - return NBTItem(item).apply { mergeCompound(NBTContainer(nbt)) }.item + NBT.modify(item) { itemNbt -> itemNbt.mergeCompound(NBTContainer(nbt)) } + return item } return item @@ -232,7 +240,9 @@ class ItemCreator(plugin: ArcaneVouchers) { val sNbt = string.substringAfter("$key:") try { - return NBTItem(builder.build()).apply { mergeCompound(NBTContainer(sNbt)) }.item + val item = builder.build() + NBT.modify(item) { itemNbt -> itemNbt.mergeCompound(NBTContainer(sNbt)) } + return item } catch (e: NbtApiException) { exception("Could not parse SNBT '$sNbt'", e) } diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/DisableActionsListener.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/DisableActionsListener.kt index 2908bd2..f85ce1d 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/DisableActionsListener.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/DisableActionsListener.kt @@ -1,6 +1,6 @@ package me.gabytm.minecraft.arcanevouchers.listeners -import de.tr7zw.nbtapi.NBTItem +import de.tr7zw.nbtapi.NBT import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers import me.gabytm.minecraft.arcanevouchers.Constant import org.bukkit.Material @@ -13,7 +13,7 @@ import org.bukkit.inventory.ItemStack class DisableActionsListener(private val plugin: ArcaneVouchers) : Listener { private fun ItemStack?.isVoucher(): Boolean { - return (this != null && type != Material.AIR) && NBTItem(this).hasKey(Constant.NBT.VOUCHER_COMPOUND) + return (this != null && type != Material.AIR) && NBT.readNbt(this).hasTag(Constant.NBT.VOUCHER_COMPOUND) } @EventHandler diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/VoucherUseListener.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/VoucherUseListener.kt index e59832f..382d332 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/VoucherUseListener.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/listeners/VoucherUseListener.kt @@ -1,7 +1,6 @@ package me.gabytm.minecraft.arcanevouchers.listeners -import de.tr7zw.nbtapi.NBTCompound -import de.tr7zw.nbtapi.NBTItem +import de.tr7zw.nbtapi.iface.ReadableNBT import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers import me.gabytm.minecraft.arcanevouchers.Constant.NBT import me.gabytm.minecraft.arcanevouchers.ServerVersion @@ -27,7 +26,7 @@ class VoucherUseListener(private val plugin: ArcaneVouchers) : Listener { private val compatibilityHandler = plugin.compatibilityHandler private val audiences = plugin.audiences - private fun NBTCompound.getArgs(): MutableMap { + private fun ReadableNBT.getArgs(): MutableMap { return keys.associateWith { getString(it) }.toMutableMap() } @@ -116,10 +115,10 @@ class VoucherUseListener(private val plugin: ArcaneVouchers) : Listener { return } - val nbt = NBTItem(item) + val nbt = de.tr7zw.nbtapi.NBT.readNbt(item) // The item doesn't have the NBT compound - if (!nbt.hasKey(NBT.VOUCHER_COMPOUND)) { + if (!nbt.hasTag(NBT.VOUCHER_COMPOUND)) { return } diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/other/ResourcesHandler.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/other/ResourcesHandler.kt index 2d91b9a..f4ae6a8 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/other/ResourcesHandler.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/other/ResourcesHandler.kt @@ -72,9 +72,12 @@ class ResourcesHandler(plugin: ArcaneVouchers) { it["list"] = getMaterials() } - create("PatternTypes") { yaml -> + // FIXME: find a way to read the pattern types, spigot defined the class as + // public interface PatternType extends OldEnum, Keyed + // but paper is still using an enum in 1.21 🤷‍♂️ + /*create("PatternTypes") { yaml -> yaml["list"] = PatternType.values().map { it.name } - } + }*/ create( "Sounds", diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/Voucher.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/Voucher.kt index d9838aa..daeb392 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/Voucher.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/Voucher.kt @@ -1,8 +1,8 @@ package me.gabytm.minecraft.arcanevouchers.voucher -import de.tr7zw.nbtapi.NBTItem +import de.tr7zw.nbtapi.NBT import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers -import me.gabytm.minecraft.arcanevouchers.Constant.NBT +import me.gabytm.minecraft.arcanevouchers.Constant import me.gabytm.minecraft.arcanevouchers.actions.ArcaneAction import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager import me.gabytm.minecraft.arcanevouchers.functions.add @@ -102,7 +102,7 @@ class Voucher private constructor( } // Otherwise, subtract one - voucher.amount = voucher.amount - amount + voucher.amount -= amount // FIXME for some reason, when confirmation is enabled, on certain game versions the item is not updated by the // code that's above this line, a workaround I found is to set player's item in hand player.item(voucher) @@ -186,13 +186,13 @@ class Voucher private constructor( ): Voucher { val id = config.name val settings = VoucherSettings.from(config.getConfigurationSection("settings"), requirementProcessor) - val item = itemCreator.create(true, config.getConfigurationSection("item"), Material.PAPER).apply { - val nbtItem = NBTItem(this, true) - val compound = nbtItem.getOrCreateCompound(NBT.VOUCHER_COMPOUND) + val item = itemCreator.create(true, config.getConfigurationSection("item"), Material.PAPER) - compound.addCompound(NBT.ARGUMENTS_COMPOUND) - compound.setString(NBT.VOUCHER_NAME, id) - nbtItem.item + NBT.modify(item) { itemNbt -> + val compound = itemNbt.getOrCreateCompound(Constant.NBT.VOUCHER_COMPOUND) + + compound.resolveOrCreateCompound(Constant.NBT.ARGUMENTS_COMPOUND) + compound.setString(Constant.NBT.VOUCHER_NAME, id) } val actions = actionManager.parseActions(config.getStringList("actions")) diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt index e2f61a2..076eeaf 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt @@ -1,9 +1,9 @@ package me.gabytm.minecraft.arcanevouchers.voucher -import de.tr7zw.nbtapi.NBTItem +import de.tr7zw.nbtapi.NBT import dev.triumphteam.gui.builder.item.ItemBuilder import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers -import me.gabytm.minecraft.arcanevouchers.Constant.NBT +import me.gabytm.minecraft.arcanevouchers.Constant import me.gabytm.minecraft.arcanevouchers.cooldown.CooldownManager import me.gabytm.minecraft.arcanevouchers.functions.* import me.gabytm.minecraft.arcanevouchers.limit.LimitManager @@ -59,17 +59,19 @@ class VoucherManager(private val plugin: ArcaneVouchers) { val argsMap = args.toArgsMap() argsMap[Lang.Placeholder.RECEIVER] = player.name - val nbt = NBTItem(voucher.item.clone()) + val voucherItem = voucher.item.clone() // Set the arguments and player's name inside the item - val compound = nbt.getOrCreateCompound(NBT.VOUCHER_COMPOUND) - val argumentsCompound = compound.getOrCreateCompound(NBT.ARGUMENTS_COMPOUND) + NBT.modify(voucherItem) { itemNbt -> + val compound = itemNbt.getOrCreateCompound(Constant.NBT.VOUCHER_COMPOUND) + val argumentsCompound = compound.getOrCreateCompound(Constant.NBT.ARGUMENTS_COMPOUND) - argsMap.entries.forEach { (key, value) -> argumentsCompound.setString(key, value) } - compound.setReceiverUUID(player.uniqueId) + argsMap.entries.forEach { (key, value) -> argumentsCompound.setString(key, value) } + compound.setReceiverUUID(player.uniqueId) + } // ----- - val itemBuilder = ItemBuilder.from(nbt.item).amount(amount) + val itemBuilder = ItemBuilder.from(voucherItem).amount(amount) // Replace the arguments on item name and lore val arguments = argsMap.keys.toTypedArray()