Skip to content

Commit

Permalink
feat(plugin): fix heads in 1.20.5+ and update nbt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iGabyTM committed Jul 28, 2024
1 parent 73776b0 commit 5259a23
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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 {
setString(Constant.NBT.RECEIVER_UUID, uuid.toString())
}
}

fun NBTCompound.getReceiverUUID(): UUID? {
fun ReadableNBT.getReceiverUUID(): UUID? {
return if (ServerVersion.HAS_UUID_NBT_COMPOUND) {
getUUID(Constant.NBT.RECEIVER_UUID)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String, String> {
private fun ReadableNBT.getArgs(): MutableMap<String, String> {
return keys.associateWith { getString(it) }.toMutableMap()
}

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PatternType>, Keyed
// but paper is still using an enum in 1.21 🤷‍♂️
/*create("PatternTypes") { yaml ->
yaml["list"] = PatternType.values().map { it.name }
}
}*/

create(
"Sounds",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5259a23

Please sign in to comment.