From a74728c7a8b8dd94fb0731cd57d6a893af5c4805 Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Mon, 25 Nov 2024 00:26:15 +0200 Subject: [PATCH] fix(plugin): ItemStack.clone() not working well on < 1.13 --- .../gabytm/minecraft/arcanevouchers/functions/Players.kt | 4 ++-- .../arcanevouchers/listeners/VoucherUseListener.kt | 2 +- .../gabytm/minecraft/arcanevouchers/voucher/Voucher.kt | 6 +++--- .../minecraft/arcanevouchers/voucher/VoucherManager.kt | 9 ++++++++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Players.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Players.kt index 1e87a71..4a4cad2 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Players.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Players.kt @@ -10,7 +10,7 @@ import org.bukkit.inventory.ItemStack * @return the [ItemStack] that the player is holding */ @Suppress("DEPRECATION") -fun Player.item(): ItemStack { +fun Player.itemInHand(): ItemStack { return if (ServerVersion.HAS_OFF_HAND) { this.inventory.itemInMainHand } else { @@ -23,7 +23,7 @@ fun Player.item(): ItemStack { * @param item item to set */ @Suppress("DEPRECATION") -fun Player.item(item: ItemStack?) { +fun Player.itemInHand(item: ItemStack?) { if (ServerVersion.HAS_OFF_HAND) { this.inventory.setItemInMainHand(item) } else { 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 382d332..18dd9c9 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 @@ -104,7 +104,7 @@ class VoucherUseListener(private val plugin: ArcaneVouchers) : Listener { return } - val item = this.player.item() + val item = this.player.itemInHand() if (item.type == Material.AIR) { return 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 daeb392..1bc33ca 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 @@ -8,7 +8,7 @@ import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager import me.gabytm.minecraft.arcanevouchers.functions.add import me.gabytm.minecraft.arcanevouchers.functions.audience import me.gabytm.minecraft.arcanevouchers.functions.debug -import me.gabytm.minecraft.arcanevouchers.functions.item +import me.gabytm.minecraft.arcanevouchers.functions.itemInHand import me.gabytm.minecraft.arcanevouchers.items.ItemCreator import me.gabytm.minecraft.arcanevouchers.limit.LimitType import me.gabytm.minecraft.arcanevouchers.voucher.requirements.ArcaneRequirementProcessor @@ -97,7 +97,7 @@ class Voucher private constructor( private fun removeVouchers(player: Player, voucher: ItemStack, amount: Int) { // Remove the item completely if it has the same amount as the amount of redeemed vouchers if (voucher.amount == amount) { - player.item(ItemStack(Material.AIR)) + player.itemInHand(ItemStack(Material.AIR)) return } @@ -105,7 +105,7 @@ class Voucher private constructor( 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) + player.itemInHand(voucher) } fun redeem( 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 076eeaf..f17bd2b 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 @@ -4,6 +4,7 @@ import de.tr7zw.nbtapi.NBT import dev.triumphteam.gui.builder.item.ItemBuilder import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers import me.gabytm.minecraft.arcanevouchers.Constant +import me.gabytm.minecraft.arcanevouchers.ServerVersion import me.gabytm.minecraft.arcanevouchers.cooldown.CooldownManager import me.gabytm.minecraft.arcanevouchers.functions.* import me.gabytm.minecraft.arcanevouchers.limit.LimitManager @@ -59,7 +60,13 @@ class VoucherManager(private val plugin: ArcaneVouchers) { val argsMap = args.toArgsMap() argsMap[Lang.Placeholder.RECEIVER] = player.name - val voucherItem = voucher.item.clone() + val voucherItem = if (ServerVersion.IS_LEGACY) { + // Gabi: on 1.8-1.12.x ItemStack#clone doesn't create a PROPER clone. Setting the NBT updates the original item as well. + // This alternative was suggested by tr7ze, author of NBT API + NBT.itemStackFromNBT(NBT.itemStackToNBT(voucher.item)) ?: throw NullPointerException("Could not transform voucher item to NBT and back to ItemStack") + } else { + voucher.item.clone() + } // Set the arguments and player's name inside the item NBT.modify(voucherItem) { itemNbt ->