From af466b9873d6faf1463610fda4efc2a6d3ecddb4 Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Mon, 5 Jun 2023 19:32:08 +0300 Subject: [PATCH] feat(plugin): parse placeholders in voucher name & lore (close #20) --- .../arcanevouchers/functions/Components.kt | 2 +- .../arcanevouchers/voucher/VoucherManager.kt | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Components.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Components.kt index 39adba2..90dbdab 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Components.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Components.kt @@ -4,7 +4,7 @@ import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.TextDecoration /** - * Remove the [italic][TextDecoration.ITALIC] of a component, use for [item][org.bukkit.inventory.ItemStack] name and lore + * Remove the [italic][TextDecoration.ITALIC] of a component, used for [item][org.bukkit.inventory.ItemStack] name and lore * @return a [Component] without [italic][TextDecoration.ITALIC] */ fun Component.removeItalic(): Component = decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE) \ No newline at end of file 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 42f4d5f..732fa47 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 @@ -8,6 +8,7 @@ import me.gabytm.minecraft.arcanevouchers.cooldown.CooldownManager import me.gabytm.minecraft.arcanevouchers.functions.* import me.gabytm.minecraft.arcanevouchers.limit.LimitManager import me.gabytm.minecraft.arcanevouchers.message.Lang +import net.kyori.adventure.text.Component import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.* @@ -67,27 +68,27 @@ class VoucherManager(private val plugin: ArcaneVouchers) { compound.setReceiverUUID(player.uniqueId) // ----- - if (argsMap.isEmpty()) { - val item = nbt.item - item.amount = amount - return item; - } - - val item = ItemBuilder.from(nbt.item).amount(amount) + val itemBuilder = ItemBuilder.from(nbt.item).amount(amount) // Replace the arguments on item name and lore - val placeholders = argsMap.keys.toTypedArray() + val arguments = argsMap.keys.toTypedArray() val values = argsMap.values.toTypedArray() + val transformer: (String) -> Component = { + it.replace(arguments, values) // Replace arguments + .papi(player) // Set PAPI placeholders + .mini(true) // Parse mini tags + } + if (voucher.itemName.isNotBlank()) { - item.name(voucher.itemName.replace(placeholders, values).mini(true)) + itemBuilder.name(transformer(voucher.itemName)) } if (voucher.itemLore.isNotEmpty()) { - item.lore(voucher.itemLore.map { it.replace(placeholders, values).mini(true) }) + itemBuilder.lore(voucher.itemLore.map(transformer)) } - return item.build() + return itemBuilder.build() } fun giveVoucher(player: Player, voucher: Voucher, amount: Int, args: Array) {