Skip to content

Commit

Permalink
feat: add <papi> MM tag
Browse files Browse the repository at this point in the history
Used for placeholders that contains legacy format / MM tags
  • Loading branch information
iGabyTM committed Aug 6, 2023
1 parent 8f54f39 commit 2955782
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,11 +33,16 @@ fun String.replace(placeholders: Array<String>, values: Array<String>): 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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)

}

}
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 me.gabytm.minecraft.arcanevouchers.util.PapiMiniMessageTag
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 2955782

Please sign in to comment.