mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
per color code permission
This commit is contained in:
parent
809dc3488b
commit
d926b5001e
7 changed files with 119 additions and 20 deletions
|
|
@ -77,6 +77,18 @@ allow_color_code: false
|
|||
allow_hexadecimal_color: false
|
||||
allow_minimessage: false
|
||||
|
||||
# This enables restricting color code for player having specific permission
|
||||
# It requires allow_color_code enabled for... obvious reasons
|
||||
#
|
||||
# For example: if player want to use "&aHello" it will be required that the player has
|
||||
# the permission "ca.color.code.a" as he used the color code "a"
|
||||
# In general permission to give to the player is "ca.color.code.[code]"
|
||||
# where [code] is the color code you wish to allow the player
|
||||
#
|
||||
# It is kinda of useless when minimessage is supported as players would be able to bypass
|
||||
# that using the equivalent minimessage tag
|
||||
per_color_code_permission: false
|
||||
|
||||
# Toggle if color should only be applicable if the player a certain permission.
|
||||
#
|
||||
# permission are "ca.color.code" for use of color code and "ca.color.hex" for use of hexadecimal color.
|
||||
|
|
|
|||
|
|
@ -79,6 +79,18 @@ allow_color_code: false
|
|||
allow_hexadecimal_color: false
|
||||
allow_minimessage: false
|
||||
|
||||
# This enables restricting color code for player having specific permission
|
||||
# It requires allow_color_code enabled for... obvious reasons
|
||||
#
|
||||
# For example: if player want to use "&aHello" it will be required that the player has
|
||||
# the permission "ca.color.code.a" as he used the color code "a"
|
||||
# In general permission to give to the player is "ca.color.code.[code]"
|
||||
# where [code] is the color code you wish to allow the player
|
||||
#
|
||||
# It is kinda of useless when minimessage is supported as players would be able to bypass
|
||||
# that using the equivalent minimessage tag
|
||||
per_color_code_permission: false
|
||||
|
||||
# Toggle if color should only be applicable if the player a certain permission.
|
||||
#
|
||||
# permission are "ca.color.code" for use of color code and "ca.color.hex" for use of hexadecimal color.
|
||||
|
|
|
|||
|
|
@ -77,6 +77,18 @@ allow_color_code: false
|
|||
allow_hexadecimal_color: false
|
||||
allow_minimessage: false
|
||||
|
||||
# This enables restricting color code for player having specific permission
|
||||
# It requires allow_color_code enabled for... obvious reasons
|
||||
#
|
||||
# For example: if player want to use "&aHello" it will be required that the player has
|
||||
# the permission "ca.color.code.a" as he used the color code "a"
|
||||
# In general permission to give to the player is "ca.color.code.[code]"
|
||||
# where [code] is the color code you wish to allow the player
|
||||
#
|
||||
# It is kinda of useless when minimessage is supported as players would be able to bypass
|
||||
# that using the equivalent minimessage tag
|
||||
per_color_code_permission: false
|
||||
|
||||
# Toggle if color should only be applicable if the player a certain permission.
|
||||
#
|
||||
# permission are "ca.color.code" for use of color code and "ca.color.hex" for use of hexadecimal color.
|
||||
|
|
|
|||
|
|
@ -77,6 +77,18 @@ allow_color_code: false
|
|||
allow_hexadecimal_color: false
|
||||
allow_minimessage: false
|
||||
|
||||
# This enables restricting color code for player having specific permission
|
||||
# It requires allow_color_code enabled for... obvious reasons
|
||||
#
|
||||
# For example: if player want to use "&aHello" it will be required that the player has
|
||||
# the permission "ca.color.code.a" as he used the color code "a"
|
||||
# In general permission to give to the player is "ca.color.code.[code]"
|
||||
# where [code] is the color code you wish to allow the player
|
||||
#
|
||||
# It is kinda of useless when minimessage is supported as players would be able to bypass
|
||||
# that using the equivalent minimessage tag
|
||||
per_color_code_permission: false
|
||||
|
||||
# Toggle if color should only be applicable if the player a certain permission.
|
||||
#
|
||||
# permission are "ca.color.code" for use of color code and "ca.color.hex" for use of hexadecimal color.
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package io.delilaheve.util
|
|||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.NamespacedKey
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.config.WorkPenaltyType
|
||||
|
|
@ -45,6 +44,8 @@ object ConfigOptions {
|
|||
const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color"
|
||||
const val USE_OF_COLOR_COST = "use_of_color_cost"
|
||||
|
||||
const val PER_COLOR_CODE_PERMISSION = "per_color_code_permission"
|
||||
|
||||
// Work penalty config
|
||||
const val WORK_PENALTY_ROOT = "work_penalty"
|
||||
const val WORK_PENALTY_INCREASE = "shared_increase"
|
||||
|
|
@ -100,6 +101,8 @@ object ConfigOptions {
|
|||
const val DEFAULT_PERMISSION_NEEDED_FOR_COLOR = true
|
||||
const val DEFAULT_USE_OF_COLOR_COST = 0
|
||||
|
||||
const val DEFAULT_PER_COLOR_CODE_PERMISSION = false
|
||||
|
||||
// Debug flag
|
||||
private const val DEFAULT_DEBUG_LOG = false
|
||||
private const val DEFAULT_VERBOSE_DEBUG_LOG = false
|
||||
|
|
@ -297,6 +300,16 @@ object ConfigOptions {
|
|||
.getBoolean(PERMISSION_NEEDED_FOR_COLOR, DEFAULT_PERMISSION_NEEDED_FOR_COLOR)
|
||||
}
|
||||
|
||||
/**
|
||||
* Should each color code require a permission
|
||||
*/
|
||||
val usePerColorCodePermission: Boolean
|
||||
get() {
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(PER_COLOR_CODE_PERMISSION, DEFAULT_PER_COLOR_CODE_PERMISSION)
|
||||
}
|
||||
|
||||
/**
|
||||
* How many xp should use of color should cost
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
import io.delilaheve.util.ConfigOptions
|
||||
import net.kyori.adventure.text.Component
|
||||
import org.bukkit.permissions.Permissible
|
||||
import java.util.regex.Matcher
|
||||
|
|
@ -13,7 +14,8 @@ object AnvilColorUtil {
|
|||
class ColorPermissions(
|
||||
val canUseColorCode: Boolean,
|
||||
val canUseHexColor: Boolean,
|
||||
val canUseMinimessage: Boolean
|
||||
val canUseMinimessage: Boolean,
|
||||
val permissible: Permissible, // source of the permission. tried to avoid needing it but meh
|
||||
) {
|
||||
fun allowed(): Boolean {
|
||||
return canUseColorCode || canUseHexColor || canUseMinimessage
|
||||
|
|
@ -36,7 +38,8 @@ object AnvilColorUtil {
|
|||
return ColorPermissions(
|
||||
canUseColorCode = false,
|
||||
canUseHexColor = false,
|
||||
canUseMinimessage = false
|
||||
canUseMinimessage = false,
|
||||
player,
|
||||
)
|
||||
|
||||
val canUseColorCode =
|
||||
|
|
@ -54,7 +57,7 @@ object AnvilColorUtil {
|
|||
useType.hexColorPerm
|
||||
))
|
||||
|
||||
return ColorPermissions(canUseColorCode, canUseHexColor, canUseMinimessage)
|
||||
return ColorPermissions(canUseColorCode, canUseHexColor, canUseMinimessage, player)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -70,9 +73,11 @@ object AnvilColorUtil {
|
|||
allowMinimessage: Boolean,
|
||||
useType: ColorUseType
|
||||
): Component? {
|
||||
val permission = calculatePermissions(player, usePermission,
|
||||
val permission = calculatePermissions(
|
||||
player, usePermission,
|
||||
allowColorCode, allowHexadecimalColor, allowMinimessage,
|
||||
useType)
|
||||
useType
|
||||
)
|
||||
return handleColor(textToColorText, permission)
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +87,8 @@ object AnvilColorUtil {
|
|||
*/
|
||||
fun handleColor(
|
||||
textToColorText: String,
|
||||
permission: ColorPermissions
|
||||
permission: ColorPermissions,
|
||||
|
||||
): Component? {
|
||||
if (!permission.allowed()) return null
|
||||
|
||||
|
|
@ -93,7 +99,12 @@ object AnvilColorUtil {
|
|||
var nbReplacement = replaceAll(textToColor, "&", "§", 2)
|
||||
nbReplacement -= 2 * replaceAll(textToColor, "§§", "&", 2)
|
||||
|
||||
if (nbReplacement > 0) useColor = true
|
||||
if (nbReplacement > 0) {
|
||||
useColor = true
|
||||
|
||||
if (ConfigOptions.usePerColorCodePermission)
|
||||
filterPermissibleColorCode(textToColor, permission.permissible)
|
||||
}
|
||||
}
|
||||
|
||||
if (permission.canUseHexColor) {
|
||||
|
|
@ -121,6 +132,21 @@ object AnvilColorUtil {
|
|||
else null
|
||||
}
|
||||
|
||||
private fun filterPermissibleColorCode(textToColor: StringBuilder, player: Permissible) {
|
||||
var index = 0
|
||||
while (true) {
|
||||
index = textToColor.indexOf('§', index)
|
||||
if (index == -1 || index == textToColor.length - 1) return
|
||||
|
||||
val next = textToColor[index + 1]
|
||||
// check permission for this color
|
||||
if(!player.hasPermission("ca.color.code.$next"))
|
||||
textToColor.replace(index, index + 1, "&")
|
||||
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Best effort to revert a component to the smallest allowed string
|
||||
* that would result in it getting closest as possible to handleColor
|
||||
|
|
|
|||
|
|
@ -79,6 +79,18 @@ allow_color_code: false
|
|||
allow_hexadecimal_color: false
|
||||
allow_minimessage: false
|
||||
|
||||
# This enables restricting color code for player having specific permission
|
||||
# It requires allow_color_code enabled for... obvious reasons
|
||||
#
|
||||
# For example: if player want to use "&aHello" it will be required that the player has
|
||||
# the permission "ca.color.code.a" as he used the color code "a"
|
||||
# In general permission to give to the player is "ca.color.code.[code]"
|
||||
# where [code] is the color code you wish to allow the player
|
||||
#
|
||||
# It is kinda of useless when minimessage is supported as players would be able to bypass
|
||||
# that using the equivalent minimessage tag
|
||||
per_color_code_permission: false
|
||||
|
||||
# Toggle if color should only be applicable if the player a certain permission.
|
||||
#
|
||||
# permission are "ca.color.code" for use of color code and "ca.color.hex" for use of hexadecimal color.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue