From 984ffa4e0b072f7c4c9afc6652980dca3ea8eff8 Mon Sep 17 00:00:00 2001 From: Groovin Date: Thu, 25 Apr 2024 18:52:47 -0400 Subject: [PATCH] Update to support team name for all new placeholders This update not only adds the `displayname` parameter but also adds support for passing a team name to any placeholder. Placeholders are now also auto generated and will show all teams if using papi parse --- README.md | 7 +- gradle.properties | 2 +- .../dev/groovin/teamsexpansion/Plugin.kt | 66 ++++++++++++------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 2cb82f9..12e59ce 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,14 @@ Adds `6` new PlaceholderAPI expansions: - %team_prefix% - %team_suffix% - %team_count% -- %team_count_\% +- %team_displayname% - %team_name% +All placeholders accept an optional team name parameter, +for example `%team_color_test%` will return the color of the team named `test`. + +The team name parameter takes precedence over the player's team. + # How to build 1. Clone the repository diff --git a/gradle.properties b/gradle.properties index 0a1b451..5c439db 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ kotlin.code.style=official group=dev.groovin -version=1.0.2 +version=2.0.0 plugin_main=dev.groovin.teamsexpansion.Plugin diff --git a/src/main/kotlin/dev/groovin/teamsexpansion/Plugin.kt b/src/main/kotlin/dev/groovin/teamsexpansion/Plugin.kt index 558aa8c..7c513b7 100644 --- a/src/main/kotlin/dev/groovin/teamsexpansion/Plugin.kt +++ b/src/main/kotlin/dev/groovin/teamsexpansion/Plugin.kt @@ -3,41 +3,56 @@ package dev.groovin.teamsexpansion import me.clip.placeholderapi.expansion.PlaceholderExpansion import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer import org.bukkit.Bukkit -import org.bukkit.entity.Player +import org.bukkit.OfflinePlayer import org.bukkit.scoreboard.Scoreboard import org.bukkit.scoreboard.Team -// I added this because ChatColor is deprecated but this code still works const val COLOR_CHAR = '\u00A7' class TeamsExpansion : PlaceholderExpansion() { override fun canRegister(): Boolean = true - override fun getName(): String = "TeamsExpansion" override fun getIdentifier(): String = "team" override fun getAuthor(): String = "Groovin-Dev" override fun getVersion(): String = javaClass.getResource("/version.txt")?.readText() ?: "Unknown" - override fun getPlaceholders(): List = listOf("color", "prefix", "suffix", "name", "count", "count_").map { "%${identifier}_$it%" } - - override fun onPlaceholderRequest(player: Player?, params: String): String { - val teamName = params.substringAfter("_") - val team = if (params.startsWith("count_")) { - scoreboard.getTeam(teamName) - } else { - player?.let { scoreboard.getEntryTeam(it.name) } ?: scoreboard.getTeam(teamName) + override fun getPlaceholders(): List = generatePlaceholders() + + override fun onRequest(player: OfflinePlayer?, params: String): String { + val parts = params.split("_", limit = 2) + val placeholder = parts.getOrNull(0).orEmpty() + val teamName = parts.getOrNull(1).orEmpty() + + val team = getTeam(player, teamName) + + return when (placeholder) { + "color" -> team?.getTeamColor().orEmpty() + "prefix" -> team?.getTeamPrefix().orEmpty() + "suffix" -> team?.getTeamSuffix().orEmpty() + "count" -> team?.getTeamCount().orEmpty() + "name" -> team?.name.orEmpty() + "displayname" -> team?.getDisplayName.orEmpty() + else -> "" } + } - if (team == null) { - return "" + private fun generatePlaceholders(): List { + val placeholderTypes = listOf("color", "prefix", "suffix", "count", "name", "displayname") + val teams = scoreboard.teams.map { it.name } + + val placeholders = mutableListOf() + for (placeholderType in placeholderTypes) { + placeholders.add("%${identifier}_$placeholderType%") + for (team in teams) { + placeholders.add("%${identifier}_${placeholderType}_$team%") + } } + return placeholders + } + private fun getTeam(player: OfflinePlayer?, teamName: String): Team? { return when { - params.startsWith("color") -> team.getTeamColor() - params.startsWith("prefix") -> team.getTeamPrefix() - params.startsWith("suffix") -> team.getTeamSuffix() - params == "count" -> team.getTeamCount() - params.startsWith("count_") -> team.getTeamCount() - params == "name" -> team.name - else -> "" + teamName.isNotEmpty() -> scoreboard.getTeam(teamName) + player != null -> player.name?.let { scoreboard.getEntryTeam(it) } + else -> null } } @@ -46,10 +61,13 @@ class TeamsExpansion : PlaceholderExpansion() { } } -private fun Team.getTeamColor(): String = runCatching { color().toString() }.getOrDefault("") +private fun Team.getTeamColor(): String? = runCatching { color().toString() }.getOrNull() + +private fun Team.getTeamPrefix(): String = prefix().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) } -private fun Team.getTeamPrefix(): String = LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(prefix()) +private fun Team.getTeamSuffix(): String = suffix().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) } -private fun Team.getTeamSuffix(): String = LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(suffix()) +private fun Team.getTeamCount(): String = entries.size.toString() -private fun Team.getTeamCount(): String = entries.size.toString() \ No newline at end of file +private val Team.getDisplayName: String + get() = displayName().let { LegacyComponentSerializer.legacy(COLOR_CHAR).serialize(it) } \ No newline at end of file