mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
some refractor
This commit is contained in:
parent
8411b21d1c
commit
c9e41aceb6
6 changed files with 56 additions and 53 deletions
|
|
@ -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)
|
- 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)
|
- Can handle some custom enchantment plugins (see below for more information)
|
||||||
- Gui to configure the plugin in game.
|
- 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)
|
- (Experimental) Folia support (gui do not work)
|
||||||
---
|
---
|
||||||
### Permissions:
|
### 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
|
# Permissions related to use of color and minimessage
|
||||||
ca.color.code: Allow player to use color code on rename 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 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
|
# 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)
|
||||||
|
|
|
||||||
|
|
@ -186,24 +186,23 @@ class PrepareAnvilListener : Listener {
|
||||||
|
|
||||||
private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int {
|
private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int {
|
||||||
// Can be null
|
// Can be null
|
||||||
var inventoryName = ChatColor.stripColor(inventory.renameText)
|
var renameText = ChatColor.stripColor(inventory.renameText)
|
||||||
|
|
||||||
var sumCost = 0
|
var sumCost = 0
|
||||||
var useColor = false
|
var useColor = false
|
||||||
if (ConfigOptions.renameColorPossible && inventoryName != null) {
|
if (ConfigOptions.renameColorPossible && renameText != null) {
|
||||||
val resultString = StringBuilder(inventoryName)
|
val component = AnvilColorUtil.handleColor(
|
||||||
|
renameText, player,
|
||||||
useColor = AnvilColorUtil.handleColor(
|
|
||||||
resultString, player,
|
|
||||||
ConfigOptions.permissionNeededForColor,
|
ConfigOptions.permissionNeededForColor,
|
||||||
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
|
ConfigOptions.allowColorCode, ConfigOptions.allowHexadecimalColor, ConfigOptions.allowMinimessage,
|
||||||
AnvilColorUtil.ColorUseType.RENAME
|
AnvilColorUtil.ColorUseType.RENAME
|
||||||
)
|
)
|
||||||
|
|
||||||
if (useColor) {
|
if (component != null) {
|
||||||
inventoryName = resultString.toString()
|
renameText = MiniMessageUtil.legacy_mm.serialize(component)
|
||||||
|
|
||||||
sumCost += ConfigOptions.useOfColorCost
|
sumCost += ConfigOptions.useOfColorCost
|
||||||
|
useColor = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,8 +213,8 @@ class PrepareAnvilListener : Listener {
|
||||||
else if (useColor) it.displayName
|
else if (useColor) it.displayName
|
||||||
else ChatColor.stripColor(it.displayName)
|
else ChatColor.stripColor(it.displayName)
|
||||||
|
|
||||||
if (!displayName.contentEquals(inventoryName)) {
|
if (!displayName.contentEquals(renameText)) {
|
||||||
it.setDisplayName(inventoryName)
|
it.setDisplayName(renameText)
|
||||||
resultItem.itemMeta = it
|
resultItem.itemMeta = it
|
||||||
|
|
||||||
sumCost += ConfigOptions.itemRenameCost
|
sumCost += ConfigOptions.itemRenameCost
|
||||||
|
|
@ -233,10 +232,10 @@ class PrepareAnvilListener : Listener {
|
||||||
) {
|
) {
|
||||||
val newEnchants = first.findEnchantments()
|
val newEnchants = first.findEnchantments()
|
||||||
.combineWith(second.findEnchantments(), first, player)
|
.combineWith(second.findEnchantments(), first, player)
|
||||||
var hasChanged = !isIdentical(first.findEnchantments(), newEnchants);
|
var hasChanged = !isIdentical(first.findEnchantments(), newEnchants)
|
||||||
|
|
||||||
val resultItem = first.clone()
|
val resultItem = first.clone()
|
||||||
var anvilCost = 0;
|
var anvilCost = 0
|
||||||
if(hasChanged){
|
if(hasChanged){
|
||||||
resultItem.setEnchantmentsUnsafe(newEnchants)
|
resultItem.setEnchantmentsUnsafe(newEnchants)
|
||||||
// Calculate enchantment cost
|
// Calculate enchantment cost
|
||||||
|
|
@ -248,7 +247,7 @@ class PrepareAnvilListener : Listener {
|
||||||
// we only need to be concerned with repair when neither item is a book
|
// we only need to be concerned with repair when neither item is a book
|
||||||
val repaired = resultItem.repairFrom(first, second)
|
val repaired = resultItem.repairFrom(first, second)
|
||||||
anvilCost += if (repaired) ConfigOptions.itemRepairCost else 0
|
anvilCost += if (repaired) ConfigOptions.itemRepairCost else 0
|
||||||
hasChanged = hasChanged || repaired;
|
hasChanged = hasChanged || repaired
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test/stop if nothing changed.
|
// Test/stop if nothing changed.
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package xyz.alexcrea.cuanvil.util
|
package xyz.alexcrea.cuanvil.util
|
||||||
|
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage
|
import net.kyori.adventure.text.Component
|
||||||
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
|
||||||
|
|
@ -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 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 or if minimessage formating was applied
|
* @return if the stringbuilder was changed and color applied or if minimessage formating was applied
|
||||||
*/
|
*/
|
||||||
fun handleColor(
|
fun handleColor(
|
||||||
textToColor: StringBuilder,
|
textToColorText: String,
|
||||||
player: Permissible,
|
player: Permissible,
|
||||||
usePermission: Boolean,
|
usePermission: Boolean,
|
||||||
allowColorCode: Boolean,
|
allowColorCode: Boolean,
|
||||||
allowHexadecimalColor: Boolean,
|
allowHexadecimalColor: Boolean,
|
||||||
allowMinimessage: Boolean,
|
allowMinimessage: Boolean,
|
||||||
useType: ColorUseType
|
useType: ColorUseType
|
||||||
): Boolean {
|
): Component? {
|
||||||
if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return false
|
if (!allowColorCode && !allowHexadecimalColor && !allowMinimessage) return null
|
||||||
|
|
||||||
val canUseColorCode =
|
val canUseColorCode =
|
||||||
allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission(
|
allowColorCode && (!usePermission || useType.colorCodePerm == null || player.hasPermission(
|
||||||
|
|
@ -50,8 +37,9 @@ object AnvilColorUtil {
|
||||||
useType.minimessagePerm
|
useType.minimessagePerm
|
||||||
))
|
))
|
||||||
|
|
||||||
if ((!canUseColorCode) && (!canUseHexColor)) return false
|
if (!canUseColorCode && !canUseHexColor && !canUseMinimessage) return null
|
||||||
|
|
||||||
|
val textToColor = StringBuilder(textToColorText)
|
||||||
var useColor = false
|
var useColor = false
|
||||||
// Handle color code
|
// Handle color code
|
||||||
if (canUseColorCode) { // maybe should use LegacyComponentSerializer ?
|
if (canUseColorCode) { // maybe should use LegacyComponentSerializer ?
|
||||||
|
|
@ -67,23 +55,23 @@ object AnvilColorUtil {
|
||||||
if (nbReplacement > 0) useColor = true
|
if (nbReplacement > 0) useColor = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val previousStr = textToColor.toString()
|
||||||
|
var result: Component = MiniMessageUtil.legacy_mm.deserialize(previousStr)
|
||||||
if(canUseMinimessage) {
|
if(canUseMinimessage) {
|
||||||
val previousStr = textToColor.toString()
|
|
||||||
|
|
||||||
// we dance with formats here
|
// we dance with formats here
|
||||||
val fromLegacy = legacymm.deserialize(previousStr)
|
val toMinimessage = MiniMessageUtil.mm.serialize(result)
|
||||||
val toMinimessage = mm.serialize(fromLegacy)
|
|
||||||
val hackySolution = toMinimessage.replace("\\<", "<")
|
val hackySolution = toMinimessage.replace("\\<", "<")
|
||||||
val fromMinimessage = mm.deserialize(hackySolution)
|
val fromMinimessage = MiniMessageUtil.mm.deserialize(hackySolution)
|
||||||
val toLegacy = legacymm.serialize(fromMinimessage)
|
val toLegacy = MiniMessageUtil.legacy_mm.serialize(fromMinimessage)
|
||||||
|
|
||||||
if(previousStr != toLegacy){
|
if(previousStr != toLegacy){
|
||||||
useColor = true
|
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 hexColorPerm: String?,
|
||||||
val minimessagePerm: 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)
|
LORE_EDIT(null, null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -249,27 +249,22 @@ object AnvilLoreEditUtil {
|
||||||
// Handle color and minimessage 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 component = AnvilColorUtil.handleColor(
|
||||||
|
line,
|
||||||
val lineUsedColor = AnvilColorUtil.handleColor(
|
|
||||||
coloredLine,
|
|
||||||
player,
|
player,
|
||||||
false,
|
false,
|
||||||
canUseColorCode, canUseHex, minimessage,
|
canUseColorCode, canUseHex, minimessage,
|
||||||
AnvilColorUtil.ColorUseType.LORE_EDIT
|
AnvilColorUtil.ColorUseType.LORE_EDIT
|
||||||
)
|
)
|
||||||
|
|
||||||
if (lineUsedColor) {
|
if (component != null) {
|
||||||
hasUsedColor = true
|
hasUsedColor = true
|
||||||
lines[index] = coloredLine.toString()
|
lines[index] = MiniMessageUtil.legacy_mm.serialize(component)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return if (hasUsedColor) {
|
return if (hasUsedColor) colorCost
|
||||||
colorCost
|
else 0
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun uncolorLines(player: Permissible, lines: ArrayList<String>, editType: LoreEditType): Int {
|
fun uncolorLines(player: Permissible, lines: ArrayList<String>, editType: LoreEditType): Int {
|
||||||
|
|
|
||||||
18
src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt
Normal file
18
src/main/kotlin/xyz/alexcrea/cuanvil/util/MiniMessageUtil.kt
Normal 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()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -46,6 +46,9 @@ permissions:
|
||||||
ca.color.hex:
|
ca.color.hex:
|
||||||
default: op
|
default: op
|
||||||
description: Allow player to use hexadecimal color if enabled (toggleable)
|
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
|
# lore edit permissions
|
||||||
ca.lore_edit.book:
|
ca.lore_edit.book:
|
||||||
default: op
|
default: op
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue