add minimessage for color and decoration only

This commit is contained in:
alexcrea 2025-10-19 16:45:45 +02:00
parent c63482c9df
commit 9912da869c
Signed by: alexcrea
GPG key ID: E346CD16413450E3
12 changed files with 91 additions and 14 deletions

View file

@ -41,9 +41,10 @@ ca.config.edit: Allow administrator to edit the plugin's config in game
# Bellow permissions also require some config change to allow usage of features # Bellow permissions also require some config change to allow usage of features
# usage of these permission is toggleable in basic config gui or config.yml # usage of these permission is toggleable in basic config gui or config.yml
# Permissions related to use of color # Permissions related to use of color and minimessage
ca.color.code: Allow player to use color code if enabled (toggleable) ca.color.code: Allow player to use color code on rename if enabled (toggleable)
ca.color.hex: Allow player to use hexadecimal color 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)
# Permissions related to edition of the lore # Permissions related to edition of the lore
ca.lore_edit.book: Allow player to edit lore via book and quil if enabled (toggleable) ca.lore_edit.book: Allow player to edit lore via book and quil if enabled (toggleable)

View file

@ -35,6 +35,9 @@ dependencies {
// Spigot api // Spigot api
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT") compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")
// minimessage
implementation("net.kyori:adventure-text-minimessage:4.25.0")
// Gui library // Gui library
val inventoryFramework = "xyz.alexcrea.cuanvil.inventoryframework:IF-CustomAnvil:0.10.18.2" val inventoryFramework = "xyz.alexcrea.cuanvil.inventoryframework:IF-CustomAnvil:0.10.18.2"
implementation(inventoryFramework) implementation(inventoryFramework)

View file

@ -59,8 +59,10 @@ sacrifice_illegal_enchant_cost: 1
# #
# Color code are prefixed by "&" and hexadecimal color by "#". # Color code are prefixed by "&" and hexadecimal color by "#".
# Color code will not be applied if it colors nothing. "&&" can be used to write "&". # Color code will not be applied if it colors nothing. "&&" can be used to write "&".
# For minimessage search for minimessage formating https://docs.papermc.io/adventure/minimessage/format/
allow_color_code: false allow_color_code: false
allow_hexadecimal_color: false allow_hexadecimal_color: false
allow_minimessage: false
# Toggle if color should only be applicable if the player a certain permission. # Toggle if color should only be applicable if the player a certain permission.
# #

View file

@ -59,8 +59,10 @@ sacrifice_illegal_enchant_cost: 1
# #
# Color code are prefixed by "&" and hexadecimal color by "#". # Color code are prefixed by "&" and hexadecimal color by "#".
# Color code will not be applied if it colors nothing. "&&" can be used to write "&". # Color code will not be applied if it colors nothing. "&&" can be used to write "&".
# For minimessage search for minimessage formating https://docs.papermc.io/adventure/minimessage/format/
allow_color_code: false allow_color_code: false
allow_hexadecimal_color: false allow_hexadecimal_color: false
allow_minimessage: false
# Toggle if color should only be applicable if the player a certain permission. # Toggle if color should only be applicable if the player a certain permission.
# #

View file

@ -59,8 +59,10 @@ sacrifice_illegal_enchant_cost: 1
# #
# Color code are prefixed by "&" and hexadecimal color by "#". # Color code are prefixed by "&" and hexadecimal color by "#".
# Color code will not be applied if it colors nothing. "&&" can be used to write "&". # Color code will not be applied if it colors nothing. "&&" can be used to write "&".
# For minimessage search for minimessage formating https://docs.papermc.io/adventure/minimessage/format/
allow_color_code: false allow_color_code: false
allow_hexadecimal_color: false allow_hexadecimal_color: false
allow_minimessage: false
# Toggle if color should only be applicable if the player a certain permission. # Toggle if color should only be applicable if the player a certain permission.
# #

View file

