From 2955782eea66b9a19b5ce90430076966683b6c7c Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Sun, 6 Aug 2023 10:23:54 +0300 Subject: [PATCH] feat: add MM tag Used for placeholders that contains legacy format / MM tags --- .../arcanevouchers/functions/Strings.kt | 17 +++++--- .../arcanevouchers/util/PapiMiniMessageTag.kt | 43 +++++++++++++++++++ .../arcanevouchers/voucher/VoucherManager.kt | 3 +- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/util/PapiMiniMessageTag.kt diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt index e7bcd9f..40744c6 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt @@ -6,6 +6,8 @@ import me.clip.placeholderapi.PlaceholderAPI import me.gabytm.minecraft.arcanevouchers.Constant import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.TextDecoration +import net.kyori.adventure.text.minimessage.ParsingException +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver import net.md_5.bungee.api.ChatColor import org.apache.commons.lang.StringUtils import org.bukkit.Bukkit @@ -31,11 +33,16 @@ fun String.replace(placeholders: Array, values: Array): String { return StringUtils.replaceEach(this, placeholders, values) } -fun String.mini(removeItalic: Boolean = false): Component { - return if (removeItalic) { - EMPTY_COMPONENT_WITHOUT_ITALIC.append(Constant.MINI.deserialize(this)) - } else { - Constant.MINI.deserialize(this) +fun String.mini(removeItalic: Boolean = false, vararg tagResolver: TagResolver): Component { + return try { + if (removeItalic) { + EMPTY_COMPONENT_WITHOUT_ITALIC.append(Constant.MINI.deserialize(this, *tagResolver)) + } else { + Constant.MINI.deserialize(this, *tagResolver) + } + } catch (e: ParsingException) { + exception("Could not parse '$this'", e) + return Component.text(this) } } diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/util/PapiMiniMessageTag.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/util/PapiMiniMessageTag.kt new file mode 100644 index 0000000..7d69de5 --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/util/PapiMiniMessageTag.kt @@ -0,0 +1,43 @@ +package me.gabytm.minecraft.arcanevouchers.util + +import me.gabytm.minecraft.arcanevouchers.functions.papi +import net.kyori.adventure.text.Component +import net.kyori.adventure.text.minimessage.Context +import net.kyori.adventure.text.minimessage.tag.Tag +import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer +import org.bukkit.entity.Player + +class PapiMiniMessageTag(private val player: Player) : TagResolver { + + override fun resolve(name: String, arguments: ArgumentQueue, ctx: Context): Tag? { + val placeholder = arguments.popOr("Placeholder argument not found").value() + val parsed = "%$placeholder%".papi(player) + + // If the placeholder returns itself it means it is invalid + if (parsed == "%$placeholder%") { + return null + } + + if (parsed.isEmpty()) { + return Tag.inserting(Component.empty()) + } + + if (LEGACY_REGEX.containsMatchIn(parsed)) { + return Tag.inserting(LegacyComponentSerializer.legacyAmpersand().deserialize(parsed.replace('§', '&'))) + } + + return Tag.inserting(ctx.deserialize(parsed)); + } + + override fun has(name: String): Boolean = name == NAME + + companion object { + + private const val NAME: String = "papi" + private val LEGACY_REGEX: Regex = Regex("[&|§][0-9A-FK-OR]", RegexOption.IGNORE_CASE) + + } + +} \ 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 732fa47..1ef197e 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 me.gabytm.minecraft.arcanevouchers.util.PapiMiniMessageTag import net.kyori.adventure.text.Component import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack @@ -77,7 +78,7 @@ class VoucherManager(private val plugin: ArcaneVouchers) { val transformer: (String) -> Component = { it.replace(arguments, values) // Replace arguments .papi(player) // Set PAPI placeholders - .mini(true) // Parse mini tags + .mini(true, PapiMiniMessageTag(player)) // Parse mini tags } if (voucher.itemName.isNotBlank()) {