From ad24eb0423f6baf168824be2bed40e9df317a5cc Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Sat, 22 Apr 2023 17:31:04 +0300 Subject: [PATCH] Added the ability to use more hex patterns. Each can be enabled or disabled --- .../github/frcsty/actions/action/Action.kt | 3 + .../broadcast/ActionbarBroadcastAction.kt | 5 +- .../broadcast/AudienceBroadcastAction.kt | 5 +- .../action/broadcast/BroadcastAction.kt | 5 +- .../PersistentActionbarBroadcastAction.kt | 5 +- .../action/broadcast/TitleBroadcastAction.kt | 7 ++- .../action/player/ActionbarMessageAction.kt | 5 +- .../action/player/CenterMessageAction.kt | 5 +- .../actions/action/player/EquipItemAction.kt | 7 ++- .../actions/action/player/MessageAction.kt | 5 +- .../PersistentActionbarMessageAction.kt | 5 +- .../action/player/TitleMessageAction.kt | 7 ++- .../github/frcsty/actions/load/Settings.kt | 2 + .../frcsty/actions/util/ActinbarUtils.kt | 6 +- .../github/frcsty/actions/util/Extensions.kt | 59 ++++++------------- .../github/frcsty/command/FormatCommand.kt | 6 +- .../com/github/frcsty/command/HelpCommand.kt | 2 +- .../com/github/frcsty/command/InfoCommand.kt | 2 +- .../github/frcsty/command/ReloadCommand.kt | 2 +- .../frcsty/configuration/MessageLoader.kt | 6 +- .../kotlin/com/github/frcsty/load/Loader.kt | 11 ++-- .../kotlin/com/github/frcsty/load/Settings.kt | 15 +++++ .../github/frcsty/message/MessageFormatter.kt | 2 +- src/main/resources/config.yml | 11 ++++ 24 files changed, 107 insertions(+), 81 deletions(-) diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/Action.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/Action.kt index d8718e8..c261607 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/Action.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/Action.kt @@ -1,11 +1,14 @@ package com.github.frcsty.actions.action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import org.bukkit.entity.Player import org.bukkit.plugin.Plugin interface Action { val id: String fun run(player: Player, data: String, cache: PlaceholderCache? = null) {} + fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache? = null) {} fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache? = null) {} + fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache? = null) {} } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/ActionbarBroadcastAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/ActionbarBroadcastAction.kt index ffc769f..1b0fb7d 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/ActionbarBroadcastAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/ActionbarBroadcastAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage import org.bukkit.Bukkit @@ -10,7 +11,7 @@ import org.bukkit.entity.Player object ActionbarBroadcastAction : Action { override val id = "ACTIONBARBROADCAST" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { - Bukkit.getServer().onlinePlayers.forEach { it.sendActionBar(data.getTranslatedMessage(player, cache).color()) } + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { + Bukkit.getServer().onlinePlayers.forEach { it.sendActionBar(data.getTranslatedMessage(player, cache).color(settings.hexPatterns)) } } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/AudienceBroadcastAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/AudienceBroadcastAction.kt index 5db891c..f7772f7 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/AudienceBroadcastAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/AudienceBroadcastAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.sendTranslatedMessage import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -9,8 +10,8 @@ import org.bukkit.entity.Player object AudienceBroadcastAction : Action { override val id = "AUDIENCEBROADCAST" - override fun run(player: Player, data: String, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach { + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach { if (player == it) return@forEach - it.sendTranslatedMessage(data, player, cache) + it.sendTranslatedMessage(data, player, settings.hexPatterns, cache) } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/BroadcastAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/BroadcastAction.kt index e08d2db..fcae8f6 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/BroadcastAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/BroadcastAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.sendTranslatedMessage import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -9,7 +10,7 @@ import org.bukkit.entity.Player object BroadcastAction : Action { override val id = "BROADCAST" - override fun run(player: Player, data: String, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach { - it.sendTranslatedMessage(data, player, cache) + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) = Bukkit.getServer().onlinePlayers.forEach { + it.sendTranslatedMessage(data, player, settings.hexPatterns, cache) } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/PersistentActionbarBroadcastAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/PersistentActionbarBroadcastAction.kt index 73e6721..cd124ea 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/PersistentActionbarBroadcastAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/PersistentActionbarBroadcastAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.sendPersistentActionbarMessage import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -10,7 +11,7 @@ import org.bukkit.plugin.Plugin object PersistentActionbarBroadcastAction : Action { override val id = "PERSISTENTACTIONBARBROADCAST" - override fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache?) { - sendPersistentActionbarMessage(plugin, data, cache, player, Bukkit.getServer().onlinePlayers) + override fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { + sendPersistentActionbarMessage(plugin, data, cache, player, Bukkit.getServer().onlinePlayers, settings.hexPatterns) } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/TitleBroadcastAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/TitleBroadcastAction.kt index 6b17f33..c6ac949 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/TitleBroadcastAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/broadcast/TitleBroadcastAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.broadcast import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage import org.bukkit.Bukkit @@ -10,7 +11,7 @@ import org.bukkit.entity.Player object TitleBroadcastAction : Action { override val id = "TITLEBROADCAST" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { val args = data.split(";") val (title, subtitle) = args @@ -20,8 +21,8 @@ object TitleBroadcastAction : Action { Bukkit.getServer().onlinePlayers.forEach { it.sendTitle( - title.getTranslatedMessage(player, cache).color(), - subtitle.getTranslatedMessage(player, cache).color(), + title.getTranslatedMessage(player, cache).color(settings.hexPatterns), + subtitle.getTranslatedMessage(player, cache).color(settings.hexPatterns), fadeIn, stay, fadeOut diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/ActionbarMessageAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/ActionbarMessageAction.kt index a43eb70..9412f52 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/ActionbarMessageAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/ActionbarMessageAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage import org.bukkit.entity.Player @@ -9,7 +10,7 @@ import org.bukkit.entity.Player object ActionbarMessageAction : Action { override val id = "ACTIONBARMESSAGE" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { - player.sendActionBar(data.getTranslatedMessage(player, cache).color()) + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { + player.sendActionBar(data.getTranslatedMessage(player, cache).color(settings.hexPatterns)) } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/CenterMessageAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/CenterMessageAction.kt index d040790..cdee1b2 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/CenterMessageAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/CenterMessageAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.DefaultFontInfo import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage @@ -13,8 +14,8 @@ object CenterMessageAction : Action { private const val MAX_PX = 250 override val id = "CENTERMESSAGE" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { - var message = data.getTranslatedMessage(player, cache).color() + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { + var message = data.getTranslatedMessage(player, cache).color(settings.hexPatterns) var messagePxSize = 0 var previousCode = false diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/EquipItemAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/EquipItemAction.kt index 2b685ac..26773a6 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/EquipItemAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/EquipItemAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage import org.bukkit.Material @@ -12,7 +13,7 @@ object EquipItemAction : Action { override val id = "EQUIP" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { val args = data.split(";") val vars: List = args[0].split(",") val useData = vars.size == 2 @@ -32,9 +33,9 @@ object EquipItemAction : Action { } val meta = item.itemMeta - meta.setDisplayName(display.getTranslatedMessage(player, cache).color()) + meta.setDisplayName(display.getTranslatedMessage(player, cache).color(settings.hexPatterns)) if (lore.isNotEmpty()) { - meta.lore = lore.map { it.getTranslatedMessage(player, cache).color() } + meta.lore = lore.map { it.getTranslatedMessage(player, cache).color(settings.hexPatterns) } } item.itemMeta = meta diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/MessageAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/MessageAction.kt index 05da390..7c292fe 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/MessageAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/MessageAction.kt @@ -2,12 +2,13 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.sendTranslatedMessage import org.bukkit.entity.Player object MessageAction : Action { override val id = "MESSAGE" - override fun run(player: Player, data: String, cache: PlaceholderCache?) = - player.sendTranslatedMessage(data, player, cache) + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) = + player.sendTranslatedMessage(data, player, settings.hexPatterns, cache) } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/PersistentActionbarMessageAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/PersistentActionbarMessageAction.kt index a4a9f6c..20838d9 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/PersistentActionbarMessageAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/PersistentActionbarMessageAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.sendPersistentActionbarMessage import org.bukkit.entity.Player import org.bukkit.plugin.Plugin @@ -9,7 +10,7 @@ import org.bukkit.plugin.Plugin object PersistentActionbarMessageAction : Action { override val id = "PERSISTENTACTIONBARMESSAGE" - override fun run(plugin: Plugin, player: Player, data: String, cache: PlaceholderCache?) { - sendPersistentActionbarMessage(plugin, data, cache, player, listOf(player)) + override fun run(plugin: Plugin, player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { + sendPersistentActionbarMessage(plugin, data, cache, player, listOf(player), settings.hexPatterns) } } \ No newline at end of file diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/TitleMessageAction.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/TitleMessageAction.kt index 395835c..a971c7e 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/TitleMessageAction.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/action/player/TitleMessageAction.kt @@ -2,6 +2,7 @@ package com.github.frcsty.actions.action.player import com.github.frcsty.actions.action.Action import com.github.frcsty.actions.cache.PlaceholderCache +import com.github.frcsty.actions.load.Settings import com.github.frcsty.actions.util.color import com.github.frcsty.actions.util.getTranslatedMessage import org.bukkit.entity.Player @@ -10,7 +11,7 @@ object TitleMessageAction : Action { override val id = "TITLEMESSAGE" - override fun run(player: Player, data: String, cache: PlaceholderCache?) { + override fun run(player: Player, data: String, settings: Settings, cache: PlaceholderCache?) { val args = data.split(";") val (title, subtitle) = args @@ -19,8 +20,8 @@ object TitleMessageAction : Action { val fadeOut = args.getOrNull(5)?.toIntOrNull() ?: DEFAULT_FADE_OUT player.sendTitle( - title.getTranslatedMessage(player, cache).color(), - subtitle.getTranslatedMessage(player, cache).color(), + title.getTranslatedMessage(player, cache).color(settings.hexPatterns), + subtitle.getTranslatedMessage(player, cache).color(settings.hexPatterns), fadeIn, stay, fadeOut diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/load/Settings.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/load/Settings.kt index 4cb1078..a31413c 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/load/Settings.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/load/Settings.kt @@ -1,8 +1,10 @@ package com.github.frcsty.actions.load import java.util.logging.Logger +import java.util.regex.Pattern interface Settings { + val hexPatterns: LinkedHashSet val cacheUpdateInterval: Long var cachedPlaceholders: List val debug: Boolean diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/util/ActinbarUtils.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/util/ActinbarUtils.kt index b3ba9e5..64f9f09 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/util/ActinbarUtils.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/util/ActinbarUtils.kt @@ -4,6 +4,7 @@ import com.github.frcsty.actions.cache.PlaceholderCache import org.bukkit.entity.Player import org.bukkit.plugin.Plugin import org.bukkit.scheduler.BukkitRunnable +import java.util.regex.Pattern fun getPersistentActionbarMessageAndTime(data: String): Pair { val split = data.split(";", limit = 2) @@ -18,7 +19,8 @@ fun sendPersistentActionbarMessage( data: String, cache: PlaceholderCache?, actionPlayer: Player, - recipientPlayers: Collection + recipientPlayers: Collection, + hexPatterns: LinkedHashSet ) { val (time, message) = getPersistentActionbarMessageAndTime(data) @@ -31,7 +33,7 @@ fun sendPersistentActionbarMessage( } for (recipientPlayer in recipientPlayers) { - recipientPlayer.sendActionBar(message.getTranslatedMessage(actionPlayer, cache).color()) + recipientPlayer.sendActionBar(message.getTranslatedMessage(actionPlayer, cache).color(hexPatterns)) } } diff --git a/action-lib/src/main/kotlin/com/github/frcsty/actions/util/Extensions.kt b/action-lib/src/main/kotlin/com/github/frcsty/actions/util/Extensions.kt index 887b091..664f3ae 100644 --- a/action-lib/src/main/kotlin/com/github/frcsty/actions/util/Extensions.kt +++ b/action-lib/src/main/kotlin/com/github/frcsty/actions/util/Extensions.kt @@ -10,44 +10,21 @@ import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.plugin.Plugin -private val LEGACY_HEX_PATTERN: Pattern = Pattern.compile("#<([A-Fa-f0-9]){6}>") -private val HEX_PATTERN: Pattern = Pattern.compile("#([A-Fa-f0-9]){6}") - -fun String.color(): String { - var translation = this.colorLegacy() - - var matcher = HEX_PATTERN.matcher(translation) - - while (matcher.find()) { - val hex: ChatColor = ChatColor.of(matcher.group()) - val before = translation.substring(0, matcher.start()) - val after = translation.substring(matcher.end()) - - translation = before + hex + after - matcher = HEX_PATTERN.matcher(translation) - } - - return ChatColor.translateAlternateColorCodes('&', translation) -} - -fun String.colorLegacy(): String { +fun String.color(hexPatterns: LinkedHashSet): String { var translation = this - - var matcher = LEGACY_HEX_PATTERN.matcher(translation) - - while (matcher.find()) { - var hexString = matcher.group() - - hexString = "#" + hexString.substring(2, hexString.length - 1) - val hex: ChatColor = ChatColor.of(hexString) - val before = translation.substring(0, matcher.start()) - val after = translation.substring(matcher.end()) - - translation = before + hex + after - matcher = LEGACY_HEX_PATTERN.matcher(translation) + for (hexPattern in hexPatterns) { + var matcher = hexPattern.matcher(translation) + + while (matcher.find()) { + val hex: ChatColor = ChatColor.of("#" + matcher.group("hex")) + val before = translation.substring(0, matcher.start()) + val after = translation.substring(matcher.end()) + translation = before + hex + after + matcher = hexPattern.matcher(translation) + } } - return translation + return ChatColor.translateAlternateColorCodes('&', translation) } fun String.setPAPIPlaceholders(player: Player, cache: PlaceholderCache? = null): String { @@ -58,16 +35,16 @@ fun String.replacePlaceholder(placeholder: String, value: String): String { return this.replace(placeholder, value) } -fun Player.sendTranslatedMessage(message: String, cache: PlaceholderCache? = null) { - this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(this, cache).color())) +fun Player.sendTranslatedMessage(message: String, hexPatterns: LinkedHashSet, cache: PlaceholderCache? = null) { + this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(this, cache).color(hexPatterns))) } -fun Player.sendTranslatedMessage(message: String, player: Player, cache: PlaceholderCache? = null) { - this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color())) +fun Player.sendTranslatedMessage(message: String, player: Player, hexPatterns: LinkedHashSet, cache: PlaceholderCache? = null) { + this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color(hexPatterns))) } -fun CommandSender.sendTranslatedMessage(player: Player, message: String, cache: PlaceholderCache? = null) { - this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color())) +fun CommandSender.sendTranslatedMessage(player: Player, message: String, hexPatterns: LinkedHashSet, cache: PlaceholderCache? = null) { + this.spigot().sendMessage(*TextComponent.fromLegacyText(message.getTranslatedMessage(player, cache).color(hexPatterns))) } fun String.getTranslatedMessage(player: Player, cache: PlaceholderCache? = null): String { diff --git a/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt b/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt index 724dafa..f1c95d2 100644 --- a/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/FormatCommand.kt @@ -5,6 +5,7 @@ import com.github.frcsty.actions.util.replacePlaceholder import com.github.frcsty.actions.util.sendTranslatedMessage import com.github.frcsty.configuration.MessageLoader import com.github.frcsty.load.Loader +import com.github.frcsty.load.Settings import com.github.frcsty.util.removeCustomMessage import com.github.frcsty.util.setCustomMessage import me.mattstudios.mf.annotations.Alias @@ -51,7 +52,8 @@ class FormatCommand(private val messageLoader: MessageLoader, private val loader player, messageLoader.getMessage("customMessageSetTargetMessage") .replacePlaceholder("%type%", argument.lowercase()) - .replacePlaceholder("%message%", message.color()), + .replacePlaceholder("%message%", message.color(loader.settings.hexPatterns)), + loader.settings.hexPatterns, loader.placeholderCache ) } @@ -76,6 +78,6 @@ class FormatCommand(private val messageLoader: MessageLoader, private val loader } sender.sendTranslatedMessage(player, messageLoader.getMessage("customMessageRemoveTargetMessage") - .replacePlaceholder("%type%", argument.lowercase()), loader.placeholderCache) + .replacePlaceholder("%type%", argument.lowercase()), loader.settings.hexPatterns, loader.placeholderCache) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt b/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt index 78fc496..e51cf0b 100644 --- a/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/HelpCommand.kt @@ -31,7 +31,7 @@ class HelpCommand( val help = messageLoader.getMessageList("helpMessage") for (line in help) { - sender.sendMessage(line.replacePlaceholder("{version}", settings.pluginVersion).color()) + sender.sendMessage(line.replacePlaceholder("{version}", settings.pluginVersion).color(settings.hexPatterns)) } if (settings.debug) logger.info("Executor ${sender.name} executed action 'help'") diff --git a/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt b/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt index 7d78698..2ab70c1 100644 --- a/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/InfoCommand.kt @@ -30,7 +30,7 @@ class InfoCommand( val lines = messageLoader.getMessageList("infoMessage") for (line in lines) { - sender.sendMessage((line.replacePlaceholder("{version}", settings.pluginVersion)).color()) + sender.sendMessage((line.replacePlaceholder("{version}", settings.pluginVersion)).color(settings.hexPatterns)) } if (settings.debug) logger.info("Executor ${sender.name} executed action 'info'") diff --git a/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt b/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt index af72efb..f3dd414 100644 --- a/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt +++ b/src/main/kotlin/com/github/frcsty/command/ReloadCommand.kt @@ -44,7 +44,7 @@ class ReloadCommand( }.runTaskAsynchronously(plugin) val estimatedTime = System.currentTimeMillis() - startTime - sender.sendMessage((message.replace("%time%", estimatedTime.toString()).color())) + sender.sendMessage((message.replace("%time%", estimatedTime.toString()).color(loader.settings.hexPatterns))) if (loader.settings.debug) plugin.logger.info("Executor ${sender.name} executed action 'reload'") } diff --git a/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt b/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt index 3fd75b3..b9aa07c 100644 --- a/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt +++ b/src/main/kotlin/com/github/frcsty/configuration/MessageLoader.kt @@ -2,9 +2,11 @@ package com.github.frcsty.configuration import com.github.frcsty.FrozenJoinPlugin import com.github.frcsty.actions.util.color +import com.github.frcsty.load.Settings import org.bukkit.configuration.ConfigurationSection +import java.util.regex.Pattern -class MessageLoader(private val plugin: FrozenJoinPlugin) { +class MessageLoader(private val plugin: FrozenJoinPlugin, private val settings: Settings) { private val messages = mutableMapOf() private val listMessages = mutableMapOf?>() @@ -23,7 +25,7 @@ class MessageLoader(private val plugin: FrozenJoinPlugin) { fun getMessage(key: String): String { val message = messages[key] ?: key - return message.color() + return message.color(settings.hexPatterns) } fun getMessageList(key: String): List { diff --git a/src/main/kotlin/com/github/frcsty/load/Loader.kt b/src/main/kotlin/com/github/frcsty/load/Loader.kt index 0e6cb96..b31e8aa 100644 --- a/src/main/kotlin/com/github/frcsty/load/Loader.kt +++ b/src/main/kotlin/com/github/frcsty/load/Loader.kt @@ -25,10 +25,11 @@ class Loader(private val plugin: FrozenJoinPlugin) : Loader { override val actionHandler = ActionHandler(plugin, this, settings) val formatManager = FormatManager(plugin) val positionStorage = PositionStorage - private val messageLoader = MessageLoader(plugin) + private val messageLoader = MessageLoader(plugin, settings) fun initialize() { plugin.saveDefaultConfig() + settings.loadHexPatterns() positionStorage.initialize(plugin) messageLoader.load() @@ -73,16 +74,16 @@ class Loader(private val plugin: FrozenJoinPlugin) : Loader { val handler = manager.messageHandler with(handler) { register("cmd.no.console") { sender: CommandSender -> - sender.sendMessage(messages.getMessage("playerOnlyMessage").color()) + sender.sendMessage(messages.getMessage("playerOnlyMessage").color(settings.hexPatterns)) } register("cmd.no.permission") { sender: CommandSender -> - sender.sendMessage(messages.getMessage("denyMessage").color()) + sender.sendMessage(messages.getMessage("denyMessage").color(settings.hexPatterns)) } register("cmd.no.exists") { sender: CommandSender -> - sender.sendMessage(messages.getMessage("unknownCommandMessage").color()) + sender.sendMessage(messages.getMessage("unknownCommandMessage").color(settings.hexPatterns)) } register("cmd.wrong.usage") { sender: CommandSender -> - sender.sendMessage(messages.getMessage("usageMessage").color()) + sender.sendMessage(messages.getMessage("usageMessage").color(settings.hexPatterns)) } } } diff --git a/src/main/kotlin/com/github/frcsty/load/Settings.kt b/src/main/kotlin/com/github/frcsty/load/Settings.kt index 791a8aa..35ac9c0 100644 --- a/src/main/kotlin/com/github/frcsty/load/Settings.kt +++ b/src/main/kotlin/com/github/frcsty/load/Settings.kt @@ -2,8 +2,15 @@ package com.github.frcsty.load import com.github.frcsty.FrozenJoinPlugin import com.github.frcsty.actions.load.Settings +import java.util.regex.Pattern + +private val OLD_HEX_PATTERN: Pattern = Pattern.compile("#<(?[A-Fa-f0-9]{6})>") +private val AMPERSAND_HEX_PATTERN: Pattern = Pattern.compile("&#(?[A-Fa-f0-9]{6})") +private val SIMPLE_HEX_PATTERN: Pattern = Pattern.compile("#(?[A-Fa-f0-9]{6})") class Settings(private val plugin: FrozenJoinPlugin) : Settings { + + override var hexPatterns = linkedSetOf() override val cacheUpdateInterval = plugin.config.getLong("placeholder-cache.update-interval", 20L) override var cachedPlaceholders: List = plugin.config.getStringList("placeholder-cache.placeholders") override val debug = plugin.config.getString("consoleMessages", "ENABLED").equals("ENABLED", ignoreCase = true) @@ -14,5 +21,13 @@ class Settings(private val plugin: FrozenJoinPlugin) : Settings { fun reload() { cachedPlaceholders = plugin.config.getStringList("placeholder-cache.placeholders") + loadHexPatterns() + } + + fun loadHexPatterns() { + hexPatterns.clear() + if (plugin.config.getBoolean("hex-patterns.old", true)) hexPatterns.add(OLD_HEX_PATTERN) + if (plugin.config.getBoolean("hex-patterns.ampersand", true)) hexPatterns.add(AMPERSAND_HEX_PATTERN) + if (plugin.config.getBoolean("hex-patterns.simple", true)) hexPatterns.add(SIMPLE_HEX_PATTERN) } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt b/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt index 8134d4f..df1ecfc 100644 --- a/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt +++ b/src/main/kotlin/com/github/frcsty/message/MessageFormatter.kt @@ -35,7 +35,7 @@ object MessageFormatter { if (motd == null) { if (command) { if (settings.debug) plugin.logger.info("Executor ${player.name} executed action 'motd'") - player.sendTranslatedMessage(message, cache) + player.sendTranslatedMessage(message, settings.hexPatterns, cache) } return } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b9e4ce4..259c0c2 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -237,6 +237,17 @@ placeholder-cache: placeholders: - "%luckperms_prefix%" +# Hex colors are parsed in the following order: old -> ampersand -> simple. This means there shouldn't be any conflicts +# so multiple patterns can be enabled at the same time. More patterns to be added in the future? +hex-patterns: + # Enable support for the # hex pattern + old: true + # Enable support for the &#aaFF00 hex pattern + ampersand: true + # Enable support for the #aaFF00 hex pattern + simple: true + + # If you want bstats to track your server leave this enabled, # otherwise disable it by setting it to false stonks: true \ No newline at end of file