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 0eee1b3 commit f21a0b2
Showing 1 changed file with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import org.bukkit.ChatColor
import org.bukkit.entity.Player

internal class FallbackFormatter(private val plugin: ChatPlugin) : BaseFormatter(plugin) {
private fun formatOnePlayer(
private fun format(
player: Player,
playerName: String,
template: String,
message: String,
alwaysTranslateColors: Boolean = false,
to: (() -> String?)? = null
): String {
return prepareTemplate(template,
{ playerName },
{ playerName },
to ?: { playerName },
{ playerName },
{ playerName },
{ playerName },
Expand All @@ -32,41 +33,28 @@ internal class FallbackFormatter(private val plugin: ChatPlugin) : BaseFormatter
}

override fun formatPrivate(from: Player, to: Player, message: String): String {
return prepareTemplate(plugin.config.privateTemplate,
{ "%1\$s" },
{ to.displayName },
{ "%1\$s" },
{ "%1\$s" },
{ "%1\$s" },
{ "%1\$s" },
{
if (from.hasPermission("mcn.chat.colors")) {
ChatColor.translateAlternateColorCodes(
'&', message
)
} else {
ChatColor.stripColor(message)
}
})
return format(from, "%1\$s", plugin.config.fromConsoleTemplate, message, to = {
to.displayName
})
}

override fun formatConsole(to: Player, message: String): String {
return formatOnePlayer(to, to.displayName, plugin.config.consoleTemplate, message, false)
return format(to, to.displayName, plugin.config.consoleTemplate, message, alwaysTranslateColors = true)
}

override fun formatToConsole(from: Player, message: String): String {
return formatOnePlayer(from, "%1\$s", plugin.config.fromConsoleTemplate, message)
return format(from, "%1\$s", plugin.config.fromConsoleTemplate, message)
}

override fun formatGlobal(from: Player, message: String): String {
return formatOnePlayer(from, "%1\$s", plugin.config.globalTemplate, message)
return format(from, "%1\$s", plugin.config.globalTemplate, message)
}

override fun formatLocal(from: Player, message: String): String {
return formatOnePlayer(from, "%1\$s", plugin.config.localTemplate, message)
return format(from, "%1\$s", plugin.config.localTemplate, message)
}

override fun formatSpy(from: Player, message: String): String {
return formatOnePlayer(from, "%1\$s", plugin.config.spyTemplate, message)
return format(from, "%1\$s", plugin.config.spyTemplate, message)
}
}

0 comments on commit f21a0b2

Please # to comment.