Skip to content

Commit

Permalink
[plugin] fix for vouchers not being removed
Browse files Browse the repository at this point in the history
On some versions, if confirmation is enabled, vouchers were not removed
  • Loading branch information
iGabyTM committed Jul 2, 2022
1 parent 8663e18 commit a790eeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ fun Player.item(): ItemStack {
}
}

/**
* Set a [Player]'s item in hand using the right method for each game version
* @param item item to set
*/
@Suppress("DEPRECATION")
fun Player.item(item: ItemStack?) {
if (ServerVersion.HAS_OFF_HAND) {
this.inventory.setItemInMainHand(item)
} else {
this.inventory.setItemInHand(item)
}
}

/**
* Add items to a player inventory and drop on the ground the leftovers
* @param items items to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package me.gabytm.minecraft.arcanevouchers.voucher
import de.tr7zw.nbtapi.NBTItem
import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers
import me.gabytm.minecraft.arcanevouchers.Constant.NBT
import me.gabytm.minecraft.arcanevouchers.ServerVersion
import me.gabytm.minecraft.arcanevouchers.actions.ArcaneAction
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.items.ItemCreator
import me.gabytm.minecraft.arcanevouchers.limit.LimitType
import me.gabytm.minecraft.arcanevouchers.voucher.settings.VoucherSettings
Expand Down Expand Up @@ -91,19 +91,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) {
// On 1.11.2+ we can just set the item amount to 0, and it will be removed
if (ServerVersion.ITEMS_WITH_ZERO_AMOUNT_ARE_REMOVED) {
voucher.amount = 0
} else {
// But on pre 1.11.2 that doesn't work
player.setItemInHand(null)
}

player.item(ItemStack(Material.AIR))
return
}

// Otherwise, subtract one
voucher.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)
}

fun redeem(player: Player, voucher: ItemStack, args: MutableMap<String, String>, plugin: ArcaneVouchers, isBulk: Boolean) {
Expand Down

0 comments on commit a790eeb

Please sign in to comment.