Skip to content

Commit

Permalink
Reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Mar 8, 2023
1 parent f21a0b2 commit dd1c85b
Showing 1 changed file with 30 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit dd1c85b

Please # to comment.