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

@ -38,6 +38,7 @@ object ConfigOptions {
// Color related config
const val ALLOW_COLOR_CODE = "allow_color_code"
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 USE_OF_COLOR_COST = "use_of_color_cost"
@ -87,13 +88,14 @@ object ConfigOptions {
const val DEFAULT_ITEM_RENAME_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
// Color related config
const val DEFAULT_ALLOW_COLOR_CODE = 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_USE_OF_COLOR_COST = 0
@ -269,12 +271,22 @@ object ConfigOptions {
.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
*/
val renameColorPossible: Boolean
get() {
return allowColorCode || allowHexadecimalColor
return allowColorCode || allowHexadecimalColor || allowHexadecimalColor
}
/**

View file

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

View file

@ -1,5 +1,9 @@
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 java.util.regex.Matcher
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 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
* @return if the stringbuilder was changed and color applied or if minimessage formating was applied
*/
fun handleColor(
textToColor: StringBuilder,
@ -18,9 +32,10 @@ object AnvilColorUtil {
usePermission: Boolean,
allowColorCode: Boolean,
allowHexadecimalColor: Boolean,
allowMinimessage: Boolean,
useType: ColorUseType
): Boolean {
if (!allowColorCode && !allowHexadecimalColor) return false
if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return false
val canUseColorCode =
allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission(
@ -30,12 +45,16 @@ object AnvilColorUtil {
allowHexadecimalColor && (!usePermission || useType.hexColorPerm == null || player.hasPermission(
useType.hexColorPerm
))
val canUseMinimessage =
allowMinimessage && (!usePermission || useType.minimessagePerm == null || player.hasPermission(
useType.minimessagePerm
))
if ((!canUseColorCode) && (!canUseHexColor)) return false
var useColor = false
// Handle color code
if (canUseColorCode) {
if (canUseColorCode) { // maybe should use LegacyComponentSerializer ?
var nbReplacement = replaceAll(textToColor, "&", "§", 2)
nbReplacement -= 2 * replaceAll(textToColor, "§§", "&", 2)
@ -48,6 +67,22 @@ object AnvilColorUtil {
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
}
@ -177,10 +212,11 @@ object AnvilColorUtil {
enum class ColorUseType(
val colorCodePerm: String?,
val hexColorPerm: String?
val hexColorPerm: String?,
val minimessagePerm: String?
) {
RENAME("ca.color.code", "ca.color.hex"),
LORE_EDIT(null, null)
RENAME("ca.color.code", "ca.color.hex", "ca.color.minimessage"),
LORE_EDIT(null, null, null)
}
}

View file

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

View file

@ -17,6 +17,7 @@ object LoreEditConfigUtil {
// Color configs path
const val ALLOW_COLOR_CODE = "allow_color_code"
const val ALLOW_HEX_COLOR = "allow_hexadecimal_color"
const val ALLOW_MINIMESSAGE = "allow_minimessage"
const val USE_COLOR_COST = "use_cost"
const val REMOVE_COLOR_ON_LORE_REMOVE = "remove_color_on_remove"
@ -42,6 +43,7 @@ object LoreEditConfigUtil {
// Color configs defaults
const val DEFAULT_ALLOW_COLOR_CODE = true
const val DEFAULT_ALLOW_HEX_COLOR = true
const val DEFAULT_ALLOW_MINIMESSAGE = true
const val DEFAULT_USE_COLOR_COST = 0
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.config.LoreEditConfigUtil.ALLOW_COLOR_CODE
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_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_ON_LORE_REMOVE
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)
}
/**
* 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
*/

View file

@ -59,8 +59,10 @@ sacrifice_illegal_enchant_cost: 1
#
# Color code are prefixed by "&" and hexadecimal color by "#".
# 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_hexadecimal_color: false
allow_minimessage: false
# Toggle if color should only be applicable if the player a certain permission.
#