From dd1c85bfbb74064fb74bbd1e9e6fc0915dd8f28d Mon Sep 17 00:00:00 2001 From: wtlgo Date: Wed, 8 Mar 2023 04:31:30 +0300 Subject: [PATCH] Reduce duplication --- .../papi/formatting/PAPIFormatter.kt | 102 ++++++------------ 1 file changed, 30 insertions(+), 72 deletions(-) diff --git a/src/main/java/dev/mikchan/mcnp/chat/implementation/papi/formatting/PAPIFormatter.kt b/src/main/java/dev/mikchan/mcnp/chat/implementation/papi/formatting/PAPIFormatter.kt index 6deb6f1..165ce0a 100644 --- a/src/main/java/dev/mikchan/mcnp/chat/implementation/papi/formatting/PAPIFormatter.kt +++ b/src/main/java/dev/mikchan/mcnp/chat/implementation/papi/formatting/PAPIFormatter.kt @@ -7,100 +7,58 @@ import org.bukkit.ChatColor import org.bukkit.entity.Player internal class PAPIFormatter(private val plugin: ChatPlugin) : BaseFormatter(plugin) { - private fun prepareFromPlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.fromTemplate) - ) + private fun prepareColors(player: Player, template: String): String { + return ChatColor.translateAlternateColorCodes('&', PlaceholderAPI.setPlaceholders(player, template)) } - private fun prepareToPlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.toTemplate) - ) - } - - private fun preparePlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.playerTemplate) - ) - } - - private fun prepareGlobalPlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.globalPlayerTemplate) - ) - } - - private fun prepareLocalPlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.localPlayerTemplate) - ) - } - - private fun prepareSpyPlayer(player: Player): String { - return ChatColor.translateAlternateColorCodes( - '&', PlaceholderAPI.setPlaceholders(player, plugin.config.spyPlayerTemplate) - ) - } - - private fun formatOnePlayer(player: Player, template: String, message: String): String { + private fun format( + player: Player, + template: String, + message: String, + alwaysTranslateColors: Boolean = false, + to: (() -> String?)? = null, + ): String { return prepareTemplate(template, - { prepareFromPlayer(player) }, - { prepareToPlayer(player) }, - { preparePlayer(player) }, - { prepareGlobalPlayer(player) }, - { prepareLocalPlayer(player) }, - { prepareSpyPlayer(player) }, + { prepareColors(player, plugin.config.fromTemplate) }, + to ?: { prepareColors(player, plugin.config.toTemplate) }, + { prepareColors(player, plugin.config.playerTemplate) }, + { prepareColors(player, plugin.config.globalPlayerTemplate) }, + { prepareColors(player, plugin.config.localPlayerTemplate) }, + { prepareColors(player, plugin.config.spyPlayerTemplate) }, { - if (player.hasPermission("mcn.chat.colors")) ChatColor.translateAlternateColorCodes( - '&', message - ) else ChatColor.stripColor(message) + if (alwaysTranslateColors || player.hasPermission("mcn.chat.colors")) { + ChatColor.translateAlternateColorCodes( + '&', message + ) + } else { + ChatColor.stripColor(message) + } }) } override fun formatPrivate(from: Player, to: Player, message: String): String { - return prepareTemplate(plugin.config.privateTemplate, - { prepareFromPlayer(from) }, - { prepareToPlayer(to) }, - { preparePlayer(from) }, - { prepareGlobalPlayer(from) }, - { prepareLocalPlayer(from) }, - { prepareSpyPlayer(from) }, - { - if (from.hasPermission("mcn.chat.colors")) ChatColor.translateAlternateColorCodes( - '&', message - ) else ChatColor.stripColor(message) - }) + return format(from, plugin.config.privateTemplate, message, to = { + prepareColors(to, plugin.config.toTemplate) + }) } override fun formatConsole(to: Player, message: String): String { - return prepareTemplate(plugin.config.consoleTemplate, - { prepareFromPlayer(to) }, - { prepareToPlayer(to) }, - { preparePlayer(to) }, - { prepareGlobalPlayer(to) }, - { prepareLocalPlayer(to) }, - { prepareSpyPlayer(to) }, - { - ChatColor.translateAlternateColorCodes( - '&', message - ) - }) + return format(to, plugin.config.consoleTemplate, message, alwaysTranslateColors = true) } override fun formatToConsole(from: Player, message: String): String { - return formatOnePlayer(from, plugin.config.consoleTemplate, message) + return format(from, plugin.config.fromConsoleTemplate, message) } override fun formatGlobal(from: Player, message: String): String { - return formatOnePlayer(from, plugin.config.globalTemplate, message) + return format(from, plugin.config.globalTemplate, message) } override fun formatLocal(from: Player, message: String): String { - return formatOnePlayer(from, plugin.config.localTemplate, message) + return format(from, plugin.config.localTemplate, message) } override fun formatSpy(from: Player, message: String): String { - return formatOnePlayer(from, plugin.config.spyTemplate, message) + return format(from, plugin.config.spyTemplate, message) } }