disallow use of hex code and minimessage on append

This commit is contained in:
alexcrea 2025-10-23 15:34:04 +02:00
parent 11f7bf8602
commit 517fcf3430
Signed by: alexcrea
GPG key ID: E346CD16413450E3
8 changed files with 58 additions and 51 deletions

View file

@ -26,6 +26,7 @@ import xyz.alexcrea.cuanvil.util.AnvilLoreEditUtil
import xyz.alexcrea.cuanvil.util.AnvilUseType
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
import xyz.alexcrea.cuanvil.util.MiniMessageUtil
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil
import xyz.alexcrea.cuanvil.util.config.LoreEditType
@ -412,7 +413,9 @@ class AnvilResultListener : Listener {
val bookPage = StringBuilder()
lore.forEach {
if (bookPage.isNotEmpty()) bookPage.append('\n')
bookPage.append(it)
if(it == null) return@forEach
bookPage.append(MiniMessageUtil.plain_text_mm.serialize(it))
}
val resultPage = bookPage.toString()

View file

@ -195,7 +195,7 @@ class PrepareAnvilListener : Listener {
renameText, player,
ConfigOptions.permissionNeededForColor,
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
AnvilColorUtil.ColorUseType.RENAME
AnvilColorUtil.ColorUseType.RENAME, true
)
if (component != null) {

View file

@ -29,7 +29,9 @@ object AnvilColorUtil {
allowColorCode: Boolean,
allowHexadecimalColor: Boolean,
allowMinimessage: Boolean,
useType: ColorUseType): ColorPermissions {
useType: ColorUseType,
isAppend: Boolean = true
): ColorPermissions {
if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage)
return ColorPermissions(
canUseColorCode = false,
@ -41,15 +43,20 @@ object AnvilColorUtil {
allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission(
useType.colorCodePerm
))
val canUseHexColor =
allowHexadecimalColor && (!usePermission || useType.hexColorPerm == null || player.hasPermission(
useType.hexColorPerm
))
val canUseMinimessage =
allowMinimessage && (!usePermission || useType.minimessagePerm == null || player.hasPermission(
useType.minimessagePerm
))
// Do not allow minimessage and hex color at the same time when coming from string to component (usually/assumed append)
val minimessageConflict = canUseMinimessage && isAppend
val canUseHexColor = !minimessageConflict &&
allowHexadecimalColor && (!usePermission || useType.hexColorPerm == null || player.hasPermission(
useType.hexColorPerm
))
return ColorPermissions(canUseColorCode, canUseHexColor, canUseMinimessage)
}
@ -64,11 +71,12 @@ object AnvilColorUtil {
allowColorCode: Boolean,
allowHexadecimalColor: Boolean,
allowMinimessage: Boolean,
useType: ColorUseType
useType: ColorUseType,
isAppend: Boolean
): Component? {
val permission = calculatePermissions(player, usePermission,
allowColorCode, allowHexadecimalColor, allowMinimessage,
useType)
useType, isAppend)
return handleColor(textToColorText, permission)
}
@ -117,27 +125,6 @@ object AnvilColorUtil {
else null
}
/**
* Best effort to revert a component to the smallest allowed string
* that would result in it getting closest as possible to handleColor
* with current set of color type, color use type and player permissions
* @return the new component if had any change. null otherwise
*/
fun revertColorSmallest(
component: Component,
player: Permissible,
usePermission: Boolean,
allowColorCode: Boolean,
allowMinimessage: Boolean,
allowHexadecimalColor: Boolean,
useType: ColorUseType
): String? {
val permission = calculatePermissions(player, usePermission,
allowColorCode, allowHexadecimalColor, allowMinimessage,
useType)
return revertColorSmallest(component, permission)
}
/**
* Best effort to revert a component to the smallest allowed string
* that would result in it getting closest as possible to handleColor

View file

@ -117,10 +117,10 @@ object AnvilLoreEditUtil {
}
fun tryLoreEditByBook(player: HumanEntity, first: ItemStack, second: ItemStack, xpCost: AtomicInteger): ItemStack? {
val bookType = bookLoreEditIsAppend(first, second) ?: return null
val isAppend = bookLoreEditIsAppend(first, second) ?: return null
val meta = second.itemMeta as BookMeta
return if (bookType) handleLoreAppendByBook(player, first, meta, xpCost)
return if (isAppend) handleLoreAppendByBook(player, first, meta, xpCost)
else handleLoreRemoveByBook(player, first, xpCost)
}
@ -225,9 +225,9 @@ object AnvilLoreEditUtil {
second: ItemStack,
xpCost: AtomicInteger
): ItemStack? {
val bookType = paperLoreEditIsAppend(first, second) ?: return null
val isAppend = paperLoreEditIsAppend(first, second) ?: return null
return if (bookType) handleLoreAppendByPaper(player, first, second, xpCost)
return if (isAppend) handleLoreAppendByPaper(player, first, second, xpCost)
else handleLoreRemoveByPaper(player, first, xpCost)
}
@ -248,7 +248,8 @@ object AnvilLoreEditUtil {
editType.allowColorCode,
editType.allowHexColor,
editType.allowMinimessage,
AnvilColorUtil.ColorUseType.LORE_EDIT)
AnvilColorUtil.ColorUseType.LORE_EDIT,
editType.isAppend)
}
private fun colorLine(line: String, permission: AnvilColorUtil.ColorPermissions): Component? {