@ -38,6 +38,7 @@ object ConfigOptions {
// Color related config // Color related config
const val ALLOW_COLOR_CODE = "allow_color_code" const val ALLOW_COLOR_CODE = "allow_color_code"
const val ALLOW_HEXADECIMAL_COLOR = "allow_hexadecimal_color" const val ALLOW_HEXADECIMAL_COLOR = "allow_hexadecimal_color"
const val ALLOW_MINIMESSAGE = "allow_minimessage"
const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color" const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color"
const val USE_OF_COLOR_COST = "use_of_color_cost" const val USE_OF_COLOR_COST = "use_of_color_cost"
@ -87,13 +88,14 @@ object ConfigOptions {
const val DEFAULT_ITEM_RENAME_COST = 1 const val DEFAULT_ITEM_RENAME_COST = 1
const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1 const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1
const val DEFAULT_ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT = false; const val DEFAULT_ADD_BOOK_ENCHANTMENT_AS_STORED_ENCHANTMENT = false
const val DEFAULT_ENCHANT_COUNT_LIMIT = -1 const val DEFAULT_ENCHANT_COUNT_LIMIT = -1
// Color related config // Color related config
const val DEFAULT_ALLOW_COLOR_CODE = false const val DEFAULT_ALLOW_COLOR_CODE = false
const val DEFAULT_ALLOW_HEXADECIMAL_COLOR = false const val DEFAULT_ALLOW_HEXADECIMAL_COLOR = false
const val DEFAULT_ALLOW_MINIMESSAGE = false
const val DEFAULT_PERMISSION_NEEDED_FOR_COLOR = true const val DEFAULT_PERMISSION_NEEDED_FOR_COLOR = true
const val DEFAULT_USE_OF_COLOR_COST = 0 const val DEFAULT_USE_OF_COLOR_COST = 0
@ -269,12 +271,22 @@ object ConfigOptions {
.getBoolean(ALLOW_HEXADECIMAL_COLOR, DEFAULT_ALLOW_HEXADECIMAL_COLOR) .getBoolean(ALLOW_HEXADECIMAL_COLOR, DEFAULT_ALLOW_HEXADECIMAL_COLOR)
} }
/**
* Allow usage of minimessage formating
*/
val allowMinimessage: Boolean
get() {
return ConfigHolder.DEFAULT_CONFIG
.config
.getBoolean(ALLOW_MINIMESSAGE, DEFAULT_ALLOW_MINIMESSAGE)
}
/** /**
* If one of the color component is enabled * If one of the color component is enabled
*/ */
val renameColorPossible: Boolean val renameColorPossible: Boolean
get() { get() {
return allowColorCode || allowHexadecimalColor return allowColorCode || allowHexadecimalColor || allowHexadecimalColor
} }
/** /**

View file

@ -196,7 +196,7 @@ class PrepareAnvilListener : Listener {
useColor = AnvilColorUtil.handleColor( useColor = AnvilColorUtil.handleColor(
resultString, player, resultString, player,
ConfigOptions.permissionNeededForColor, ConfigOptions.permissionNeededForColor,
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
AnvilColorUtil.ColorUseType.RENAME AnvilColorUtil.ColorUseType.RENAME
) )

View file

@ -1,5 +1,9 @@
package xyz.alexcrea.cuanvil.util 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 org.bukkit.permissions.Permissible import org.bukkit.permissions.Permissible
import java.util.regex.Matcher import java.util.regex.Matcher
import java.util.regex.Pattern import java.util.regex.Pattern
@ -8,9 +12,19 @@ object AnvilColorUtil {
private val HEX_PATTERN: Pattern = Pattern.compile("#[A-Fa-f0-9]{6}") // pattern to find hexadecimal string 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 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 * 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 * @return if the stringbuilder was changed and color applied or if minimessage formating was applied
*/ */
fun handleColor( fun handleColor(
textToColor: StringBuilder, textToColor: StringBuilder,
@ -18,9 +32,10 @@ object AnvilColorUtil {
usePermission: Boolean, usePermission: Boolean,
allowColorCode: Boolean, allowColorCode: Boolean,
allowHexadecimalColor: Boolean, allowHexadecimalColor: Boolean,
allowMinimessage: Boolean,
useType: ColorUseType useType: ColorUseType
): Boolean { ): Boolean {
if (!allowColorCode && !allowHexadecimalColor) return false if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return false
val canUseColorCode = val canUseColorCode =
allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission( allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission(
@ -30,12 +45,16 @@ object AnvilColorUtil {
allowHexadecimalColor && (!usePermission || useType.hexColorPerm == null || player.hasPermission( allowHexadecimalColor && (!usePermission || useType.hexColorPerm == null || player.hasPermission(
useType.hexColorPerm useType.hexColorPerm
)) ))
val canUseMinimessage =
allowMinimessage && (!usePermission || useType.minimessagePerm == null || player.hasPermission(
useType.minimessagePerm
))
if ((!canUseColorCode) && (!canUseHexColor)) return false if ((!canUseColorCode) && (!canUseHexColor)) return false
var useColor = false var useColor = false
// Handle color code // Handle color code
if (canUseColorCode) { if (canUseColorCode) { // maybe should use LegacyComponentSerializer ?
var nbReplacement = replaceAll(textToColor, "&", "§", 2) var nbReplacement = replaceAll(textToColor, "&", "§", 2)
nbReplacement -= 2 * replaceAll(textToColor, "§§", "&", 2) nbReplacement -= 2 * replaceAll(textToColor, "§§", "&", 2)
@ -48,6 +67,22 @@ object AnvilColorUtil {
if (nbReplacement > 0) useColor = true if (nbReplacement > 0) useColor = true
} }
if(canUseMinimessage) {
val previousStr = textToColor.toString()
// we dance with formats here
val fromLegacy = legacymm.deserialize(previousStr)
val toMinimessage = mm.serialize(fromLegacy)
val hackySolution = toMinimessage.replace("\\<", "<")
val fromMinimessage = mm.deserialize(hackySolution)
val toLegacy = legacymm.serialize(fromMinimessage)
if(previousStr != toLegacy){
useColor = true
textToColor.replace(0, textToColor.length, toLegacy)
}
}
return useColor return useColor
} }
@ -177,10 +212,11 @@ object AnvilColorUtil {
enum class ColorUseType( enum class ColorUseType(
val colorCodePerm: String?, val colorCodePerm: String?,
val hexColorPerm: String? val hexColorPerm: String?,
val minimessagePerm: String?
) { ) {
RENAME("ca.color.code", "ca.color.hex"), RENAME("ca.color.code", "ca.color.hex", "ca.color.minimessage"),
LORE_EDIT(null, null) LORE_EDIT(null, null, null)
} }
} }

View file

@ -243,9 +243,10 @@ object AnvilLoreEditUtil {
private fun colorLines(player: Permissible, lines: ArrayList<String>, editType: LoreEditType): Int { private fun colorLines(player: Permissible, lines: ArrayList<String>, editType: LoreEditType): Int {
val canUseHex = editType.allowHexColor val canUseHex = editType.allowHexColor
val canUseColorCode = editType.allowColorCode val canUseColorCode = editType.allowColorCode
val minimessage = editType.allowMinimessage
val colorCost = editType.useColorCost val colorCost = editType.useColorCost
// Now handle color of each lines // Handle color and minimessage of each lines
var hasUsedColor = false var hasUsedColor = false
for ((index, line) in lines.withIndex()) { for ((index, line) in lines.withIndex()) {
val coloredLine = StringBuilder(line) val coloredLine = StringBuilder(line)
@ -253,7 +254,8 @@ object AnvilLoreEditUtil {
val lineUsedColor = AnvilColorUtil.handleColor( val lineUsedColor = AnvilColorUtil.handleColor(
coloredLine, coloredLine,
player, player,
false, canUseColorCode, canUseHex, false,
canUseColorCode, canUseHex, minimessage,
AnvilColorUtil.ColorUseType.LORE_EDIT AnvilColorUtil.ColorUseType.LORE_EDIT
) )

View file

@ -17,6 +17,7 @@ object LoreEditConfigUtil {
// Color configs path // Color configs path
const val ALLOW_COLOR_CODE = "allow_color_code" const val ALLOW_COLOR_CODE = "allow_color_code"
const val ALLOW_HEX_COLOR = "allow_hexadecimal_color" const val ALLOW_HEX_COLOR = "allow_hexadecimal_color"
const val ALLOW_MINIMESSAGE = "allow_minimessage"
const val USE_COLOR_COST = "use_cost" const val USE_COLOR_COST = "use_cost"
const val REMOVE_COLOR_ON_LORE_REMOVE = "remove_color_on_remove" const val REMOVE_COLOR_ON_LORE_REMOVE = "remove_color_on_remove"
@ -42,6 +43,7 @@ object LoreEditConfigUtil {
// Color configs defaults // Color configs defaults
const val DEFAULT_ALLOW_COLOR_CODE = true const val DEFAULT_ALLOW_COLOR_CODE = true
const val DEFAULT_ALLOW_HEX_COLOR = true const val DEFAULT_ALLOW_HEX_COLOR = true
const val DEFAULT_ALLOW_MINIMESSAGE = true
const val DEFAULT_USE_COLOR_COST = 0 const val DEFAULT_USE_COLOR_COST = 0
const val DEFAULT_REMOVE_COLOR_ON_LORE_REMOVE = false const val DEFAULT_REMOVE_COLOR_ON_LORE_REMOVE = false

View file

@ -3,8 +3,10 @@ package xyz.alexcrea.cuanvil.util.config
import xyz.alexcrea.cuanvil.util.AnvilUseType import xyz.alexcrea.cuanvil.util.AnvilUseType
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.ALLOW_COLOR_CODE import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.ALLOW_COLOR_CODE
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.ALLOW_HEX_COLOR import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.ALLOW_HEX_COLOR
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.ALLOW_MINIMESSAGE
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_ALLOW_COLOR_CODE import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_ALLOW_COLOR_CODE
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_ALLOW_HEX_COLOR import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_ALLOW_HEX_COLOR
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_ALLOW_MINIMESSAGE
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_REMOVE_COLOR_COST import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_REMOVE_COLOR_COST
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_REMOVE_COLOR_ON_LORE_REMOVE import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_REMOVE_COLOR_ON_LORE_REMOVE
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_USE_COLOR_COST import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil.DEFAULT_USE_COLOR_COST
@ -100,6 +102,17 @@ enum class LoreEditType(
.getBoolean("${rootPath}.$ALLOW_HEX_COLOR", DEFAULT_ALLOW_HEX_COLOR) .getBoolean("${rootPath}.$ALLOW_HEX_COLOR", DEFAULT_ALLOW_HEX_COLOR)
} }
/**
* Allow usage of minimessage on lore add
*/
val allowMinimessage: Boolean
get() {
if (!isAppend) throw IllegalStateException("Can only call with an append edit type")
return CONFIG
.config
.getBoolean("${rootPath}.$ALLOW_MINIMESSAGE", DEFAULT_ALLOW_MINIMESSAGE)
}
/** /**
* Cost when using either color code and hex color on lore add * Cost when using either color code and hex color on lore add
*/ */

View file

@ -59,8 +59,10 @@ sacrifice_illegal_enchant_cost: 1
# #
# Color code are prefixed by "&" and hexadecimal color by "#". # Color code are prefixed by "&" and hexadecimal color by "#".
# Color code will not be applied if it colors nothing. "&&" can be used to write "&". # Color code will not be applied if it colors nothing. "&&" can be used to write "&".
# For minimessage search for minimessage formating https://docs.papermc.io/adventure/minimessage/format/
allow_color_code: false allow_color_code: false
allow_hexadecimal_color: false allow_hexadecimal_color: false
allow_minimessage: false
# Toggle if color should only be applicable if the player a certain permission. # Toggle if color should only be applicable if the player a certain permission.
# #