From c9e41aceb6314c978bf2371f3379a7f64121acb2 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 20 Oct 2025 11:25:43 +0200 Subject: [PATCH] some refractor --- README.md | 4 +- .../cuanvil/listener/PrepareAnvilListener.kt | 25 ++++++----- .../alexcrea/cuanvil/util/AnvilColorUtil.kt | 42 +++++++------------ .../cuanvil/util/AnvilLoreEditUtil.kt | 17 +++----- .../alexcrea/cuanvil/util/MiniMessageUtil.kt | 18 ++++++++ src/main/resources/plugin.yml | 3 ++ 6 files changed, 56 insertions(+), 53 deletions(-) create mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt diff --git a/README.md b/README.md index 492d6f6..cc868c5 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ the plugin can be downloaded on - Display XP cost instead of "too expensive" when above level 40. (see below for more information) - Can handle some custom enchantment plugins (see below for more information) - Gui to configure the plugin in game. -- Support of color code and hexadecimal color +- Support use of color code, hexadecimal color and minimessage for color/decoration - (Experimental) Folia support (gui do not work) --- ### Permissions: @@ -44,7 +44,7 @@ ca.config.edit: Allow administrator to edit the plugin's config in game # Permissions related to use of color and minimessage ca.color.code: Allow player to use color code on rename if enabled (toggleable) ca.color.hex: Allow player to use hexadecimal color on rename if enabled (toggleable) -ca.color.minimessage: Allow player to use minimessage formating on rename if enabled (toggleable) +ca.rename.minimessage: Allow player to use minimessage formating on rename if enabled (toggleable) (only legacy compatible at the time) # Permissions related to edition of the lore ca.lore_edit.book: Allow player to edit lore via book and quil if enabled (toggleable) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index 5ba1dfa..17d5d0d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -186,24 +186,23 @@ class PrepareAnvilListener : Listener { private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int { // Can be null - var inventoryName = ChatColor.stripColor(inventory.renameText) + var renameText = ChatColor.stripColor(inventory.renameText) var sumCost = 0 var useColor = false - if (ConfigOptions.renameColorPossible && inventoryName != null) { - val resultString = StringBuilder(inventoryName) - - useColor = AnvilColorUtil.handleColor( - resultString, player, + if (ConfigOptions.renameColorPossible && renameText != null) { + val component = AnvilColorUtil.handleColor( + renameText, player, ConfigOptions.permissionNeededForColor, ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage, AnvilColorUtil.ColorUseType.RENAME ) - if (useColor) { - inventoryName = resultString.toString() + if (component != null) { + renameText = MiniMessageUtil.legacy_mm.serialize(component) sumCost += ConfigOptions.useOfColorCost + useColor = true } } @@ -214,8 +213,8 @@ class PrepareAnvilListener : Listener { else if (useColor) it.displayName else ChatColor.stripColor(it.displayName) - if (!displayName.contentEquals(inventoryName)) { - it.setDisplayName(inventoryName) + if (!displayName.contentEquals(renameText)) { + it.setDisplayName(renameText) resultItem.itemMeta = it sumCost += ConfigOptions.itemRenameCost @@ -233,10 +232,10 @@ class PrepareAnvilListener : Listener { ) { val newEnchants = first.findEnchantments() .combineWith(second.findEnchantments(), first, player) - var hasChanged = !isIdentical(first.findEnchantments(), newEnchants); + var hasChanged = !isIdentical(first.findEnchantments(), newEnchants) val resultItem = first.clone() - var anvilCost = 0; + var anvilCost = 0 if(hasChanged){ resultItem.setEnchantmentsUnsafe(newEnchants) // Calculate enchantment cost @@ -248,7 +247,7 @@ class PrepareAnvilListener : Listener { // we only need to be concerned with repair when neither item is a book val repaired = resultItem.repairFrom(first, second) anvilCost += if (repaired) ConfigOptions.itemRepairCost else 0 - hasChanged = hasChanged || repaired; + hasChanged = hasChanged || repaired } // Test/stop if nothing changed. diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilColorUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilColorUtil.kt index 91fc31d..2629d1f 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilColorUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilColorUtil.kt @@ -1,9 +1,6 @@ package xyz.alexcrea.cuanvil.util -import net.kyori.adventure.text.minimessage.MiniMessage -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver -import net.kyori.adventure.text.minimessage.tag.standard.StandardTags -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer +import net.kyori.adventure.text.Component import org.bukkit.permissions.Permissible import java.util.regex.Matcher import java.util.regex.Pattern @@ -12,30 +9,20 @@ object AnvilColorUtil { private val HEX_PATTERN: Pattern = Pattern.compile("#[A-Fa-f0-9]{6}") // pattern to find hexadecimal string private val TRANSFORMED_HEX_PATTERN = Pattern.compile("§x(§[0-9a-fA-F]){6}") // pattern to find minecraft hex string - //TODO use only things compatible with legacy formating - private val mm = MiniMessage.builder() - .tags(TagResolver.resolver( - StandardTags.color(), - StandardTags.decorations())) - .build() - private val legacymm = LegacyComponentSerializer.legacySection() - /** - * //TODO rework on 2.x.x use (return) component and not legacy string - * * Color a stringbuilder object depending on allowed color type and player permissions on color use type * @return if the stringbuilder was changed and color applied or if minimessage formating was applied */ fun handleColor( - textToColor: StringBuilder, + textToColorText: String, player: Permissible, usePermission: Boolean, allowColorCode: Boolean, allowHexadecimalColor: Boolean, allowMinimessage: Boolean, useType: ColorUseType - ): Boolean { - if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return false + ): Component? { + if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return null val canUseColorCode = allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission( @@ -50,8 +37,9 @@ object AnvilColorUtil { useType.minimessagePerm )) - if ((!canUseColorCode) && (!canUseHexColor)) return false + if (!canUseColorCode && !canUseHexColor && !canUseMinimessage) return null + val textToColor = StringBuilder(textToColorText) var useColor = false // Handle color code if (canUseColorCode) { // maybe should use LegacyComponentSerializer ? @@ -67,23 +55,23 @@ object AnvilColorUtil { if (nbReplacement > 0) useColor = true } + val previousStr = textToColor.toString() + var result: Component = MiniMessageUtil.legacy_mm.deserialize(previousStr) if(canUseMinimessage) { - val previousStr = textToColor.toString() - // we dance with formats here - val fromLegacy = legacymm.deserialize(previousStr) - val toMinimessage = mm.serialize(fromLegacy) + val toMinimessage = MiniMessageUtil.mm.serialize(result) val hackySolution = toMinimessage.replace("\\<", "<") - val fromMinimessage = mm.deserialize(hackySolution) - val toLegacy = legacymm.serialize(fromMinimessage) + val fromMinimessage = MiniMessageUtil.mm.deserialize(hackySolution) + val toLegacy = MiniMessageUtil.legacy_mm.serialize(fromMinimessage) if(previousStr != toLegacy){ useColor = true - textToColor.replace(0, textToColor.length, toLegacy) + result = fromMinimessage } } - return useColor + return if(useColor) result + else null } /** @@ -215,7 +203,7 @@ object AnvilColorUtil { val hexColorPerm: String?, val minimessagePerm: String? ) { - RENAME("ca.color.code", "ca.color.hex", "ca.color.minimessage"), + RENAME("ca.color.code", "ca.color.hex", "ca.rename.minimessage"), LORE_EDIT(null, null, null) } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt index 598a7ce..0f0d69d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt @@ -249,27 +249,22 @@ object AnvilLoreEditUtil { // Handle color and minimessage of each lines var hasUsedColor = false for ((index, line) in lines.withIndex()) { - val coloredLine = StringBuilder(line) - - val lineUsedColor = AnvilColorUtil.handleColor( - coloredLine, + val component = AnvilColorUtil.handleColor( + line, player, false, canUseColorCode, canUseHex, minimessage, AnvilColorUtil.ColorUseType.LORE_EDIT ) - if (lineUsedColor) { + if (component != null) { hasUsedColor = true - lines[index] = coloredLine.toString() + lines[index] = MiniMessageUtil.legacy_mm.serialize(component) } } - return if (hasUsedColor) { - colorCost - } else { - 0 - } + return if (hasUsedColor) colorCost + else 0 } fun uncolorLines(player: Permissible, lines: ArrayList, editType: LoreEditType): Int { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt new file mode 100644 index 0000000..10500e8 --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt @@ -0,0 +1,18 @@ +package xyz.alexcrea.cuanvil.util + +import net.kyori.adventure.text.minimessage.MiniMessage +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer + +object MiniMessageUtil { + + val mm = MiniMessage.builder() + .tags(TagResolver.resolver( + StandardTags.color(), + StandardTags.decorations())) + .build() + + val legacy_mm = LegacyComponentSerializer.legacySection() + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0e58169..9449f8b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -46,6 +46,9 @@ permissions: ca.color.hex: default: op description: Allow player to use hexadecimal color if enabled (toggleable) + ca.rename.minimessage: + default: op + description: Allow player to use minimessage formating on rename if enabled (toggleable) (only legacy compatible at the time) # lore edit permissions ca.lore_edit.book: default: op