Skip to content

Commit

Permalink
feat(plugin): parse placeholders in voucher name & lore (close #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
iGabyTM committed Jun 5, 2023
1 parent 897f9b9 commit af466b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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<String>) {
Expand Down

0 comments on commit af466b9

Please sign in to comment.