Skip to content

Commit

Permalink
fix(plugin): ItemStack.clone() not working well on < 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
iGabyTM committed Nov 24, 2024
1 parent 16a520e commit a74728c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,15 +97,15 @@ 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
}

// Otherwise, subtract one
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit a74728c

Please sign in to comment.