some refractor

This commit is contained in:
alexcrea 2025-10-20 11:25:43 +02:00
parent 8411b21d1c
commit c9e41aceb6
Signed by: alexcrea
GPG key ID: E346CD16413450E3
6 changed files with 56 additions and 53 deletions

View file

@ -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)

View file

@ -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.

View file

@ -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
}
if(canUseMinimessage) {
val previousStr = textToColor.toString()
var result: Component = MiniMessageUtil.legacy_mm.deserialize(previousStr)
if(canUseMinimessage) {
// 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)
}

View file

@ -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<String>, editType: LoreEditType): Int {

View file

@ -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()
}

View file

@ -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